Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package org.mozilla.gecko.background.common.log.writers;
7 import java.util.IdentityHashMap;
8 import java.util.Map;
10 import android.util.Log;
12 /**
13 * Make a <code>LogWriter</code> only log when the Android log system says to.
14 */
15 public class AndroidLevelCachingLogWriter extends LogWriter {
16 protected final LogWriter inner;
18 public AndroidLevelCachingLogWriter(LogWriter inner) {
19 this.inner = inner;
20 }
22 // I can't believe we have to implement this ourselves.
23 // These aren't synchronized (and neither are the setters) because
24 // the logging calls themselves are synchronized.
25 private Map<String, Boolean> isErrorLoggable = new IdentityHashMap<String, Boolean>();
26 private Map<String, Boolean> isWarnLoggable = new IdentityHashMap<String, Boolean>();
27 private Map<String, Boolean> isInfoLoggable = new IdentityHashMap<String, Boolean>();
28 private Map<String, Boolean> isDebugLoggable = new IdentityHashMap<String, Boolean>();
29 private Map<String, Boolean> isVerboseLoggable = new IdentityHashMap<String, Boolean>();
31 /**
32 * Empty the caches of log levels.
33 */
34 public void refreshLogLevels() {
35 isErrorLoggable = new IdentityHashMap<String, Boolean>();
36 isWarnLoggable = new IdentityHashMap<String, Boolean>();
37 isInfoLoggable = new IdentityHashMap<String, Boolean>();
38 isDebugLoggable = new IdentityHashMap<String, Boolean>();
39 isVerboseLoggable = new IdentityHashMap<String, Boolean>();
40 }
42 private boolean shouldLogError(String logTag) {
43 Boolean out = isErrorLoggable.get(logTag);
44 if (out != null) {
45 return out.booleanValue();
46 }
47 out = Log.isLoggable(logTag, Log.ERROR);
48 isErrorLoggable.put(logTag, out);
49 return out;
50 }
52 private boolean shouldLogWarn(String logTag) {
53 Boolean out = isWarnLoggable.get(logTag);
54 if (out != null) {
55 return out.booleanValue();
56 }
57 out = Log.isLoggable(logTag, Log.WARN);
58 isWarnLoggable.put(logTag, out);
59 return out;
60 }
62 private boolean shouldLogInfo(String logTag) {
63 Boolean out = isInfoLoggable.get(logTag);
64 if (out != null) {
65 return out.booleanValue();
66 }
67 out = Log.isLoggable(logTag, Log.INFO);
68 isInfoLoggable.put(logTag, out);
69 return out;
70 }
72 private boolean shouldLogDebug(String logTag) {
73 Boolean out = isDebugLoggable.get(logTag);
74 if (out != null) {
75 return out.booleanValue();
76 }
77 out = Log.isLoggable(logTag, Log.DEBUG);
78 isDebugLoggable.put(logTag, out);
79 return out;
80 }
82 @Override
83 public boolean shouldLogVerbose(String logTag) {
84 Boolean out = isVerboseLoggable.get(logTag);
85 if (out != null) {
86 return out.booleanValue();
87 }
88 out = Log.isLoggable(logTag, Log.VERBOSE);
89 isVerboseLoggable.put(logTag, out);
90 return out;
91 }
93 public void error(String tag, String message, Throwable error) {
94 if (shouldLogError(tag)) {
95 inner.error(tag, message, error);
96 }
97 }
99 public void warn(String tag, String message, Throwable error) {
100 if (shouldLogWarn(tag)) {
101 inner.warn(tag, message, error);
102 }
103 }
105 public void info(String tag, String message, Throwable error) {
106 if (shouldLogInfo(tag)) {
107 inner.info(tag, message, error);
108 }
109 }
111 public void debug(String tag, String message, Throwable error) {
112 if (shouldLogDebug(tag)) {
113 inner.debug(tag, message, error);
114 }
115 }
117 public void trace(String tag, String message, Throwable error) {
118 if (shouldLogVerbose(tag)) {
119 inner.trace(tag, message, error);
120 }
121 }
123 public void close() {
124 inner.close();
125 }
126 }