michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.background.common.log.writers; michael@0: michael@0: import java.util.IdentityHashMap; michael@0: import java.util.Map; michael@0: michael@0: import android.util.Log; michael@0: michael@0: /** michael@0: * Make a LogWriter only log when the Android log system says to. michael@0: */ michael@0: public class AndroidLevelCachingLogWriter extends LogWriter { michael@0: protected final LogWriter inner; michael@0: michael@0: public AndroidLevelCachingLogWriter(LogWriter inner) { michael@0: this.inner = inner; michael@0: } michael@0: michael@0: // I can't believe we have to implement this ourselves. michael@0: // These aren't synchronized (and neither are the setters) because michael@0: // the logging calls themselves are synchronized. michael@0: private Map isErrorLoggable = new IdentityHashMap(); michael@0: private Map isWarnLoggable = new IdentityHashMap(); michael@0: private Map isInfoLoggable = new IdentityHashMap(); michael@0: private Map isDebugLoggable = new IdentityHashMap(); michael@0: private Map isVerboseLoggable = new IdentityHashMap(); michael@0: michael@0: /** michael@0: * Empty the caches of log levels. michael@0: */ michael@0: public void refreshLogLevels() { michael@0: isErrorLoggable = new IdentityHashMap(); michael@0: isWarnLoggable = new IdentityHashMap(); michael@0: isInfoLoggable = new IdentityHashMap(); michael@0: isDebugLoggable = new IdentityHashMap(); michael@0: isVerboseLoggable = new IdentityHashMap(); michael@0: } michael@0: michael@0: private boolean shouldLogError(String logTag) { michael@0: Boolean out = isErrorLoggable.get(logTag); michael@0: if (out != null) { michael@0: return out.booleanValue(); michael@0: } michael@0: out = Log.isLoggable(logTag, Log.ERROR); michael@0: isErrorLoggable.put(logTag, out); michael@0: return out; michael@0: } michael@0: michael@0: private boolean shouldLogWarn(String logTag) { michael@0: Boolean out = isWarnLoggable.get(logTag); michael@0: if (out != null) { michael@0: return out.booleanValue(); michael@0: } michael@0: out = Log.isLoggable(logTag, Log.WARN); michael@0: isWarnLoggable.put(logTag, out); michael@0: return out; michael@0: } michael@0: michael@0: private boolean shouldLogInfo(String logTag) { michael@0: Boolean out = isInfoLoggable.get(logTag); michael@0: if (out != null) { michael@0: return out.booleanValue(); michael@0: } michael@0: out = Log.isLoggable(logTag, Log.INFO); michael@0: isInfoLoggable.put(logTag, out); michael@0: return out; michael@0: } michael@0: michael@0: private boolean shouldLogDebug(String logTag) { michael@0: Boolean out = isDebugLoggable.get(logTag); michael@0: if (out != null) { michael@0: return out.booleanValue(); michael@0: } michael@0: out = Log.isLoggable(logTag, Log.DEBUG); michael@0: isDebugLoggable.put(logTag, out); michael@0: return out; michael@0: } michael@0: michael@0: @Override michael@0: public boolean shouldLogVerbose(String logTag) { michael@0: Boolean out = isVerboseLoggable.get(logTag); michael@0: if (out != null) { michael@0: return out.booleanValue(); michael@0: } michael@0: out = Log.isLoggable(logTag, Log.VERBOSE); michael@0: isVerboseLoggable.put(logTag, out); michael@0: return out; michael@0: } michael@0: michael@0: public void error(String tag, String message, Throwable error) { michael@0: if (shouldLogError(tag)) { michael@0: inner.error(tag, message, error); michael@0: } michael@0: } michael@0: michael@0: public void warn(String tag, String message, Throwable error) { michael@0: if (shouldLogWarn(tag)) { michael@0: inner.warn(tag, message, error); michael@0: } michael@0: } michael@0: michael@0: public void info(String tag, String message, Throwable error) { michael@0: if (shouldLogInfo(tag)) { michael@0: inner.info(tag, message, error); michael@0: } michael@0: } michael@0: michael@0: public void debug(String tag, String message, Throwable error) { michael@0: if (shouldLogDebug(tag)) { michael@0: inner.debug(tag, message, error); michael@0: } michael@0: } michael@0: michael@0: public void trace(String tag, String message, Throwable error) { michael@0: if (shouldLogVerbose(tag)) { michael@0: inner.trace(tag, message, error); michael@0: } michael@0: } michael@0: michael@0: public void close() { michael@0: inner.close(); michael@0: } michael@0: }