Back to project page demo-flickr-feed-android.
The source code is released under:
This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...
If you think the Android project demo-flickr-feed-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package uk.org.tomek.flickrfeed.utils; //from w ww . ja v a2 s . c o m import android.util.Log; /** * Logger class. */ public final class Logger { private static final boolean IS_DEBUG_ENABLED = true; private static final int MAX_LOG_TAG_LENGTH = 30; private final String mLogPrefix; /** * Use the supplied custom prefix in log output. */ private Logger(String logPrefix) { if (logPrefix.length() > MAX_LOG_TAG_LENGTH) { mLogPrefix = logPrefix.substring(0, MAX_LOG_TAG_LENGTH - 1); } else { mLogPrefix = logPrefix; } } public static Logger getLogger(Class<?> classZ) { return new Logger(classZ.getSimpleName()); } public final void i(final String msg) { log(Log.INFO, msg); } public final void i(final String msg, final Throwable throwable) { log(Log.INFO, msg, throwable); } public final void i(final String msg, final Object... args) { log(Log.INFO, String.format(msg, args)); } public final void i(final String msg, final Throwable throwable, final Object... args) { log(Log.INFO, String.format(msg, args), throwable); } public final void d(final String msg) { log(Log.DEBUG, msg); } public final void d(final String msg, final Object... args) { log(Log.DEBUG, String.format(msg, args)); } public final void d(final String msg, final Throwable throwable) { log(Log.DEBUG, msg, throwable); } public final void d(final String msg, final Throwable throwable, final Object... args) { log(Log.DEBUG, String.format(msg, args), throwable); } public final void e(final String msg) { log(Log.ERROR, msg); } public final void e(final String msg, final Object... args) { log(Log.ERROR, String.format(msg, args)); } public final void e(final String msg, final Throwable throwable) { log(Log.ERROR, msg, throwable); } public final void e(final String msg, final Throwable throwable, final Object... args) { log(Log.ERROR, String.format(msg, args), throwable); } public final void v(String msg) { log(Log.VERBOSE, msg); } public final void v(final String msg, final Object... args) { log(Log.VERBOSE, String.format(msg, args)); } public final void v(final String msg, final Throwable throwable) { log(Log.VERBOSE, msg, throwable); } public final void v(final String msg, final Throwable throwable, final Object... args) { log(Log.VERBOSE, String.format(msg, args), throwable); } public final void w(final String msg) { log(Log.WARN, msg); } public final void w(final String msg, final Object... args) { log(Log.WARN, String.format(msg, args)); } public final void w(final String msg, final Throwable throwable) { log(Log.WARN, msg, throwable); } public final void w(final String msg, final Throwable throwable, final Object... args) { log(Log.WARN, String.format(msg, args), throwable); } public final void w(final Throwable throwable) { log(Log.ASSERT, Log.getStackTraceString(throwable)); } public final void wtf(final String msg) { log(Log.ASSERT, msg); } public final void wtf(final String msg, final Object... args) { log(Log.ASSERT, String.format(msg, args)); } public final void wtf(final String msg, final Throwable throwable) { log(Log.ASSERT, msg, throwable); } public final void wtf(final String msg, final Throwable throwable, final Object... args) { log(Log.ASSERT, String.format(msg, args), throwable); } private final void log(final int flag, final String msg) { log(flag, msg, null); } private final void log(final int flag, String msg, final Throwable throwable) { if (IS_DEBUG_ENABLED) { if (throwable != null) { msg += '\n' + Log.getStackTraceString(throwable); } Log.println(flag, mLogPrefix, getMsg(msg)); } } private static String getMsg(String msg) { StackTraceElement[] stackTraceElement = Thread.currentThread().getStackTrace(); Integer methodIndex = null; boolean loggerFound = false; boolean stop = false; for (int i = 0; i < stackTraceElement.length && !stop; i++) { if (stackTraceElement[i].getClassName().contains(Logger.class.getSimpleName())) { loggerFound = true; } else if (loggerFound) { methodIndex = i; stop = true; } } if (methodIndex != null) { String fullClassName = stackTraceElement[methodIndex].getClassName(); String className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1); String methodName = stackTraceElement[methodIndex].getMethodName(); String lineNumber = String.valueOf(stackTraceElement[methodIndex].getLineNumber()); return String.format("#%s %s(%s.java:%s)", methodName, msg, className, lineNumber); } else { return msg; } } }