2012-02-15 22:05:52 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko.sync;
|
|
|
|
|
2012-02-15 22:05:52 -08:00
|
|
|
import java.util.IdentityHashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2012-02-15 22:05:52 -08:00
|
|
|
import android.util.Log;
|
|
|
|
|
2012-02-15 22:05:52 -08:00
|
|
|
/**
|
|
|
|
* Logging helper class. Serializes all log operations (by synchronizing),
|
|
|
|
* and caches log level settings.
|
|
|
|
*
|
|
|
|
* Ultimately this will also be a hook point for our own logging system.
|
|
|
|
*
|
|
|
|
* @author rnewman
|
|
|
|
*
|
|
|
|
*/
|
2012-02-15 22:05:52 -08:00
|
|
|
public class Logger {
|
|
|
|
|
|
|
|
// For extra debugging.
|
|
|
|
public static boolean LOG_PERSONAL_INFORMATION = false;
|
|
|
|
|
|
|
|
// If true, log to System.out as well as using Android's Log.* calls.
|
|
|
|
public static boolean LOG_TO_STDOUT = false;
|
|
|
|
|
2012-02-15 22:05:52 -08:00
|
|
|
// I can't believe we have to implement this ourselves.
|
|
|
|
// These aren't synchronized (and neither are the setters) because
|
|
|
|
// the logging calls themselves are synchronized.
|
|
|
|
private static Map<String, Boolean> isErrorLoggable = new IdentityHashMap<String, Boolean>();
|
|
|
|
private static Map<String, Boolean> isWarnLoggable = new IdentityHashMap<String, Boolean>();
|
|
|
|
private static Map<String, Boolean> isInfoLoggable = new IdentityHashMap<String, Boolean>();
|
|
|
|
private static Map<String, Boolean> isDebugLoggable = new IdentityHashMap<String, Boolean>();
|
|
|
|
private static Map<String, Boolean> isVerboseLoggable = new IdentityHashMap<String, Boolean>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Empty the caches of log levels.
|
|
|
|
*/
|
|
|
|
public synchronized void refreshLogLevels() {
|
|
|
|
isErrorLoggable = new IdentityHashMap<String, Boolean>();
|
|
|
|
isWarnLoggable = new IdentityHashMap<String, Boolean>();
|
|
|
|
isInfoLoggable = new IdentityHashMap<String, Boolean>();
|
|
|
|
isDebugLoggable = new IdentityHashMap<String, Boolean>();
|
2012-02-15 22:05:53 -08:00
|
|
|
isVerboseLoggable = new IdentityHashMap<String, Boolean>();
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean shouldLogError(String logTag) {
|
|
|
|
Boolean out = isErrorLoggable.get(logTag);
|
|
|
|
if (out != null) {
|
|
|
|
return out.booleanValue();
|
|
|
|
}
|
|
|
|
out = Log.isLoggable(logTag, Log.ERROR);
|
|
|
|
isErrorLoggable.put(logTag, out);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean shouldLogWarn(String logTag) {
|
|
|
|
Boolean out = isWarnLoggable.get(logTag);
|
|
|
|
if (out != null) {
|
|
|
|
return out.booleanValue();
|
|
|
|
}
|
|
|
|
out = Log.isLoggable(logTag, Log.WARN);
|
|
|
|
isWarnLoggable.put(logTag, out);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean shouldLogInfo(String logTag) {
|
|
|
|
Boolean out = isInfoLoggable.get(logTag);
|
|
|
|
if (out != null) {
|
|
|
|
return out.booleanValue();
|
|
|
|
}
|
|
|
|
out = Log.isLoggable(logTag, Log.INFO);
|
|
|
|
isInfoLoggable.put(logTag, out);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean shouldLogDebug(String logTag) {
|
|
|
|
Boolean out = isDebugLoggable.get(logTag);
|
|
|
|
if (out != null) {
|
|
|
|
return out.booleanValue();
|
|
|
|
}
|
|
|
|
out = Log.isLoggable(logTag, Log.DEBUG);
|
|
|
|
isDebugLoggable.put(logTag, out);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean shouldLogVerbose(String logTag) {
|
|
|
|
Boolean out = isVerboseLoggable.get(logTag);
|
|
|
|
if (out != null) {
|
|
|
|
return out.booleanValue();
|
|
|
|
}
|
|
|
|
out = Log.isLoggable(logTag, Log.VERBOSE);
|
|
|
|
isVerboseLoggable.put(logTag, out);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Synchronized version for other classes to use.
|
|
|
|
public static synchronized boolean logVerbose(String logTag) {
|
|
|
|
return shouldLogVerbose(logTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void logToStdout(String... s) {
|
2012-02-15 22:05:52 -08:00
|
|
|
if (LOG_TO_STDOUT) {
|
|
|
|
for (String string : s) {
|
|
|
|
System.out.print(string);
|
|
|
|
}
|
|
|
|
System.out.println("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void error(String logTag, String message) {
|
|
|
|
Logger.error(logTag, message, null);
|
|
|
|
}
|
|
|
|
|
2012-02-15 22:05:52 -08:00
|
|
|
public static synchronized void error(String logTag, String message, Throwable error) {
|
2012-02-15 22:05:52 -08:00
|
|
|
logToStdout(logTag, " :: ERROR: ", message);
|
2012-02-15 22:05:52 -08:00
|
|
|
if (shouldLogError(logTag)) {
|
|
|
|
Log.e(logTag, message, error);
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void warn(String logTag, String message) {
|
|
|
|
Logger.warn(logTag, message, null);
|
|
|
|
}
|
|
|
|
|
2012-02-15 22:05:52 -08:00
|
|
|
public static synchronized void warn(String logTag, String message, Throwable error) {
|
2012-02-15 22:05:52 -08:00
|
|
|
logToStdout(logTag, " :: WARN: ", message);
|
2012-02-15 22:05:52 -08:00
|
|
|
if (shouldLogWarn(logTag)) {
|
|
|
|
Log.w(logTag, message, error);
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-15 22:05:52 -08:00
|
|
|
public static synchronized void info(String logTag, String message) {
|
2012-02-15 22:05:52 -08:00
|
|
|
logToStdout(logTag, " :: INFO: ", message);
|
2012-02-15 22:05:52 -08:00
|
|
|
if (shouldLogInfo(logTag)) {
|
|
|
|
Log.i(logTag, message);
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void debug(String logTag, String message) {
|
|
|
|
Logger.debug(logTag, message, null);
|
|
|
|
}
|
|
|
|
|
2012-02-15 22:05:52 -08:00
|
|
|
public static synchronized void debug(String logTag, String message, Throwable error) {
|
2012-02-15 22:05:52 -08:00
|
|
|
logToStdout(logTag, " :: DEBUG: ", message);
|
2012-02-15 22:05:52 -08:00
|
|
|
if (shouldLogDebug(logTag)) {
|
|
|
|
Log.d(logTag, message, error);
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-15 22:05:52 -08:00
|
|
|
public static synchronized void trace(String logTag, String message) {
|
2012-02-15 22:05:52 -08:00
|
|
|
logToStdout(logTag, " :: TRACE: ", message);
|
2012-02-15 22:05:52 -08:00
|
|
|
if (shouldLogVerbose(logTag)) {
|
|
|
|
Log.v(logTag, message);
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void pii(String logTag, String message) {
|
|
|
|
if (LOG_PERSONAL_INFORMATION) {
|
|
|
|
Logger.debug(logTag, "$$PII$$: " + message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|