Created
October 19, 2016 12:01
-
-
Save Timmmm/62c5c86d1092f763b29a96e15c567888 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Copyright 2016 Alex Yanchenko | |
* | |
* Extracted from DroidParts by Tim Hutt, 1/8/2016. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com....; | |
import java.io.PrintWriter; | |
import java.io.StringWriter; | |
import android.content.Context; | |
import android.content.pm.ApplicationInfo; | |
import android.content.pm.PackageManager; | |
import android.util.Log; | |
public class L | |
{ | |
// Call this in your Application's onCreate(). Like this: | |
// @Override | |
// public void onCreate() | |
// { | |
// super.onCreate(); | |
// L.setApplicationContext(getApplicationContext()); | |
// } | |
public static void setApplicationContext(Context c) | |
{ | |
mApplicationContext = c; | |
} | |
public interface Listener | |
{ | |
void onMessageLogged(int priority, String tag, String msg); | |
} | |
public static void v(Object obj) | |
{ | |
if (isLoggable(VERBOSE)) | |
log(VERBOSE, obj); | |
} | |
public static void v(String format, Object... args) | |
{ | |
if (isLoggable(VERBOSE)) | |
log(VERBOSE, format, args); | |
} | |
public static void d(Object obj) | |
{ | |
if (isLoggable(DEBUG)) | |
log(DEBUG, obj); | |
} | |
public static void d(String format, Object... args) | |
{ | |
if (isLoggable(DEBUG)) | |
log(DEBUG, format, args); | |
} | |
public static void i(Object obj) | |
{ | |
if (isLoggable(INFO)) | |
log(INFO, obj); | |
} | |
public static void i(String format, Object... args) | |
{ | |
if (isLoggable(INFO)) | |
log(INFO, format, args); | |
} | |
public static void w(Object obj) | |
{ | |
if (isLoggable(WARN)) | |
log(WARN, obj); | |
} | |
public static void w(String format, Object... args) | |
{ | |
if (isLoggable(WARN)) | |
log(WARN, format, args); | |
} | |
public static void e(Object obj) | |
{ | |
if (isLoggable(ERROR)) | |
log(ERROR, obj); | |
} | |
public static void e(String format, Object... args) | |
{ | |
if (isLoggable(ERROR)) | |
log(ERROR, format, args); | |
} | |
public static void wtf(Object obj) | |
{ | |
if (isLoggable(ASSERT)) | |
log(ASSERT, obj); | |
} | |
public static void wtf(String format, Object... args) | |
{ | |
if (isLoggable(ASSERT)) | |
log(ASSERT, format, args); | |
} | |
public static void wtf() | |
{ | |
if (isLoggable(ASSERT)) | |
log(ASSERT, "WTF"); | |
} | |
public static boolean isLoggable(int level) | |
{ | |
return (level >= getLogLevel()); | |
} | |
public static final int VERBOSE = Log.VERBOSE; | |
public static final int DEBUG = Log.DEBUG; | |
public static final int INFO = Log.INFO; | |
public static final int WARN = Log.WARN; | |
public static final int ERROR = Log.ERROR; | |
public static final int ASSERT = Log.ASSERT; | |
private static final int DISABLE = 1024; | |
public static void setListener(Listener listener) | |
{ | |
L.listener = listener; | |
} | |
private static Listener listener; | |
private static final String TAG = "L"; | |
private static void log(int priority, Object obj) | |
{ | |
String msg; | |
if (obj instanceof Throwable) | |
{ | |
StringWriter sw = new StringWriter(); | |
((Throwable) obj).printStackTrace(new PrintWriter(sw)); | |
msg = sw.toString(); | |
} | |
else | |
{ | |
msg = String.valueOf(obj); | |
if (msg == null || msg.length() == 0) | |
{ | |
msg = "\"\""; | |
} | |
} | |
Log.println(priority, getTag(isDebug()), msg); | |
} | |
private static void log(int priority, String format, Object... args) | |
{ | |
try | |
{ | |
String tag = getTag(isDebug()); | |
String msg = String.format(format, args); | |
Log.println(priority, tag, msg); | |
if (listener != null) | |
{ | |
listener.onMessageLogged(priority, tag, msg); | |
} | |
} | |
catch (Exception e) | |
{ | |
e(e); | |
} | |
} | |
private static boolean isDebug() | |
{ | |
if (_debug == 0) | |
{ | |
Context ctx = mApplicationContext; | |
if (ctx != null) | |
{ | |
_debug = isDebuggable(ctx) ? 1 : -1; | |
} | |
} | |
return (_debug == 1); | |
} | |
private static int getLogLevel() | |
{ | |
if (_logLevel == 0) | |
{ | |
Context ctx = mApplicationContext; | |
if (ctx != null) | |
{ | |
String logLevelStr = getManifestValue(ctx, LOG_LEVEL); | |
if (VERBOSE_STRING.equalsIgnoreCase(logLevelStr)) | |
{ | |
_logLevel = VERBOSE; | |
} | |
else if (DEBUG_STRING.equalsIgnoreCase(logLevelStr)) | |
{ | |
_logLevel = DEBUG; | |
} | |
else if (INFO_STRING.equalsIgnoreCase(logLevelStr)) | |
{ | |
_logLevel = INFO; | |
} | |
else if (WARN_STRING.equalsIgnoreCase(logLevelStr)) | |
{ | |
_logLevel = WARN; | |
} | |
else if (ERROR_STRING.equalsIgnoreCase(logLevelStr)) | |
{ | |
_logLevel = ERROR; | |
} | |
else if (ASSERT_STRING.equalsIgnoreCase(logLevelStr)) | |
{ | |
_logLevel = ASSERT; | |
} | |
else if (DISABLE_STRING.equalsIgnoreCase(logLevelStr)) | |
{ | |
_logLevel = DISABLE; | |
} | |
else | |
{ | |
_logLevel = VERBOSE; | |
Log.i(TAG, "No valid <meta-data android:name=\"" + LOG_LEVEL | |
+ "\" android:value=\"...\"/> in AndroidManifest.xml."); | |
} | |
} | |
} | |
return (_logLevel != 0) ? _logLevel : VERBOSE; | |
} | |
private static String getTag(boolean debug) | |
{ | |
if (debug) | |
{ | |
StackTraceElement caller = Thread.currentThread().getStackTrace()[5]; | |
String c = caller.getClassName(); | |
String className = c.substring(c.lastIndexOf(".") + 1, c.length()); | |
StringBuilder sb = new StringBuilder(5); | |
sb.append(className); | |
sb.append("."); | |
sb.append(caller.getMethodName()); | |
sb.append("():"); | |
sb.append(caller.getLineNumber()); | |
return sb.toString(); | |
} | |
else | |
{ | |
if (_tag == null) | |
{ | |
Context ctx = mApplicationContext; | |
if (ctx != null) | |
{ | |
_tag = ctx.getPackageName(); | |
} | |
} | |
return (_tag != null) ? _tag : TAG; | |
} | |
} | |
private static int _debug; | |
private static int _logLevel; | |
private static String _tag; | |
private static Context mApplicationContext; | |
private L() | |
{ | |
} | |
public static boolean isDebuggable(Context ctx) { | |
ApplicationInfo appInfo = ctx.getApplicationInfo(); | |
boolean debug = (appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; | |
return debug; | |
} | |
public static final String LOG_LEVEL = "l_log_level"; | |
public static final String DISABLE_STRING = "disable"; | |
public static final String VERBOSE_STRING = "verbose"; | |
public static final String DEBUG_STRING = "debug"; | |
public static final String INFO_STRING = "info"; | |
public static final String WARN_STRING = "warn"; | |
public static final String ERROR_STRING = "error"; | |
public static final String ASSERT_STRING = "assert"; | |
public static String getManifestValue(Context ctx, String key) | |
{ | |
try | |
{ | |
return ctx.getPackageManager().getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA).metaData | |
.getString(key); | |
} | |
catch (Exception e) | |
{ | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment