Wed, 31 Dec 2014 07:22:50 +0100
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.writers; |
michael@0 | 6 | |
michael@0 | 7 | import java.util.IdentityHashMap; |
michael@0 | 8 | import java.util.Map; |
michael@0 | 9 | |
michael@0 | 10 | import android.util.Log; |
michael@0 | 11 | |
michael@0 | 12 | /** |
michael@0 | 13 | * Make a <code>LogWriter</code> only log when the Android log system says to. |
michael@0 | 14 | */ |
michael@0 | 15 | public class AndroidLevelCachingLogWriter extends LogWriter { |
michael@0 | 16 | protected final LogWriter inner; |
michael@0 | 17 | |
michael@0 | 18 | public AndroidLevelCachingLogWriter(LogWriter inner) { |
michael@0 | 19 | this.inner = inner; |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | // I can't believe we have to implement this ourselves. |
michael@0 | 23 | // These aren't synchronized (and neither are the setters) because |
michael@0 | 24 | // the logging calls themselves are synchronized. |
michael@0 | 25 | private Map<String, Boolean> isErrorLoggable = new IdentityHashMap<String, Boolean>(); |
michael@0 | 26 | private Map<String, Boolean> isWarnLoggable = new IdentityHashMap<String, Boolean>(); |
michael@0 | 27 | private Map<String, Boolean> isInfoLoggable = new IdentityHashMap<String, Boolean>(); |
michael@0 | 28 | private Map<String, Boolean> isDebugLoggable = new IdentityHashMap<String, Boolean>(); |
michael@0 | 29 | private Map<String, Boolean> isVerboseLoggable = new IdentityHashMap<String, Boolean>(); |
michael@0 | 30 | |
michael@0 | 31 | /** |
michael@0 | 32 | * Empty the caches of log levels. |
michael@0 | 33 | */ |
michael@0 | 34 | public void refreshLogLevels() { |
michael@0 | 35 | isErrorLoggable = new IdentityHashMap<String, Boolean>(); |
michael@0 | 36 | isWarnLoggable = new IdentityHashMap<String, Boolean>(); |
michael@0 | 37 | isInfoLoggable = new IdentityHashMap<String, Boolean>(); |
michael@0 | 38 | isDebugLoggable = new IdentityHashMap<String, Boolean>(); |
michael@0 | 39 | isVerboseLoggable = new IdentityHashMap<String, Boolean>(); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | private boolean shouldLogError(String logTag) { |
michael@0 | 43 | Boolean out = isErrorLoggable.get(logTag); |
michael@0 | 44 | if (out != null) { |
michael@0 | 45 | return out.booleanValue(); |
michael@0 | 46 | } |
michael@0 | 47 | out = Log.isLoggable(logTag, Log.ERROR); |
michael@0 | 48 | isErrorLoggable.put(logTag, out); |
michael@0 | 49 | return out; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | private boolean shouldLogWarn(String logTag) { |
michael@0 | 53 | Boolean out = isWarnLoggable.get(logTag); |
michael@0 | 54 | if (out != null) { |
michael@0 | 55 | return out.booleanValue(); |
michael@0 | 56 | } |
michael@0 | 57 | out = Log.isLoggable(logTag, Log.WARN); |
michael@0 | 58 | isWarnLoggable.put(logTag, out); |
michael@0 | 59 | return out; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | private boolean shouldLogInfo(String logTag) { |
michael@0 | 63 | Boolean out = isInfoLoggable.get(logTag); |
michael@0 | 64 | if (out != null) { |
michael@0 | 65 | return out.booleanValue(); |
michael@0 | 66 | } |
michael@0 | 67 | out = Log.isLoggable(logTag, Log.INFO); |
michael@0 | 68 | isInfoLoggable.put(logTag, out); |
michael@0 | 69 | return out; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | private boolean shouldLogDebug(String logTag) { |
michael@0 | 73 | Boolean out = isDebugLoggable.get(logTag); |
michael@0 | 74 | if (out != null) { |
michael@0 | 75 | return out.booleanValue(); |
michael@0 | 76 | } |
michael@0 | 77 | out = Log.isLoggable(logTag, Log.DEBUG); |
michael@0 | 78 | isDebugLoggable.put(logTag, out); |
michael@0 | 79 | return out; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | @Override |
michael@0 | 83 | public boolean shouldLogVerbose(String logTag) { |
michael@0 | 84 | Boolean out = isVerboseLoggable.get(logTag); |
michael@0 | 85 | if (out != null) { |
michael@0 | 86 | return out.booleanValue(); |
michael@0 | 87 | } |
michael@0 | 88 | out = Log.isLoggable(logTag, Log.VERBOSE); |
michael@0 | 89 | isVerboseLoggable.put(logTag, out); |
michael@0 | 90 | return out; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | public void error(String tag, String message, Throwable error) { |
michael@0 | 94 | if (shouldLogError(tag)) { |
michael@0 | 95 | inner.error(tag, message, error); |
michael@0 | 96 | } |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | public void warn(String tag, String message, Throwable error) { |
michael@0 | 100 | if (shouldLogWarn(tag)) { |
michael@0 | 101 | inner.warn(tag, message, error); |
michael@0 | 102 | } |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | public void info(String tag, String message, Throwable error) { |
michael@0 | 106 | if (shouldLogInfo(tag)) { |
michael@0 | 107 | inner.info(tag, message, error); |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | public void debug(String tag, String message, Throwable error) { |
michael@0 | 112 | if (shouldLogDebug(tag)) { |
michael@0 | 113 | inner.debug(tag, message, error); |
michael@0 | 114 | } |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | public void trace(String tag, String message, Throwable error) { |
michael@0 | 118 | if (shouldLogVerbose(tag)) { |
michael@0 | 119 | inner.trace(tag, message, error); |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | public void close() { |
michael@0 | 124 | inner.close(); |
michael@0 | 125 | } |
michael@0 | 126 | } |