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.IOException;
20  import java.io.Writer;
21  import java.util.Stack;
22  
23  import org.e2etrace.trace.ITraceSession;
24  import org.e2etrace.trace.ITraceStep;
25  import org.e2etrace.trace.TraceSessionRootStep;
26  import org.e2etrace.trace.TraceSessionRootStepId;
27  
28  
29  /**
30   * CSV trace formatter.
31   * <p>
32   *
33   * This formatter generates a CSV stream from the data of a trace tree to
34   * stdout. The format of the CSV stream is as follows:
35   * <p>
36   *
37   * <pre>
38   *  trace id, trace path, threadname, duration, isolated duration
39   * </pre>
40   *
41   * The column <code>trace path</code> contains chained ids that represent the trace path.
42   * The IDs will be separated by dots. Example: The Id
43   * <code>$Session.step1.step2</code> will be generated for a trace step with
44   * id <code>step2</code> that is a child of <code>step1</code> and is
45   * executed in the context of trace session <code>$Session</code>.
46   * <p>
47   *
48   * @author Gunther Popp
49   *
50   */
51  public class CSVTraceFormatter extends AbstractTraceFormatter {
52  
53    private static final String CSV_DELIM = ",";
54    private static final String PATH_DELIM = "|";
55  
56    /**
57     * Default constructor.
58     * <p>
59     *
60     */
61    public CSVTraceFormatter() {
62  
63    }
64  
65    /** {@inheritDoc} */
66    protected String formatSingleStep(ITraceStep step, int level) {
67      StringBuffer output = new StringBuffer();
68      StringBuffer path = new StringBuffer();
69      ITraceStep ts = step;
70      String threadName = null;
71      TraceSessionRootStepId rootId;
72      Stack stepHierarchy = new Stack();
73  
74      // Build the hierarchical id and retrieve the thread name
75      do {
76        stepHierarchy.push(ts.getId().asString());
77  
78        if( ts instanceof TraceSessionRootStep && threadName == null) {
79          rootId = (TraceSessionRootStepId) ts.getId();
80          threadName = rootId.getThreadName();
81        }
82  
83        ts = ts.getParent();
84  
85      } while( ts != null);
86  
87      while( !stepHierarchy.empty() ){
88        path.append((String) stepHierarchy.pop());
89        if(!stepHierarchy.empty()) {
90          path.append(PATH_DELIM);
91        }
92      }
93  
94      // Write the CSV line
95      output.append(step.getId().asString());
96      output.append(CSV_DELIM);
97      output.append(path.toString());
98      output.append(CSV_DELIM);
99      output.append(threadName);
100     output.append(CSV_DELIM);
101     output.append(step.getDuration());
102     output.append(CSV_DELIM);
103     output.append(step.getIsolatedDuration());
104     output.append(getNewLine());
105 
106     return output.toString();
107   }
108 
109   /** {@inheritDoc} */
110   protected void writeFooter(ITraceSession session, Writer toWriter) {
111     // This method intentionally does nothing
112 
113   }
114 
115   /** {@inheritDoc}
116    * @throws IOException */
117   protected void writeHeader(ITraceSession session, Writer toWriter) throws IOException {
118     toWriter.write("id,path,threadname,duration,isolated_duration");
119     toWriter.write(getNewLine());
120   }
121 }