mobile/android/base/background/common/log/Logger.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.background.common.log;
michael@0 6
michael@0 7 import java.io.PrintWriter;
michael@0 8 import java.util.Iterator;
michael@0 9 import java.util.LinkedHashSet;
michael@0 10 import java.util.Set;
michael@0 11
michael@0 12 import org.mozilla.gecko.background.common.GlobalConstants;
michael@0 13 import org.mozilla.gecko.background.common.log.writers.AndroidLevelCachingLogWriter;
michael@0 14 import org.mozilla.gecko.background.common.log.writers.AndroidLogWriter;
michael@0 15 import org.mozilla.gecko.background.common.log.writers.LogWriter;
michael@0 16 import org.mozilla.gecko.background.common.log.writers.PrintLogWriter;
michael@0 17 import org.mozilla.gecko.background.common.log.writers.SimpleTagLogWriter;
michael@0 18 import org.mozilla.gecko.background.common.log.writers.ThreadLocalTagLogWriter;
michael@0 19
michael@0 20 import android.util.Log;
michael@0 21
michael@0 22 /**
michael@0 23 * Logging helper class. Serializes all log operations (by synchronizing).
michael@0 24 */
michael@0 25 public class Logger {
michael@0 26 public static final String LOGGER_TAG = "Logger";
michael@0 27 public static final String DEFAULT_LOG_TAG = "GeckoLogger";
michael@0 28
michael@0 29 // For extra debugging.
michael@0 30 public static boolean LOG_PERSONAL_INFORMATION = false;
michael@0 31
michael@0 32 /**
michael@0 33 * Allow each thread to use its own global log tag. This allows
michael@0 34 * independent services to log as different sources.
michael@0 35 *
michael@0 36 * When your thread sets up logging, it should do something like the following:
michael@0 37 *
michael@0 38 * Logger.setThreadLogTag("MyTag");
michael@0 39 *
michael@0 40 * The value is inheritable, so worker threads and such do not need to
michael@0 41 * set the same log tag as their parent.
michael@0 42 */
michael@0 43 private static final InheritableThreadLocal<String> logTag = new InheritableThreadLocal<String>() {
michael@0 44 @Override
michael@0 45 protected String initialValue() {
michael@0 46 return DEFAULT_LOG_TAG;
michael@0 47 }
michael@0 48 };
michael@0 49
michael@0 50 public static void setThreadLogTag(final String logTag) {
michael@0 51 Logger.logTag.set(logTag);
michael@0 52 }
michael@0 53 public static String getThreadLogTag() {
michael@0 54 return Logger.logTag.get();
michael@0 55 }
michael@0 56
michael@0 57 /**
michael@0 58 * Current set of writers to which we will log.
michael@0 59 * <p>
michael@0 60 * We want logging to be available while running tests, so we initialize
michael@0 61 * this set statically.
michael@0 62 */
michael@0 63 protected final static Set<LogWriter> logWriters;
michael@0 64 static {
michael@0 65 final Set<LogWriter> defaultWriters = Logger.defaultLogWriters();
michael@0 66 logWriters = new LinkedHashSet<LogWriter>(defaultWriters);
michael@0 67 }
michael@0 68
michael@0 69 /**
michael@0 70 * Default set of log writers to log to.
michael@0 71 */
michael@0 72 public final static Set<LogWriter> defaultLogWriters() {
michael@0 73 final String processedPackage = GlobalConstants.BROWSER_INTENT_PACKAGE.replace("org.mozilla.", "");
michael@0 74
michael@0 75 final Set<LogWriter> defaultLogWriters = new LinkedHashSet<LogWriter>();
michael@0 76
michael@0 77 final LogWriter log = new AndroidLogWriter();
michael@0 78 final LogWriter cache = new AndroidLevelCachingLogWriter(log);
michael@0 79
michael@0 80 final LogWriter single = new SimpleTagLogWriter(processedPackage, new ThreadLocalTagLogWriter(Logger.logTag, cache));
michael@0 81
michael@0 82 defaultLogWriters.add(single);
michael@0 83 return defaultLogWriters;
michael@0 84 }
michael@0 85
michael@0 86 public static synchronized void startLoggingTo(LogWriter logWriter) {
michael@0 87 logWriters.add(logWriter);
michael@0 88 }
michael@0 89
michael@0 90 public static synchronized void startLoggingToWriters(Set<LogWriter> writers) {
michael@0 91 logWriters.addAll(writers);
michael@0 92 }
michael@0 93
michael@0 94 public static synchronized void stopLoggingTo(LogWriter logWriter) {
michael@0 95 try {
michael@0 96 logWriter.close();
michael@0 97 } catch (Exception e) {
michael@0 98 Log.e(LOGGER_TAG, "Got exception closing and removing LogWriter " + logWriter + ".", e);
michael@0 99 }
michael@0 100 logWriters.remove(logWriter);
michael@0 101 }
michael@0 102
michael@0 103 public static synchronized void stopLoggingToAll() {
michael@0 104 for (LogWriter logWriter : logWriters) {
michael@0 105 try {
michael@0 106 logWriter.close();
michael@0 107 } catch (Exception e) {
michael@0 108 Log.e(LOGGER_TAG, "Got exception closing and removing LogWriter " + logWriter + ".", e);
michael@0 109 }
michael@0 110 }
michael@0 111 logWriters.clear();
michael@0 112 }
michael@0 113
michael@0 114 /**
michael@0 115 * Write to only the default log writers.
michael@0 116 */
michael@0 117 public static synchronized void resetLogging() {
michael@0 118 stopLoggingToAll();
michael@0 119 logWriters.addAll(Logger.defaultLogWriters());
michael@0 120 }
michael@0 121
michael@0 122 /**
michael@0 123 * Start writing log output to stdout.
michael@0 124 * <p>
michael@0 125 * Use <code>resetLogging</code> to stop logging to stdout.
michael@0 126 */
michael@0 127 public static synchronized void startLoggingToConsole() {
michael@0 128 setThreadLogTag("Test");
michael@0 129 startLoggingTo(new PrintLogWriter(new PrintWriter(System.out, true)));
michael@0 130 }
michael@0 131
michael@0 132 // Synchronized version for other classes to use.
michael@0 133 public static synchronized boolean shouldLogVerbose(String logTag) {
michael@0 134 for (LogWriter logWriter : logWriters) {
michael@0 135 if (logWriter.shouldLogVerbose(logTag)) {
michael@0 136 return true;
michael@0 137 }
michael@0 138 }
michael@0 139 return false;
michael@0 140 }
michael@0 141
michael@0 142 public static void error(String tag, String message) {
michael@0 143 Logger.error(tag, message, null);
michael@0 144 }
michael@0 145
michael@0 146 public static void warn(String tag, String message) {
michael@0 147 Logger.warn(tag, message, null);
michael@0 148 }
michael@0 149
michael@0 150 public static void info(String tag, String message) {
michael@0 151 Logger.info(tag, message, null);
michael@0 152 }
michael@0 153
michael@0 154 public static void debug(String tag, String message) {
michael@0 155 Logger.debug(tag, message, null);
michael@0 156 }
michael@0 157
michael@0 158 public static void trace(String tag, String message) {
michael@0 159 Logger.trace(tag, message, null);
michael@0 160 }
michael@0 161
michael@0 162 public static void pii(String tag, String message) {
michael@0 163 if (LOG_PERSONAL_INFORMATION) {
michael@0 164 Logger.debug(tag, "$$PII$$: " + message);
michael@0 165 }
michael@0 166 }
michael@0 167
michael@0 168 public static synchronized void error(String tag, String message, Throwable error) {
michael@0 169 Iterator<LogWriter> it = logWriters.iterator();
michael@0 170 while (it.hasNext()) {
michael@0 171 LogWriter writer = it.next();
michael@0 172 try {
michael@0 173 writer.error(tag, message, error);
michael@0 174 } catch (Exception e) {
michael@0 175 Log.e(LOGGER_TAG, "Got exception logging; removing LogWriter " + writer + ".", e);
michael@0 176 it.remove();
michael@0 177 }
michael@0 178 }
michael@0 179 }
michael@0 180
michael@0 181 public static synchronized void warn(String tag, String message, Throwable error) {
michael@0 182 Iterator<LogWriter> it = logWriters.iterator();
michael@0 183 while (it.hasNext()) {
michael@0 184 LogWriter writer = it.next();
michael@0 185 try {
michael@0 186 writer.warn(tag, message, error);
michael@0 187 } catch (Exception e) {
michael@0 188 Log.e(LOGGER_TAG, "Got exception logging; removing LogWriter " + writer + ".", e);
michael@0 189 it.remove();
michael@0 190 }
michael@0 191 }
michael@0 192 }
michael@0 193
michael@0 194 public static synchronized void info(String tag, String message, Throwable error) {
michael@0 195 Iterator<LogWriter> it = logWriters.iterator();
michael@0 196 while (it.hasNext()) {
michael@0 197 LogWriter writer = it.next();
michael@0 198 try {
michael@0 199 writer.info(tag, message, error);
michael@0 200 } catch (Exception e) {
michael@0 201 Log.e(LOGGER_TAG, "Got exception logging; removing LogWriter " + writer + ".", e);
michael@0 202 it.remove();
michael@0 203 }
michael@0 204 }
michael@0 205 }
michael@0 206
michael@0 207 public static synchronized void debug(String tag, String message, Throwable error) {
michael@0 208 Iterator<LogWriter> it = logWriters.iterator();
michael@0 209 while (it.hasNext()) {
michael@0 210 LogWriter writer = it.next();
michael@0 211 try {
michael@0 212 writer.debug(tag, message, error);
michael@0 213 } catch (Exception e) {
michael@0 214 Log.e(LOGGER_TAG, "Got exception logging; removing LogWriter " + writer + ".", e);
michael@0 215 it.remove();
michael@0 216 }
michael@0 217 }
michael@0 218 }
michael@0 219
michael@0 220 public static synchronized void trace(String tag, String message, Throwable error) {
michael@0 221 Iterator<LogWriter> it = logWriters.iterator();
michael@0 222 while (it.hasNext()) {
michael@0 223 LogWriter writer = it.next();
michael@0 224 try {
michael@0 225 writer.trace(tag, message, error);
michael@0 226 } catch (Exception e) {
michael@0 227 Log.e(LOGGER_TAG, "Got exception logging; removing LogWriter " + writer + ".", e);
michael@0 228 it.remove();
michael@0 229 }
michael@0 230 }
michael@0 231 }
michael@0 232 }

mercurial