01: /*
02: * Copyright 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package com.google.gwt.dev.util.log;
17:
18: import java.io.PrintWriter;
19:
20: /**
21: * Tree logger that logs to a print writer.
22: */
23: public final class PrintWriterTreeLogger extends AbstractTreeLogger {
24:
25: private final PrintWriter out;
26:
27: private final String indent;
28:
29: public PrintWriterTreeLogger() {
30: this (new PrintWriter(System.out, true));
31: }
32:
33: public PrintWriterTreeLogger(PrintWriter out) {
34: this (out, "");
35: }
36:
37: protected PrintWriterTreeLogger(PrintWriter out, String indent) {
38: this .out = out;
39: this .indent = indent;
40: }
41:
42: protected AbstractTreeLogger doBranch() {
43: return new PrintWriterTreeLogger(out, indent + " ");
44: }
45:
46: protected void doCommitBranch(
47: AbstractTreeLogger childBeingCommitted, Type type,
48: String msg, Throwable caught) {
49: doLog(childBeingCommitted.getBranchedIndex(), type, msg, caught);
50: }
51:
52: protected void doLog(int indexOfLogEntryWithinParentLogger,
53: Type type, String msg, Throwable caught) {
54: out.print(indent);
55: if (type.needsAttention()) {
56: out.print("[");
57: out.print(type.getLabel());
58: out.print("] ");
59: }
60:
61: out.println(msg);
62: if (caught != null) {
63: caught.printStackTrace(out);
64: }
65: }
66: }
|