|
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/. */ |
|
4 |
|
5 package org.mozilla.gecko.background.common.log.writers; |
|
6 |
|
7 import java.util.IdentityHashMap; |
|
8 import java.util.Map; |
|
9 |
|
10 import android.util.Log; |
|
11 |
|
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; |
|
17 |
|
18 public AndroidLevelCachingLogWriter(LogWriter inner) { |
|
19 this.inner = inner; |
|
20 } |
|
21 |
|
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>(); |
|
30 |
|
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 } |
|
41 |
|
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 } |
|
51 |
|
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 } |
|
61 |
|
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 } |
|
71 |
|
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 } |
|
81 |
|
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 } |
|
92 |
|
93 public void error(String tag, String message, Throwable error) { |
|
94 if (shouldLogError(tag)) { |
|
95 inner.error(tag, message, error); |
|
96 } |
|
97 } |
|
98 |
|
99 public void warn(String tag, String message, Throwable error) { |
|
100 if (shouldLogWarn(tag)) { |
|
101 inner.warn(tag, message, error); |
|
102 } |
|
103 } |
|
104 |
|
105 public void info(String tag, String message, Throwable error) { |
|
106 if (shouldLogInfo(tag)) { |
|
107 inner.info(tag, message, error); |
|
108 } |
|
109 } |
|
110 |
|
111 public void debug(String tag, String message, Throwable error) { |
|
112 if (shouldLogDebug(tag)) { |
|
113 inner.debug(tag, message, error); |
|
114 } |
|
115 } |
|
116 |
|
117 public void trace(String tag, String message, Throwable error) { |
|
118 if (shouldLogVerbose(tag)) { |
|
119 inner.trace(tag, message, error); |
|
120 } |
|
121 } |
|
122 |
|
123 public void close() { |
|
124 inner.close(); |
|
125 } |
|
126 } |