summaryrefslogtreecommitdiffstats
path: root/mobile/android/services/src/main/java/org/mozilla/gecko/background/common/log/writers/AndroidLevelCachingLogWriter.java
blob: ac4250a03da5899dd276f31e42f3617271253a3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.background.common.log.writers;

import java.util.IdentityHashMap;
import java.util.Map;

import android.util.Log;

/**
 * Make a <code>LogWriter</code> only log when the Android log system says to.
 */
public class AndroidLevelCachingLogWriter extends LogWriter {
  protected final LogWriter inner;

  public AndroidLevelCachingLogWriter(LogWriter inner) {
    this.inner = inner;
  }

  // I can't believe we have to implement this ourselves.
  // These aren't synchronized (and neither are the setters) because
  // the logging calls themselves are synchronized.
  private Map<String, Boolean> isErrorLoggable   = new IdentityHashMap<String, Boolean>();
  private Map<String, Boolean> isWarnLoggable    = new IdentityHashMap<String, Boolean>();
  private Map<String, Boolean> isInfoLoggable    = new IdentityHashMap<String, Boolean>();
  private Map<String, Boolean> isDebugLoggable   = new IdentityHashMap<String, Boolean>();
  private Map<String, Boolean> isVerboseLoggable = new IdentityHashMap<String, Boolean>();

  /**
   * Empty the caches of log levels.
   */
  public void refreshLogLevels() {
    isErrorLoggable   = new IdentityHashMap<String, Boolean>();
    isWarnLoggable    = new IdentityHashMap<String, Boolean>();
    isInfoLoggable    = new IdentityHashMap<String, Boolean>();
    isDebugLoggable   = new IdentityHashMap<String, Boolean>();
    isVerboseLoggable = new IdentityHashMap<String, Boolean>();
  }

  private boolean shouldLogError(String logTag) {
    Boolean out = isErrorLoggable.get(logTag);
    if (out != null) {
      return out;
    }
    out = Log.isLoggable(logTag, Log.ERROR);
    isErrorLoggable.put(logTag, out);
    return out;
  }

  private boolean shouldLogWarn(String logTag) {
    Boolean out = isWarnLoggable.get(logTag);
    if (out != null) {
      return out;
    }
    out = Log.isLoggable(logTag, Log.WARN);
    isWarnLoggable.put(logTag, out);
    return out;
  }

  private boolean shouldLogInfo(String logTag) {
    Boolean out = isInfoLoggable.get(logTag);
    if (out != null) {
      return out;
    }
    out = Log.isLoggable(logTag, Log.INFO);
    isInfoLoggable.put(logTag, out);
    return out;
  }

  private boolean shouldLogDebug(String logTag) {
    Boolean out = isDebugLoggable.get(logTag);
    if (out != null) {
      return out;
    }
    out = Log.isLoggable(logTag, Log.DEBUG);
    isDebugLoggable.put(logTag, out);
    return out;
  }

  @Override
  public boolean shouldLogVerbose(String logTag) {
    Boolean out = isVerboseLoggable.get(logTag);
    if (out != null) {
      return out;
    }
    out = Log.isLoggable(logTag, Log.VERBOSE);
    isVerboseLoggable.put(logTag, out);
    return out;
  }

  @Override
  public void error(String tag, String message, Throwable error) {
    if (shouldLogError(tag)) {
      inner.error(tag, message, error);
    }
  }

  @Override
  public void warn(String tag, String message, Throwable error) {
    if (shouldLogWarn(tag)) {
      inner.warn(tag, message, error);
    }
  }

  @Override
  public void info(String tag, String message, Throwable error) {
    if (shouldLogInfo(tag)) {
      inner.info(tag, message, error);
    }
  }

  @Override
  public void debug(String tag, String message, Throwable error) {
    if (shouldLogDebug(tag)) {
      inner.debug(tag, message, error);
    }
  }

  @Override
  public void trace(String tag, String message, Throwable error) {
    if (shouldLogVerbose(tag)) {
      inner.trace(tag, message, error);
    }
  }

  @Override
  public void close() {
    inner.close();
  }
}