1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/background/common/log/writers/AndroidLevelCachingLogWriter.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,126 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.background.common.log.writers; 1.9 + 1.10 +import java.util.IdentityHashMap; 1.11 +import java.util.Map; 1.12 + 1.13 +import android.util.Log; 1.14 + 1.15 +/** 1.16 + * Make a <code>LogWriter</code> only log when the Android log system says to. 1.17 + */ 1.18 +public class AndroidLevelCachingLogWriter extends LogWriter { 1.19 + protected final LogWriter inner; 1.20 + 1.21 + public AndroidLevelCachingLogWriter(LogWriter inner) { 1.22 + this.inner = inner; 1.23 + } 1.24 + 1.25 + // I can't believe we have to implement this ourselves. 1.26 + // These aren't synchronized (and neither are the setters) because 1.27 + // the logging calls themselves are synchronized. 1.28 + private Map<String, Boolean> isErrorLoggable = new IdentityHashMap<String, Boolean>(); 1.29 + private Map<String, Boolean> isWarnLoggable = new IdentityHashMap<String, Boolean>(); 1.30 + private Map<String, Boolean> isInfoLoggable = new IdentityHashMap<String, Boolean>(); 1.31 + private Map<String, Boolean> isDebugLoggable = new IdentityHashMap<String, Boolean>(); 1.32 + private Map<String, Boolean> isVerboseLoggable = new IdentityHashMap<String, Boolean>(); 1.33 + 1.34 + /** 1.35 + * Empty the caches of log levels. 1.36 + */ 1.37 + public void refreshLogLevels() { 1.38 + isErrorLoggable = new IdentityHashMap<String, Boolean>(); 1.39 + isWarnLoggable = new IdentityHashMap<String, Boolean>(); 1.40 + isInfoLoggable = new IdentityHashMap<String, Boolean>(); 1.41 + isDebugLoggable = new IdentityHashMap<String, Boolean>(); 1.42 + isVerboseLoggable = new IdentityHashMap<String, Boolean>(); 1.43 + } 1.44 + 1.45 + private boolean shouldLogError(String logTag) { 1.46 + Boolean out = isErrorLoggable.get(logTag); 1.47 + if (out != null) { 1.48 + return out.booleanValue(); 1.49 + } 1.50 + out = Log.isLoggable(logTag, Log.ERROR); 1.51 + isErrorLoggable.put(logTag, out); 1.52 + return out; 1.53 + } 1.54 + 1.55 + private boolean shouldLogWarn(String logTag) { 1.56 + Boolean out = isWarnLoggable.get(logTag); 1.57 + if (out != null) { 1.58 + return out.booleanValue(); 1.59 + } 1.60 + out = Log.isLoggable(logTag, Log.WARN); 1.61 + isWarnLoggable.put(logTag, out); 1.62 + return out; 1.63 + } 1.64 + 1.65 + private boolean shouldLogInfo(String logTag) { 1.66 + Boolean out = isInfoLoggable.get(logTag); 1.67 + if (out != null) { 1.68 + return out.booleanValue(); 1.69 + } 1.70 + out = Log.isLoggable(logTag, Log.INFO); 1.71 + isInfoLoggable.put(logTag, out); 1.72 + return out; 1.73 + } 1.74 + 1.75 + private boolean shouldLogDebug(String logTag) { 1.76 + Boolean out = isDebugLoggable.get(logTag); 1.77 + if (out != null) { 1.78 + return out.booleanValue(); 1.79 + } 1.80 + out = Log.isLoggable(logTag, Log.DEBUG); 1.81 + isDebugLoggable.put(logTag, out); 1.82 + return out; 1.83 + } 1.84 + 1.85 + @Override 1.86 + public boolean shouldLogVerbose(String logTag) { 1.87 + Boolean out = isVerboseLoggable.get(logTag); 1.88 + if (out != null) { 1.89 + return out.booleanValue(); 1.90 + } 1.91 + out = Log.isLoggable(logTag, Log.VERBOSE); 1.92 + isVerboseLoggable.put(logTag, out); 1.93 + return out; 1.94 + } 1.95 + 1.96 + public void error(String tag, String message, Throwable error) { 1.97 + if (shouldLogError(tag)) { 1.98 + inner.error(tag, message, error); 1.99 + } 1.100 + } 1.101 + 1.102 + public void warn(String tag, String message, Throwable error) { 1.103 + if (shouldLogWarn(tag)) { 1.104 + inner.warn(tag, message, error); 1.105 + } 1.106 + } 1.107 + 1.108 + public void info(String tag, String message, Throwable error) { 1.109 + if (shouldLogInfo(tag)) { 1.110 + inner.info(tag, message, error); 1.111 + } 1.112 + } 1.113 + 1.114 + public void debug(String tag, String message, Throwable error) { 1.115 + if (shouldLogDebug(tag)) { 1.116 + inner.debug(tag, message, error); 1.117 + } 1.118 + } 1.119 + 1.120 + public void trace(String tag, String message, Throwable error) { 1.121 + if (shouldLogVerbose(tag)) { 1.122 + inner.trace(tag, message, error); 1.123 + } 1.124 + } 1.125 + 1.126 + public void close() { 1.127 + inner.close(); 1.128 + } 1.129 +}