View Javadoc

1   package org.e2etrace.formatter;
2   
3   /*
4    * Copyright 2006 Gunther Popp
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.Writer;
20  
21  import org.e2etrace.trace.ITraceSession;
22  import org.e2etrace.trace.ITraceStep;
23  import org.e2etrace.trace.TraceSessionRootStep;
24  import org.e2etrace.trace.TraceSessionRootStepId;
25  
26  
27  /**
28   * Plain text trace formatter.
29   * <p>
30   *
31   * This formatter generates plain text and can be used to print trace trees to
32   * stdout.
33   * <p>
34   *
35   * @author Gunther Popp
36   *
37   */
38  public class PlainTextTraceFormatter extends AbstractTraceFormatter {
39  
40    /**
41     * Default constructor.
42     * <p>
43     *
44     */
45    public PlainTextTraceFormatter() {
46  
47    }
48  
49    /** {@inheritDoc} */
50    protected String formatSingleStep(ITraceStep step, int level) {
51      StringBuffer output = new StringBuffer();
52      TraceSessionRootStepId rootId;
53  
54      for (int i = 0; i < level; i++) {
55        output.append("  ");
56      }
57  
58      if (step instanceof TraceSessionRootStep) {
59        rootId = (TraceSessionRootStepId) step.getId();
60  
61        output.append(">> ");
62        output.append(rootId.asString());
63        output.append(" [" + rootId.getThreadName() + "]");
64      } else {
65        output.append(step.getId().asString());
66  
67      }
68      output.append(" (");
69      output.append("Total: ");
70      output.append(step.getDuration());
71      output.append("ms, ");
72  
73      if (step.getIsolatedDuration() >= 0) {
74        output.append("Step: ");
75        output.append(step.getIsolatedDuration());
76        output.append("ms");
77      } else {
78        output.append("n/a");
79      }
80  
81      output.append(")");
82      output.append(getNewLine());
83  
84      return output.toString();
85    }
86  
87    /** {@inheritDoc} */
88    protected void writeFooter(ITraceSession session, Writer toWriter) {
89      // This method intentionally does nothing
90  
91    }
92  
93    /** {@inheritDoc} */
94    protected void writeHeader(ITraceSession session, Writer toWriter) {
95      // This method intentionally does nothing
96  
97    }
98  
99  }