summaryrefslogtreecommitdiffstats
path: root/mobile/android/services/src/main/java/org/mozilla/gecko/background/common/log/writers/PrintLogWriter.java
blob: 6e1f63de32ea3f0f4f8a0c52f026cfb328da6688 (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
/* 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.io.PrintWriter;

/**
 * Log to a <code>PrintWriter</code>.
 */
public class PrintLogWriter extends LogWriter {
  protected final PrintWriter pw;
  protected boolean closed = false;

  public static final String ERROR   = " :: E :: ";
  public static final String WARN    = " :: W :: ";
  public static final String INFO    = " :: I :: ";
  public static final String DEBUG   = " :: D :: ";
  public static final String VERBOSE = " :: V :: ";

  public PrintLogWriter(PrintWriter pw) {
    this.pw = pw;
  }

  protected void log(String tag, String message, Throwable error) {
    if (closed) {
      return;
    }

    pw.println(tag + message);
    if (error != null) {
      error.printStackTrace(pw);
    }
  }

  @Override
  public void error(String tag, String message, Throwable error) {
    log(tag, ERROR + message, error);
  }

  @Override
  public void warn(String tag, String message, Throwable error) {
    log(tag, WARN + message, error);
  }

  @Override
  public void info(String tag, String message, Throwable error) {
    log(tag, INFO + message, error);
  }

  @Override
  public void debug(String tag, String message, Throwable error) {
    log(tag, DEBUG + message, error);
  }

  @Override
  public void trace(String tag, String message, Throwable error) {
    log(tag, VERBOSE + message, error);
  }

  @Override
  public boolean shouldLogVerbose(String tag) {
    return true;
  }

  @Override
  public void close() {
    if (closed) {
      return;
    }
    if (pw != null) {
      pw.close();
    }
    closed = true;
  }
}