Created
June 15, 2013 13:49
-
-
Save StanKocken/5788206 to your computer and use it in GitHub Desktop.
An evolution of the basic "Log" of Android. This one will show the log, whatever the level, only if you are in debug. If your log is too long (like an answer of a webservice) it will be split in different log.
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 (C) 2013 Stan Kocken (http://www.stankocken.com) | |
* | |
* 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.skocken.gist; | |
import java.io.PrintWriter; | |
import java.io.StringWriter; | |
import java.net.UnknownHostException; | |
import java.util.Locale; | |
import android.util.Log; | |
public class LogP { | |
/** Used locally to tag Logs */ | |
@SuppressWarnings("unused") | |
private static final String TAG = Log.class.getSimpleName(); | |
/** @hide */ | |
private static final int LOG_ID_MAIN = 0; | |
private static final int MAX_LENGHT_LOG = 3800; | |
/** | |
* Send a {@link #VERBOSE} log message. | |
* | |
* @param tag | |
* Used to identify the source of a log message. It usually identifies | |
* the class or activity where the log call occurs. | |
* @param msg | |
* The message you would like logged. | |
* @param args | |
* the list of arguments passed to the formatter. If there are | |
* more arguments than required by {@code format}, | |
* additional arguments are ignored. | |
*/ | |
public static int v(String tag, String msg, Object... args) { | |
return println_native(LOG_ID_MAIN, Log.VERBOSE, tag, msg, args); | |
} | |
/** | |
* Send a {@link #VERBOSE} log message and log the exception. | |
* | |
* @param tag | |
* Used to identify the source of a log message. It usually identifies | |
* the class or activity where the log call occurs. | |
* @param msg | |
* The message you would like logged. | |
* @param tr | |
* An exception to log | |
* @param args | |
* the list of arguments passed to the formatter. If there are | |
* more arguments than required by {@code format}, | |
* additional arguments are ignored. | |
*/ | |
public static int v(String tag, String msg, Throwable tr, Object... args) { | |
return println_native(LOG_ID_MAIN, Log.VERBOSE, tag, msg + '\n' + getStackTraceString(tr), args); | |
} | |
/** | |
* Send a {@link #DEBUG} log message. | |
* | |
* @param tag | |
* Used to identify the source of a log message. It usually identifies | |
* the class or activity where the log call occurs. | |
* @param msg | |
* The message you would like logged. | |
* @param args | |
* the list of arguments passed to the formatter. If there are | |
* more arguments than required by {@code format}, | |
* additional arguments are ignored. | |
*/ | |
public static int d(String tag, String msg, Object... args) { | |
return println_native(LOG_ID_MAIN, Log.DEBUG, tag, msg, args); | |
} | |
/** | |
* Send a {@link #DEBUG} log message and log the exception. | |
* | |
* @param tag | |
* Used to identify the source of a log message. It usually identifies | |
* the class or activity where the log call occurs. | |
* @param msg | |
* The message you would like logged. | |
* @param tr | |
* An exception to log | |
* @param args | |
* the list of arguments passed to the formatter. If there are | |
* more arguments than required by {@code format}, | |
* additional arguments are ignored. | |
*/ | |
public static int d(String tag, String msg, Throwable tr, Object... args) { | |
return println_native(LOG_ID_MAIN, Log.DEBUG, tag, msg + '\n' + getStackTraceString(tr), args); | |
} | |
/** | |
* Send an {@link #INFO} log message. | |
* | |
* @param tag | |
* Used to identify the source of a log message. It usually identifies | |
* the class or activity where the log call occurs. | |
* @param msg | |
* The message you would like logged. | |
* @param args | |
* the list of arguments passed to the formatter. If there are | |
* more arguments than required by {@code format}, | |
* additional arguments are ignored. | |
*/ | |
public static int i(String tag, String msg, Object... args) { | |
return println_native(LOG_ID_MAIN, Log.INFO, tag, msg, args); | |
} | |
/** | |
* Send a {@link #INFO} log message and log the exception. | |
* | |
* @param tag | |
* Used to identify the source of a log message. It usually identifies | |
* the class or activity where the log call occurs. | |
* @param msg | |
* The message you would like logged. | |
* @param tr | |
* An exception to log | |
* @param args | |
* the list of arguments passed to the formatter. If there are | |
* more arguments than required by {@code format}, | |
* additional arguments are ignored. | |
*/ | |
public static int i(String tag, String msg, Throwable tr, Object... args) { | |
return println_native(LOG_ID_MAIN, Log.INFO, tag, msg + '\n' + getStackTraceString(tr), args); | |
} | |
/** | |
* Send a {@link #WARN} log message. | |
* | |
* @param tag | |
* Used to identify the source of a log message. It usually identifies | |
* the class or activity where the log call occurs. | |
* @param msg | |
* The message you would like logged. | |
* @param args | |
* the list of arguments passed to the formatter. If there are | |
* more arguments than required by {@code format}, | |
* additional arguments are ignored. | |
*/ | |
public static int w(String tag, String msg, Object... args) { | |
return println_native(LOG_ID_MAIN, Log.WARN, tag, msg, args); | |
} | |
/** | |
* Send a {@link #WARN} log message and log the exception. | |
* | |
* @param tag | |
* Used to identify the source of a log message. It usually identifies | |
* the class or activity where the log call occurs. | |
* @param msg | |
* The message you would like logged. | |
* @param tr | |
* An exception to log | |
* @param args | |
* the list of arguments passed to the formatter. If there are | |
* more arguments than required by {@code format}, | |
* additional arguments are ignored. | |
*/ | |
public static int w(String tag, String msg, Throwable tr, Object... args) { | |
return println_native(LOG_ID_MAIN, Log.WARN, tag, msg + '\n' + getStackTraceString(tr), args); | |
} | |
/* | |
* Send a {@link #WARN} log message and log the exception. | |
* | |
* @param tag Used to identify the source of a log message. It usually identifies | |
* the class or activity where the log call occurs. | |
* | |
* @param tr An exception to log | |
*/ | |
public static int w(String tag, Throwable tr) { | |
return println_native(LOG_ID_MAIN, Log.WARN, tag, getStackTraceString(tr)); | |
} | |
/** | |
* Send an {@link #ERROR} log message. | |
* | |
* @param tag | |
* Used to identify the source of a log message. It usually identifies | |
* the class or activity where the log call occurs. | |
* @param msg | |
* The message you would like logged. | |
* @param args | |
* the list of arguments passed to the formatter. If there are | |
* more arguments than required by {@code format}, | |
* additional arguments are ignored. | |
*/ | |
public static int e(String tag, String msg, Object... args) { | |
return println_native(LOG_ID_MAIN, Log.ERROR, tag, msg, args); | |
} | |
/** | |
* Send a {@link #ERROR} log message and log the exception. | |
* | |
* @param tag | |
* Used to identify the source of a log message. It usually identifies | |
* the class or activity where the log call occurs. | |
* @param msg | |
* The message you would like logged. | |
* @param tr | |
* An exception to log | |
* @param args | |
* the list of arguments passed to the formatter. If there are | |
* more arguments than required by {@code format}, | |
* additional arguments are ignored. | |
*/ | |
public static int e(String tag, String msg, Throwable tr, Object... args) { | |
return println_native(LOG_ID_MAIN, Log.ERROR, tag, msg + '\n' + getStackTraceString(tr), args); | |
} | |
/** | |
* Handy function to get a loggable stack trace from a Throwable | |
* | |
* @param tr | |
* An exception to log | |
*/ | |
public static String getStackTraceString(Throwable tr) { | |
if (tr == null) { | |
return ""; | |
} | |
// This is to reduce the amount of log spew that apps do in the non-error | |
// condition of the network being unavailable. | |
Throwable t = tr; | |
while (t != null) { | |
if (t instanceof UnknownHostException) { | |
return ""; | |
} | |
t = t.getCause(); | |
} | |
StringWriter sw = new StringWriter(); | |
PrintWriter pw = new PrintWriter(sw); | |
tr.printStackTrace(pw); | |
return sw.toString(); | |
} | |
/** | |
* Low-level logging call. | |
* | |
* @param priority | |
* The priority/type of this log message | |
* @param tag | |
* Used to identify the source of a log message. It usually identifies | |
* the class or activity where the log call occurs. | |
* @param msg | |
* The message you would like logged. | |
* @param args | |
* the list of arguments passed to the formatter. If there are | |
* more arguments than required by {@code format}, | |
* additional arguments are ignored. | |
* @return The number of bytes written. | |
*/ | |
public static int println(int priority, String tag, String msg, Object... args) { | |
return println_native(LOG_ID_MAIN, priority, tag, msg, args); | |
} | |
/** | |
* Low-level logging call. | |
* | |
* @param priority | |
* The priority/type of this log message | |
* @param tag | |
* Used to identify the source of a log message. It usually identifies | |
* the class or activity where the log call occurs. | |
* @param msg | |
* The message you would like logged. | |
* @param args | |
* the list of arguments passed to the formatter. If there are | |
* more arguments than required by {@code format}, | |
* additional arguments are ignored. | |
* @return The number of bytes written. | |
*/ | |
public static int println_native(int bufID, int priority, String tag, String msg, Object... args) { | |
if (BuildConfig.DEBUG) { | |
if (args != null && args.length > 0) { | |
msg = String.format(Locale.US, msg, args); | |
} | |
if (msg.length() > MAX_LENGHT_LOG) { | |
// we need to split this log | |
int nbBytesWrite = 0; | |
int msgLength = msg.length(); | |
for (int i = 0; i < (msgLength / MAX_LENGHT_LOG) + 1; i++) { | |
nbBytesWrite += Log.println(priority, tag, msg.substring(i * MAX_LENGHT_LOG, Math.min(msgLength, (i + 1) * MAX_LENGHT_LOG))); | |
} | |
return nbBytesWrite; | |
} else { | |
return Log.println(priority, tag, msg); | |
} | |
} else { | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment