-
Notifications
You must be signed in to change notification settings - Fork 147
/
ConsoleLogDumpApplication.java
137 lines (116 loc) · 4.88 KB
/
ConsoleLogDumpApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.insightfullogic.honest_profiler.ports.console;
import com.insightfullogic.honest_profiler.core.Monitor;
import com.insightfullogic.honest_profiler.core.parser.LogEventListener;
import com.insightfullogic.honest_profiler.core.parser.Method;
import com.insightfullogic.honest_profiler.core.parser.StackFrame;
import com.insightfullogic.honest_profiler.core.parser.TraceStart;
import com.insightfullogic.honest_profiler.ports.sources.FileLogSource;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import java.io.File;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
/**
* Converts an hpl log to text, line by line. Only extra convenience offered is the translation on method
* names where they have already been spelled out in the log.
*
* @author nitsanw
*
*/
public class ConsoleLogDumpApplication {
private final Console output;
private final Console error;
private File logLocation;
public static void main(String[] args) {
ConsoleLogDumpApplication entry = new ConsoleLogDumpApplication(() -> System.err, () -> System.out);
CmdLineParser parser = new CmdLineParser(entry);
try {
parser.parseArgument(args);
entry.run();
} catch (CmdLineException e) {
System.err.println(e.getMessage());
parser.printUsage(System.err);
}
}
public ConsoleLogDumpApplication(final Console error, final Console output) {
this.output = output;
this.error = error;
}
@Option(name = "-log", usage = "set the log that you want to parser or use", required = true)
public void setLogLocation(String logLocation) {
setLogLocation(new File(logLocation));
}
public void setLogLocation(File logLocation) {
this.logLocation = logLocation;
}
public void run() {
final PrintStream err = error.stream();
if (!logLocation.exists() || !logLocation.canRead()) {
err.println("Unable to find log file at: " + logLocation);
return;
}
final PrintStream out = output.stream();
out.println("Printing text representation for: " + logLocation.getAbsolutePath());
Monitor.consumeFile(new FileLogSource(logLocation), new LogEventListener() {
int indent;
long traceidx;
long errCount;
Map<Long, BoundMethod> methodNames = new HashMap<>();
@Override
public void handle(Method method) {
BoundMethod boundMethod = new BoundMethod(method.getClassName(), method.getMethodName());
out.printf("Method : %d -> %s.%s\n", method.getMethodId(), method.getClassName(), method.getMethodName());
methodNames.put(method.getMethodId(), boundMethod);
}
@Override
public void handle(StackFrame stackFrame) {
indent--;
long methodId = stackFrame.getMethodId();
BoundMethod boundMethod = methodNames.get(methodId);
if (methodId == 0) { // null method
errCount++;
out.print("StackFrame: ");
indent(out);
out.printf("%d @ %s\n", methodId, stackFrame.getLineNumber());
} else if (boundMethod == null) {
out.print("StackFrame: ");
indent(out);
out.printf("%d @ %s\n", methodId, stackFrame.getLineNumber());
} else {
out.print("StackFrame: ");
indent(out);
out.printf("%s::%s @ %s\n", boundMethod.className, boundMethod.methodName, stackFrame.getLineNumber());
}
}
private void indent(final PrintStream out) {
for (int i = 0; i < indent; i++)
out.print(' ');
}
@Override
public void handle(TraceStart traceStart) {
int frames = traceStart.getNumberOfFrames();
if (frames <= 0) {
out.printf("TraceStart: [%d] tid=%d,frames=%d\n", traceidx, traceStart.getThreadId(), frames);
errCount++;
} else {
out.printf("TraceStartL [%d] tid=%d,frames=%d\n", traceidx, traceStart.getThreadId(), frames);
}
indent = frames;
traceidx++;
}
@Override
public void endOfLog() {
out.printf("Processed %d traces, %d faulty\n", traceidx, errCount);
}
});
}
private static class BoundMethod {
private final String className, methodName;
public BoundMethod(String className, String methodName) {
this.className = className;
this.methodName = methodName;
}
}
}