Skip to content

Instantly share code, notes, and snippets.

@carchrae
Created October 24, 2013 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carchrae/7142250 to your computer and use it in GitHub Desktop.
Save carchrae/7142250 to your computer and use it in GitHub Desktop.
snippit of code for grabbing the stack/filename/lines in GWT
public class Log {
/*
* type - is "INFO", "DEBUG", "ERROR" etc
* object - is an "id" of the person calling it. as a convention, I use 'this' unless it is a static method, then I pass in the class
* msg - the log message
*/
public static void log(String type, Object object, String msg) {
String name;
if (object instanceof String) {
name = (String) object;
} else {
Class<? extends Object> clazz = null;
if ((object instanceof Class)) {
clazz = (Class<? extends Object>) object;
} else {
if (object == null) {
name = "?";
// default - stop outside/up one - i never use this path
clazz = Log.class;
} else {
clazz = object.getClass();
}
}
name = clazz.getName();
//after means we show then next matching element - eg, it is not interesting to see the line in the logger!
boolean after = Log.class.equals(clazz);
// name is now a class - lookup the line number
String line = findLineAndMethodInStack(name, after);
if (line!=null)
name += ":" + line;
}
String text = getTimeStamp() + " " + type + ": " + name + " : " + msg;
//these are my 'appenders' - you could just use GWT.log(text) or System.out.println(text);
if (SERVER_LOG)
new LogEvent().type(type).location(name).message(msg).user(user).fire();
if (GWT.isProdMode()) {
//these do stuff with chrome logger colors
nativeLog(type, text);
} else {
//ansi colors
String color = typeToColorMap.get(type);
System.out.println(color + text + Color.RESET);
}
}
private static String findLineAndMethodInStack(String name, boolean after) {
String[] parts = name.split("\\.");
//get rid of the package
name = parts[parts.length - 1];
//mildly complicated because of the 'after' (ie nextMatch) logic
boolean wasMatched = false;
StackTraceElement[] stack = new RuntimeException().getStackTrace();
for (StackTraceElement e : stack) {
String stackName = e.getFileName().replace(".java", "");
boolean isMatch = stackName.equals(name);
if (after) {
//check the next match logic
if (isMatch) {
//don't match yet, wait until it changes
isMatch = false;
wasMatched = true;
} else if (wasMatched && !isMatch)
//it must have changed, and we had a match before, so use this one
isMatch = true;
}
if (isMatch) {
String lineAndMethod = e.getLineNumber() + " (" + e.getMethodName()+ ")";
if (stackName.equals(name))
// exact match, so don't need the name
return ":" + lineAndMethod;
else
// name is something like Log.java, so we want to return the
// actual file name
return " " + stackName + ":" + lineAndMethod;
}
}
return null;
}
@carchrae
Copy link
Author

use it like this:

Log.log("DEBUG",this,"hey there, we are logging this");

I create a bunch of wrappers, so I can call

Log.debug(this,"hey there, debuggery!");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment