mobile/android/base/background/common/log/writers/PrintLogWriter.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/background/common/log/writers/PrintLogWriter.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,76 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.background.common.log.writers;
     1.9 +
    1.10 +import java.io.PrintWriter;
    1.11 +
    1.12 +/**
    1.13 + * Log to a <code>PrintWriter</code>.
    1.14 + */
    1.15 +public class PrintLogWriter extends LogWriter {
    1.16 +  protected final PrintWriter pw;
    1.17 +  protected boolean closed = false;
    1.18 +
    1.19 +  public static final String ERROR   = " :: E :: ";
    1.20 +  public static final String WARN    = " :: W :: ";
    1.21 +  public static final String INFO    = " :: I :: ";
    1.22 +  public static final String DEBUG   = " :: D :: ";
    1.23 +  public static final String VERBOSE = " :: V :: ";
    1.24 +
    1.25 +  public PrintLogWriter(PrintWriter pw) {
    1.26 +    this.pw = pw;
    1.27 +  }
    1.28 +
    1.29 +  protected void log(String tag, String message, Throwable error) {
    1.30 +    if (closed) {
    1.31 +      return;
    1.32 +    }
    1.33 +
    1.34 +    pw.println(tag + message);
    1.35 +    if (error != null) {
    1.36 +      error.printStackTrace(pw);
    1.37 +    }
    1.38 +  }
    1.39 +
    1.40 +  @Override
    1.41 +  public void error(String tag, String message, Throwable error) {
    1.42 +    log(tag, ERROR + message, error);
    1.43 +  }
    1.44 +
    1.45 +  @Override
    1.46 +  public void warn(String tag, String message, Throwable error) {
    1.47 +    log(tag, WARN + message, error);
    1.48 +  }
    1.49 +
    1.50 +  @Override
    1.51 +  public void info(String tag, String message, Throwable error) {
    1.52 +    log(tag, INFO + message, error);
    1.53 +  }
    1.54 +
    1.55 +  @Override
    1.56 +  public void debug(String tag, String message, Throwable error) {
    1.57 +    log(tag, DEBUG + message, error);
    1.58 +  }
    1.59 +
    1.60 +  @Override
    1.61 +  public void trace(String tag, String message, Throwable error) {
    1.62 +    log(tag, VERBOSE + message, error);
    1.63 +  }
    1.64 +
    1.65 +  @Override
    1.66 +  public boolean shouldLogVerbose(String tag) {
    1.67 +    return true;
    1.68 +  }
    1.69 +
    1.70 +  public void close() {
    1.71 +    if (closed) {
    1.72 +      return;
    1.73 +    }
    1.74 +    if (pw != null) {
    1.75 +      pw.close();
    1.76 +    }
    1.77 +    closed = true;
    1.78 +  }
    1.79 +}

mercurial