2012-02-15 22:05:52 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-24 13:17:46 -07:00
|
|
|
* 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/. */
|
2012-02-15 22:05:52 -08:00
|
|
|
|
|
|
|
package org.mozilla.gecko.sync;
|
|
|
|
|
2012-06-12 12:12:43 -07:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import org.mozilla.gecko.sync.log.writers.AndroidLevelCachingLogWriter;
|
|
|
|
import org.mozilla.gecko.sync.log.writers.AndroidLogWriter;
|
|
|
|
import org.mozilla.gecko.sync.log.writers.LogWriter;
|
2012-10-17 00:28:29 -07:00
|
|
|
import org.mozilla.gecko.sync.log.writers.SimpleTagLogWriter;
|
|
|
|
import org.mozilla.gecko.sync.log.writers.ThreadLocalTagLogWriter;
|
2012-02-15 22:05:52 -08:00
|
|
|
|
2012-02-15 22:05:52 -08:00
|
|
|
import android.util.Log;
|
|
|
|
|
2012-02-15 22:05:52 -08:00
|
|
|
/**
|
2012-06-12 12:12:43 -07:00
|
|
|
* Logging helper class. Serializes all log operations (by synchronizing).
|
2012-02-15 22:05:52 -08:00
|
|
|
*/
|
2012-02-15 22:05:52 -08:00
|
|
|
public class Logger {
|
2012-10-17 00:28:29 -07:00
|
|
|
public static final String LOGGER_TAG = "Logger";
|
|
|
|
public static final String DEFAULT_LOG_TAG = "GeckoLogger";
|
2012-02-15 22:05:52 -08:00
|
|
|
|
|
|
|
// For extra debugging.
|
|
|
|
public static boolean LOG_PERSONAL_INFORMATION = false;
|
|
|
|
|
2012-06-12 12:12:43 -07:00
|
|
|
/**
|
2012-10-17 00:28:29 -07:00
|
|
|
* Allow each thread to use its own global log tag. This allows
|
|
|
|
* independent services to log as different sources.
|
|
|
|
*
|
|
|
|
* When your thread sets up logging, it should do something like the following:
|
|
|
|
*
|
|
|
|
* Logger.setThreadLogTag("MyTag");
|
|
|
|
*
|
|
|
|
* The value is inheritable, so worker threads and such do not need to
|
|
|
|
* set the same log tag as their parent.
|
|
|
|
*/
|
|
|
|
private static final InheritableThreadLocal<String> logTag = new InheritableThreadLocal<String>() {
|
|
|
|
@Override
|
|
|
|
protected String initialValue() {
|
|
|
|
return DEFAULT_LOG_TAG;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public static void setThreadLogTag(final String logTag) {
|
|
|
|
Logger.logTag.set(logTag);
|
|
|
|
}
|
|
|
|
public static String getThreadLogTag() {
|
|
|
|
return Logger.logTag.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current set of writers to which we will log.
|
2012-06-12 12:12:43 -07:00
|
|
|
* <p>
|
2012-10-17 00:28:29 -07:00
|
|
|
* We want logging to be available while running tests, so we initialize
|
2012-06-12 12:12:43 -07:00
|
|
|
* this set statically.
|
|
|
|
*/
|
2012-10-17 00:28:29 -07:00
|
|
|
protected final static Set<LogWriter> logWriters;
|
|
|
|
static {
|
|
|
|
final Set<LogWriter> defaultWriters = Logger.defaultLogWriters();
|
|
|
|
logWriters = new LinkedHashSet<LogWriter>(defaultWriters);
|
|
|
|
}
|
2012-02-15 22:05:52 -08:00
|
|
|
|
|
|
|
/**
|
2012-06-12 12:12:43 -07:00
|
|
|
* Default set of log writers to log to.
|
2012-02-15 22:05:52 -08:00
|
|
|
*/
|
2012-10-17 00:28:29 -07:00
|
|
|
public final static Set<LogWriter> defaultLogWriters() {
|
2012-07-26 08:47:25 -07:00
|
|
|
final String processedPackage = GlobalConstants.BROWSER_INTENT_PACKAGE.replace("org.mozilla.", "");
|
2012-10-17 00:28:29 -07:00
|
|
|
|
|
|
|
final Set<LogWriter> defaultLogWriters = new LinkedHashSet<LogWriter>();
|
|
|
|
|
|
|
|
final LogWriter log = new AndroidLogWriter();
|
|
|
|
final LogWriter cache = new AndroidLevelCachingLogWriter(log);
|
|
|
|
|
|
|
|
final LogWriter single = new SimpleTagLogWriter(processedPackage, new ThreadLocalTagLogWriter(Logger.logTag, cache));
|
|
|
|
|
2012-06-12 12:12:43 -07:00
|
|
|
defaultLogWriters.add(single);
|
|
|
|
return defaultLogWriters;
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
|
2012-06-12 12:12:43 -07:00
|
|
|
public static synchronized void startLoggingTo(LogWriter logWriter) {
|
|
|
|
logWriters.add(logWriter);
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
|
2012-10-17 00:28:29 -07:00
|
|
|
public static synchronized void startLoggingToWriters(Set<LogWriter> writers) {
|
|
|
|
logWriters.addAll(writers);
|
|
|
|
}
|
|
|
|
|
2012-06-12 12:12:43 -07:00
|
|
|
public static synchronized void stopLoggingTo(LogWriter logWriter) {
|
|
|
|
try {
|
|
|
|
logWriter.close();
|
|
|
|
} catch (Exception e) {
|
2012-10-17 00:28:29 -07:00
|
|
|
Log.e(LOGGER_TAG, "Got exception closing and removing LogWriter " + logWriter + ".", e);
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
2012-06-12 12:12:43 -07:00
|
|
|
logWriters.remove(logWriter);
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
|
2012-06-12 12:12:43 -07:00
|
|
|
public static synchronized void stopLoggingToAll() {
|
|
|
|
for (LogWriter logWriter : logWriters) {
|
|
|
|
try {
|
|
|
|
logWriter.close();
|
|
|
|
} catch (Exception e) {
|
2012-10-17 00:28:29 -07:00
|
|
|
Log.e(LOGGER_TAG, "Got exception closing and removing LogWriter " + logWriter + ".", e);
|
2012-06-12 12:12:43 -07:00
|
|
|
}
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
2012-06-12 12:12:43 -07:00
|
|
|
logWriters.clear();
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
|
2012-06-12 12:12:43 -07:00
|
|
|
/**
|
|
|
|
* Write to only the default log writers.
|
|
|
|
*/
|
|
|
|
public static synchronized void resetLogging() {
|
|
|
|
stopLoggingToAll();
|
|
|
|
logWriters.addAll(Logger.defaultLogWriters());
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Synchronized version for other classes to use.
|
2012-06-12 12:12:43 -07:00
|
|
|
public static synchronized boolean shouldLogVerbose(String logTag) {
|
|
|
|
for (LogWriter logWriter : logWriters) {
|
|
|
|
if (logWriter.shouldLogVerbose(logTag)) {
|
|
|
|
return true;
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
}
|
2012-06-12 12:12:43 -07:00
|
|
|
return false;
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
|
2012-10-17 00:28:29 -07:00
|
|
|
public static void error(String tag, String message) {
|
|
|
|
Logger.error(tag, message, null);
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
|
2012-10-17 00:28:29 -07:00
|
|
|
public static void warn(String tag, String message) {
|
|
|
|
Logger.warn(tag, message, null);
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
|
2012-10-17 00:28:29 -07:00
|
|
|
public static void info(String tag, String message) {
|
|
|
|
Logger.info(tag, message, null);
|
2012-06-12 12:12:43 -07:00
|
|
|
}
|
|
|
|
|
2012-10-17 00:28:29 -07:00
|
|
|
public static void debug(String tag, String message) {
|
|
|
|
Logger.debug(tag, message, null);
|
2012-06-12 12:12:43 -07:00
|
|
|
}
|
|
|
|
|
2012-10-17 00:28:29 -07:00
|
|
|
public static void trace(String tag, String message) {
|
|
|
|
Logger.trace(tag, message, null);
|
2012-06-12 12:12:43 -07:00
|
|
|
}
|
|
|
|
|
2012-10-17 00:28:29 -07:00
|
|
|
public static void pii(String tag, String message) {
|
2012-06-12 12:12:43 -07:00
|
|
|
if (LOG_PERSONAL_INFORMATION) {
|
2012-10-17 00:28:29 -07:00
|
|
|
Logger.debug(tag, "$$PII$$: " + message);
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-17 00:28:29 -07:00
|
|
|
public static synchronized void error(String tag, String message, Throwable error) {
|
2012-06-12 12:12:43 -07:00
|
|
|
Iterator<LogWriter> it = logWriters.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
LogWriter writer = it.next();
|
|
|
|
try {
|
2012-10-17 00:28:29 -07:00
|
|
|
writer.error(tag, message, error);
|
2012-06-12 12:12:43 -07:00
|
|
|
} catch (Exception e) {
|
2012-10-17 00:28:29 -07:00
|
|
|
Log.e(LOGGER_TAG, "Got exception logging; removing LogWriter " + writer + ".", e);
|
2012-06-12 12:12:43 -07:00
|
|
|
it.remove();
|
|
|
|
}
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-17 00:28:29 -07:00
|
|
|
public static synchronized void warn(String tag, String message, Throwable error) {
|
2012-06-12 12:12:43 -07:00
|
|
|
Iterator<LogWriter> it = logWriters.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
LogWriter writer = it.next();
|
|
|
|
try {
|
2012-10-17 00:28:29 -07:00
|
|
|
writer.warn(tag, message, error);
|
2012-06-12 12:12:43 -07:00
|
|
|
} catch (Exception e) {
|
2012-10-17 00:28:29 -07:00
|
|
|
Log.e(LOGGER_TAG, "Got exception logging; removing LogWriter " + writer + ".", e);
|
2012-06-12 12:12:43 -07:00
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
|
2012-10-17 00:28:29 -07:00
|
|
|
public static synchronized void info(String tag, String message, Throwable error) {
|
2012-06-12 12:12:43 -07:00
|
|
|
Iterator<LogWriter> it = logWriters.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
LogWriter writer = it.next();
|
|
|
|
try {
|
2012-10-17 00:28:29 -07:00
|
|
|
writer.info(tag, message, error);
|
2012-06-12 12:12:43 -07:00
|
|
|
} catch (Exception e) {
|
2012-10-17 00:28:29 -07:00
|
|
|
Log.e(LOGGER_TAG, "Got exception logging; removing LogWriter " + writer + ".", e);
|
2012-06-12 12:12:43 -07:00
|
|
|
it.remove();
|
|
|
|
}
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-17 00:28:29 -07:00
|
|
|
public static synchronized void debug(String tag, String message, Throwable error) {
|
2012-06-12 12:12:43 -07:00
|
|
|
Iterator<LogWriter> it = logWriters.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
LogWriter writer = it.next();
|
|
|
|
try {
|
2012-10-17 00:28:29 -07:00
|
|
|
writer.debug(tag, message, error);
|
2012-06-12 12:12:43 -07:00
|
|
|
} catch (Exception e) {
|
2012-10-17 00:28:29 -07:00
|
|
|
Log.e(LOGGER_TAG, "Got exception logging; removing LogWriter " + writer + ".", e);
|
2012-06-12 12:12:43 -07:00
|
|
|
it.remove();
|
|
|
|
}
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-17 00:28:29 -07:00
|
|
|
public static synchronized void trace(String tag, String message, Throwable error) {
|
2012-06-12 12:12:43 -07:00
|
|
|
Iterator<LogWriter> it = logWriters.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
LogWriter writer = it.next();
|
|
|
|
try {
|
2012-10-17 00:28:29 -07:00
|
|
|
writer.trace(tag, message, error);
|
2012-06-12 12:12:43 -07:00
|
|
|
} catch (Exception e) {
|
2012-10-17 00:28:29 -07:00
|
|
|
Log.e(LOGGER_TAG, "Got exception logging; removing LogWriter " + writer + ".", e);
|
2012-06-12 12:12:43 -07:00
|
|
|
it.remove();
|
|
|
|
}
|
2012-02-15 22:05:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|