Skip to content

Instantly share code, notes, and snippets.

@billmote
Created August 10, 2014 14:23
Show Gist options
  • Save billmote/1ee629948488798f1640 to your computer and use it in GitHub Desktop.
Save billmote/1ee629948488798f1640 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="logging">true</string>
<string name="logging_level">2</string> <!-- 2 = Verbose, 7 = Assert -->
</resources>
/**
* Log
* <p/>
* <pre>
* 20140623 -- Added wtf logging and a wrapper to protect against API versions prior to FROYO (8)
* 20130114 -- Code Review
* </pre>
*/
public class Log {
/**
* wtf was introduced in FROYO, however, this Log class could be used in a version prior
* to Froyo and would not know about Build.VERSION_CODES.FROYO so create a constant.
*/
public static final int MINIMUM_API_LEVEL_SUPPORTED_FOR_WTF_IS_FROYO = 8;
/**
* Level of information to log, ASSERT (7) and ERROR (6) being the highest
* numbers and least info and VERBOSE (2) and DEBUG (3) being the lowest
* numbers and most info. Defaults to Verbose, the most information.
*/
public static int logLevel = android.util.Log.VERBOSE;
/**
* Gives ability to turn logging on or off at a global level. Defaults to be
* off. Can be set by the application.
*/
private static boolean isLogging = false;
private Log() {
// Do not allow this class to be instantiated.
}
/**
* @return if logging is on.
*/
public static boolean isLogging() {
return isLogging;
}
/**
* Allows the application to set logging on or off.
*
* @param isLogging true if logging is on, false if logging is off
*/
public static void setLogging(boolean isLogging) {
Log.isLogging = isLogging;
}
/**
* @return the level of information to log, ASSERT (7) and ERROR (6) being
* the highest numbers and least info and VERBOSE (2) and DEBUG (3)
* being the lowest numbers and most info.
*/
public static int getLogLevel() {
return logLevel;
}
/**
* @param logLevel the level of information to log, ASSERT (7) and ERROR (6)
* being the highest numbers and least info and VERBOSE (2) and
* DEBUG (3) being the lowest numbers and most info.
*/
public static void setLogLevel(int logLevel) {
Log.logLevel = logLevel;
}
// 2
public static int v(String tag, String msg) {
if (logLevel <= android.util.Log.VERBOSE && isLogging) {
return android.util.Log.v(tag, msg);
}
return 0;
}
// 2
public static int v(String tag, String msg, Throwable tr) {
if (logLevel <= android.util.Log.VERBOSE && isLogging) {
return android.util.Log.v(tag, msg, tr);
}
return 0;
}
// 3
public static int d(String tag, String msg) {
if (logLevel <= android.util.Log.DEBUG && isLogging) {
return android.util.Log.d(tag, msg);
}
return 0;
}
// 3
public static int d(String tag, String msg, Throwable tr) {
if (logLevel <= android.util.Log.DEBUG && isLogging) {
return android.util.Log.d(tag, msg, tr);
}
return 0;
}
// 4
public static int i(String tag, String msg) {
if (logLevel <= android.util.Log.INFO && isLogging) {
return android.util.Log.i(tag, msg);
}
return 0;
}
// 4
public static int i(String tag, String msg, Throwable tr) {
if (logLevel <= android.util.Log.INFO && isLogging) {
return android.util.Log.i(tag, msg, tr);
}
return 0;
}
// 5
public static int w(String tag, Throwable tr) {
if (logLevel <= android.util.Log.WARN && isLogging) {
return android.util.Log.w(tag, tr);
}
return 0;
}
// 5
public static int w(String tag, String msg) {
if (logLevel <= android.util.Log.WARN && isLogging) {
return android.util.Log.w(tag, msg);
}
return 0;
}
// 5
public static int w(String tag, String msg, Throwable tr) {
if (logLevel <= android.util.Log.WARN && isLogging) {
return android.util.Log.w(tag, msg, tr);
}
return 0;
}
// 6
public static int e(String tag, String msg) {
if (logLevel <= android.util.Log.ERROR && isLogging) {
return android.util.Log.e(tag, msg);
}
return 0;
}
// 6
public static int e(String tag, String msg, Throwable tr) {
if (logLevel <= android.util.Log.ERROR && isLogging) {
return android.util.Log.e(tag, msg, tr);
}
return 0;
}
// 7 or 6 if on API level 7 or less.
public static int wtf(String tag, Throwable tr) {
if (logLevel <= android.util.Log.ASSERT && isLogging) {
if (Build.VERSION.SDK_INT >= MINIMUM_API_LEVEL_SUPPORTED_FOR_WTF_IS_FROYO) {
return android.util.Log.wtf(tag, tr);
} else {
// There is no e(String, Throwable) method
return android.util.Log.e(tag, tr.getLocalizedMessage());
}
}
return 0;
}
// 7 or 6 if on API level 7 or less.
public static int wtf(String tag, String msg) {
if (logLevel <= android.util.Log.ASSERT && isLogging) {
if (Build.VERSION.SDK_INT >= MINIMUM_API_LEVEL_SUPPORTED_FOR_WTF_IS_FROYO) {
return android.util.Log.wtf(tag, msg);
} else {
return android.util.Log.e(tag, msg);
}
}
return 0;
}
// 7 or 6 if on API level 7 or less.
public static int wtf(String tag, String msg, Throwable tr) {
if (logLevel <= android.util.Log.ASSERT && isLogging) {
if (Build.VERSION.SDK_INT >= MINIMUM_API_LEVEL_SUPPORTED_FOR_WTF_IS_FROYO) {
return android.util.Log.wtf(tag, msg, tr);
} else {
return android.util.Log.e(tag, msg, tr);
}
}
return 0;
}
}
/**
* Created by bill.mote on 5/24/14.
*/
public class CodersConfessionalApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Set whether we're logging and at what level
try {
Log.setLogging(BuildConfig.DEBUG && Boolean.valueOf(getString(R.string.logging)));
Log.setLogLevel(Integer.valueOf(getString(R.string.logging_level)));
} catch (Exception e) {
Log.e(TAG, "Something went wrong setting logging and/or logging level. App will set what it can and use defaults otherwise.", e);
}
Log.i(TAG, "onCreate()");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment