michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.background.common.log.writers; michael@0: michael@0: import java.io.PrintWriter; michael@0: michael@0: /** michael@0: * Log to a PrintWriter. michael@0: */ michael@0: public class PrintLogWriter extends LogWriter { michael@0: protected final PrintWriter pw; michael@0: protected boolean closed = false; michael@0: michael@0: public static final String ERROR = " :: E :: "; michael@0: public static final String WARN = " :: W :: "; michael@0: public static final String INFO = " :: I :: "; michael@0: public static final String DEBUG = " :: D :: "; michael@0: public static final String VERBOSE = " :: V :: "; michael@0: michael@0: public PrintLogWriter(PrintWriter pw) { michael@0: this.pw = pw; michael@0: } michael@0: michael@0: protected void log(String tag, String message, Throwable error) { michael@0: if (closed) { michael@0: return; michael@0: } michael@0: michael@0: pw.println(tag + message); michael@0: if (error != null) { michael@0: error.printStackTrace(pw); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public void error(String tag, String message, Throwable error) { michael@0: log(tag, ERROR + message, error); michael@0: } michael@0: michael@0: @Override michael@0: public void warn(String tag, String message, Throwable error) { michael@0: log(tag, WARN + message, error); michael@0: } michael@0: michael@0: @Override michael@0: public void info(String tag, String message, Throwable error) { michael@0: log(tag, INFO + message, error); michael@0: } michael@0: michael@0: @Override michael@0: public void debug(String tag, String message, Throwable error) { michael@0: log(tag, DEBUG + message, error); michael@0: } michael@0: michael@0: @Override michael@0: public void trace(String tag, String message, Throwable error) { michael@0: log(tag, VERBOSE + message, error); michael@0: } michael@0: michael@0: @Override michael@0: public boolean shouldLogVerbose(String tag) { michael@0: return true; michael@0: } michael@0: michael@0: public void close() { michael@0: if (closed) { michael@0: return; michael@0: } michael@0: if (pw != null) { michael@0: pw.close(); michael@0: } michael@0: closed = true; michael@0: } michael@0: }