Created
October 24, 2013 18:11
-
-
Save carchrae/7142250 to your computer and use it in GitHub Desktop.
snippit of code for grabbing the stack/filename/lines in GWT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!");