|
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.io.PrintWriter; |
|
8 |
|
9 /** |
|
10 * Log to a <code>PrintWriter</code>. |
|
11 */ |
|
12 public class PrintLogWriter extends LogWriter { |
|
13 protected final PrintWriter pw; |
|
14 protected boolean closed = false; |
|
15 |
|
16 public static final String ERROR = " :: E :: "; |
|
17 public static final String WARN = " :: W :: "; |
|
18 public static final String INFO = " :: I :: "; |
|
19 public static final String DEBUG = " :: D :: "; |
|
20 public static final String VERBOSE = " :: V :: "; |
|
21 |
|
22 public PrintLogWriter(PrintWriter pw) { |
|
23 this.pw = pw; |
|
24 } |
|
25 |
|
26 protected void log(String tag, String message, Throwable error) { |
|
27 if (closed) { |
|
28 return; |
|
29 } |
|
30 |
|
31 pw.println(tag + message); |
|
32 if (error != null) { |
|
33 error.printStackTrace(pw); |
|
34 } |
|
35 } |
|
36 |
|
37 @Override |
|
38 public void error(String tag, String message, Throwable error) { |
|
39 log(tag, ERROR + message, error); |
|
40 } |
|
41 |
|
42 @Override |
|
43 public void warn(String tag, String message, Throwable error) { |
|
44 log(tag, WARN + message, error); |
|
45 } |
|
46 |
|
47 @Override |
|
48 public void info(String tag, String message, Throwable error) { |
|
49 log(tag, INFO + message, error); |
|
50 } |
|
51 |
|
52 @Override |
|
53 public void debug(String tag, String message, Throwable error) { |
|
54 log(tag, DEBUG + message, error); |
|
55 } |
|
56 |
|
57 @Override |
|
58 public void trace(String tag, String message, Throwable error) { |
|
59 log(tag, VERBOSE + message, error); |
|
60 } |
|
61 |
|
62 @Override |
|
63 public boolean shouldLogVerbose(String tag) { |
|
64 return true; |
|
65 } |
|
66 |
|
67 public void close() { |
|
68 if (closed) { |
|
69 return; |
|
70 } |
|
71 if (pw != null) { |
|
72 pw.close(); |
|
73 } |
|
74 closed = true; |
|
75 } |
|
76 } |