Last active
December 24, 2015 13:49
-
-
Save stavfx/6808390 to your computer and use it in GitHub Desktop.
Android Logger class that prints out info about the calling method, just call Log.w("msg") but still get a detailed log and never forget where that log call came from :)
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
/** | |
* Created by StavFX on 10/3/13. | |
*/ | |
public class Log { | |
private static final boolean DEBUG = true; | |
private static final String NEW_LINE = System.getProperty("line.separator"); | |
public static void w(String tag, String msg) { | |
log(tag, msg); | |
} | |
public static void w(String msg) { | |
log(null, msg); | |
} | |
private static void log(String tag, String msg) { | |
if (!DEBUG) | |
return; | |
final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); | |
if (stackTrace.length > 4) { | |
final StackTraceElement callingMethod = stackTrace[4]; | |
String header; | |
if (tag != null) | |
header = String.format("TAG[%s] (%s:%d)=>%s() <==", tag, callingMethod.getFileName(), callingMethod.getLineNumber(), callingMethod.getMethodName()); | |
else | |
header = String.format("(%s:%d)=>%s() <==", callingMethod.getFileName(), callingMethod.getLineNumber(), callingMethod.getMethodName()); | |
String footer = String.format("%" + (header.length() - 1) + "s>", "=").replace(' ', '='); | |
msg = String.format("%s%s%s%s%s", header, NEW_LINE, msg, NEW_LINE, footer); | |
} | |
System.out.println(msg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool!