1 package org.e2etrace.formatter;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
58
59
60
61 public CSVTraceFormatter() {
62
63 }
64
65
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
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
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
110 protected void writeFooter(ITraceSession session, Writer toWriter) {
111
112
113 }
114
115
116
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 }