-
-
Save gab-aquino/3865311 to your computer and use it in GitHub Desktop.
Оverride class android.util.Log
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
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import travel.opas.core.Consts; | |
import android.os.Handler; | |
import android.os.HandlerThread; | |
import android.os.Process; | |
import android.text.format.DateFormat; | |
/** | |
* Wrapper class for android logging | |
* | |
* @author e.shishkin | |
* | |
*/ | |
public class Log { | |
private static final String PATH_LOG_FOLDER = Consts.PATH_FILES_FOLDER + "/log/"; | |
private static boolean logToFile; | |
private static int logLevel; | |
public static final int ERRORS_ONLY = 1; | |
public static final int ERRORS_WARNINGS = 2; | |
public static final int ERRORS_WARNINGS_INFO = 3; | |
public static final int ERRORS_WARNINGS_INFO_DEBUG = 4; | |
public static final int ERRORS_WARNINGS_INFO_DEBUG_VERBOSE = 5; | |
private static boolean log_e, log_w, log_i, log_d, log_v; | |
private static HandlerThread logToFileThread; | |
private static Handler mHandler; | |
static { | |
logLevel = ERRORS_WARNINGS_INFO_DEBUG_VERBOSE; | |
init(); | |
} | |
/** | |
* 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. | |
*/ | |
public static void v(String tag, String string) { | |
if (log_v) | |
android.util.Log.v(tag, string); | |
if (logToFile) | |
asyncWriteToFile("V", tag, string); | |
} | |
/** | |
* 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 format the format string | |
* @param args the list of arguments passed to the formatter. | |
* If there are more arguments than required by format, additional arguments are ignored. | |
*/ | |
public static void v(String tag, String format, Object... args) { | |
if (log_v) | |
android.util.Log.v(tag, String.format(format, args)); | |
if (logToFile) | |
asyncWriteToFile("V", tag, String.format(format, 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. | |
*/ | |
public static void d(String tag, String string) { | |
if (log_d) | |
android.util.Log.d(tag, string); | |
if (logToFile) | |
asyncWriteToFile("D", tag, string); | |
} | |
/** | |
* 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 format the format string | |
* @param args the list of arguments passed to the formatter. | |
* If there are more arguments than required by format, additional arguments are ignored. | |
*/ | |
public static void d(String tag, String format, Object... args) { | |
if (log_d) | |
android.util.Log.d(tag, String.format(format, args)); | |
if (logToFile) | |
asyncWriteToFile("D", tag, String.format(format, 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. | |
*/ | |
public static void i(String tag, String string) { | |
if (log_i) | |
android.util.Log.i(tag, string); | |
if (logToFile) | |
asyncWriteToFile("I", tag, string); | |
} | |
/** | |
* Send a {@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 format the format string | |
* @param args the list of arguments passed to the formatter. | |
* If there are more arguments than required by format, additional arguments are ignored. | |
*/ | |
public static void i(String tag, String format, Object... args) { | |
if (log_i) | |
android.util.Log.i(tag, String.format(format, args)); | |
if (logToFile) | |
asyncWriteToFile("I", tag, String.format(format, 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. | |
*/ | |
public static void w(String tag, String string) { | |
if (log_w) | |
android.util.Log.w(tag, string); | |
if (logToFile) | |
asyncWriteToFile("W", tag, string); | |
} | |
/** | |
* 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 format the format string | |
* @param args the list of arguments passed to the formatter. | |
* If there are more arguments than required by format, additional arguments are ignored. | |
*/ | |
public static void w(String tag, String format, Object... args) { | |
if (log_w) | |
android.util.Log.w(tag, String.format(format, args)); | |
if (logToFile) | |
asyncWriteToFile("W", tag, String.format(format, 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 void w(String tag, Throwable e) { | |
if (log_w) | |
android.util.Log.w(tag, e.getMessage(), e); | |
if (logToFile) | |
asyncWriteToFile("W", tag, String.format("%s\n%s", e.getMessage(), throwableToString(e))); | |
} | |
/** | |
* 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 | |
*/ | |
public static void w(String tag, String message, Throwable e) { | |
if (log_w) | |
android.util.Log.w(tag, message, e); | |
if (logToFile) | |
asyncWriteToFile("W", tag, String.format("%s\n%s", message, throwableToString(e))); | |
} | |
/** | |
* 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. | |
*/ | |
public static void e(String tag, String string) { | |
if (log_e) | |
android.util.Log.e(tag, string); | |
if (logToFile) | |
asyncWriteToFile("E", tag, string); | |
} | |
/** | |
* Send a {@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 format the format string | |
* @param args the list of arguments passed to the formatter. | |
* If there are more arguments than required by format, additional arguments are ignored. | |
*/ | |
public static void e(String tag, String format, Object... args) { | |
if (log_e) | |
android.util.Log.e(tag, String.format(format, args)); | |
if (logToFile) | |
asyncWriteToFile("E", tag, String.format(format, 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 tr An exception to log | |
*/ | |
public static void e(String tag, Throwable e) { | |
if (log_e) | |
android.util.Log.e(tag, e.getMessage(), e); | |
if (logToFile) | |
asyncWriteToFile("E", tag, String.format("%s\n%s", e.getMessage(), throwableToString(e))); | |
} | |
/** | |
* 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 | |
*/ | |
public static void e(String tag, String message, Throwable e) { | |
if (log_e) { | |
android.util.Log.e(tag, message, e); | |
} | |
if (logToFile) | |
asyncWriteToFile("E", tag, String.format("%s\n%s", message, throwableToString(e))); | |
} | |
/** | |
* Handy function to get a loggable stack trace from a Throwable. | |
* @param e An exception to log | |
* @return stack trace string | |
*/ | |
public static String throwableToString(Throwable e) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append(e.getMessage()); | |
for (StackTraceElement element : e.getStackTrace()) { | |
sb.append("\n"); | |
sb.append(element.toString()); | |
} | |
sb.append("\n"); | |
return sb.toString(); | |
} | |
/** | |
* Setup flag logToFile. | |
* @param logging | |
*/ | |
public static void setLogToFile(boolean logging) { | |
logToFile = logging; | |
if (logging) { | |
initLogToFileThread(); | |
} else if (logToFileThread != null) { | |
destroyLogToFileThread(); | |
} | |
} | |
/** | |
* Checks to see whether or not a log in file. | |
* @return Whether or not that this is allowed to be logged in file. | |
*/ | |
public static boolean isLogToFile() { | |
return logToFile; | |
} | |
/** | |
* Getting path of recently Log file. | |
* @return path | |
*/ | |
public static String getRecentLogFilePath() { | |
return PATH_LOG_FOLDER + DateFormat.format("yyyy-MM-dd", new java.util.Date()); | |
} | |
/** | |
* Setup Log level. | |
* @param level | |
*/ | |
public static void setLogLevel(int level) { | |
logLevel = level; | |
init(); | |
} | |
/** | |
* initialize logging. | |
*/ | |
private static void init() { | |
log_e = logLevel >= ERRORS_ONLY; | |
log_w = logLevel >= ERRORS_WARNINGS; | |
log_i = logLevel >= ERRORS_WARNINGS_INFO; | |
log_d = logLevel >= ERRORS_WARNINGS_INFO_DEBUG; | |
log_v = logLevel >= ERRORS_WARNINGS_INFO_DEBUG_VERBOSE; | |
} | |
/** | |
* Release allocated resources. | |
*/ | |
public static void destroy() { | |
if (logToFile) { | |
destroyLogToFileThread(); | |
} | |
} | |
/** | |
* Initialization of handler thread for logging in file. | |
*/ | |
private static void initLogToFileThread() { | |
logToFileThread = new HandlerThread(Log.class.getSimpleName(), Process.THREAD_PRIORITY_BACKGROUND); | |
logToFileThread.start(); | |
mHandler = new Handler(logToFileThread.getLooper()); | |
} | |
/** | |
* Destroy of handler thread for logging in file. | |
*/ | |
private static void destroyLogToFileThread() { | |
logToFileThread.quit(); | |
logToFileThread = null; | |
mHandler = null; | |
} | |
/** | |
* Asynchronous writes to a specify file. | |
* @param level specify log level | |
* @param tag Used to identify the source of a log message. It usually identifies | |
* the class or activity where the log call occurs. | |
* @param string log message | |
*/ | |
private static void asyncWriteToFile(final String level, final String tag, final String string) { | |
mHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
writeToFile(level, tag, string); | |
} | |
}); | |
} | |
/** | |
* Write log to specify file. | |
* @param level specify log level | |
* @param tag Used to identify the source of a log message. It usually identifies | |
* the class or activity where the log call occurs. | |
* @param string log message | |
*/ | |
private static void writeToFile(String level, String tag, String string) { | |
File logFile = new File(PATH_LOG_FOLDER | |
+ android.text.format.DateFormat.format("yyyy-MM-dd", new java.util.Date())); | |
logFile.getParentFile().mkdirs(); | |
try { | |
FileWriter fstream = new FileWriter(logFile, true); | |
BufferedWriter out = new BufferedWriter(fstream); | |
out.write(String.format("%s %s/%s: %s\n", | |
android.text.format.DateFormat.format("yyyy-MM-dd hh:mm:ss", new java.util.Date()), level, tag, | |
string)); | |
out.close(); | |
} catch (IOException e) { | |
android.util.Log.w(Log.class.getSimpleName(), "Cannot write to log file", e); | |
} catch (OutOfMemoryError e) { | |
android.util.Log.w(Log.class.getSimpleName(), "Error writing to log file", e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment