-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggingInterceptor.java
More file actions
132 lines (119 loc) · 4.49 KB
/
LoggingInterceptor.java
File metadata and controls
132 lines (119 loc) · 4.49 KB
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
package DenemeAgent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.brutusin.commons.json.spi.JsonCodec;
import org.brutusin.instrumentation.Interceptor;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
public class LoggingInterceptor extends Interceptor {
private File rootFile;
private final Map<String, Long> startMap = new HashMap();
@Override
public void init(String arg) throws Exception {
if (arg == null) {
throw new IllegalArgumentException(LoggingInterceptor.class.getCanonicalName() + " Basarisiz Oldu...");
}
this.rootFile = new File(arg);
if (!rootFile.exists()) {
FileUtils.forceMkdir(rootFile);
}
System.err.println("[LoggingInterceptor agent] Logging to " + rootFile);
}
@Override
public boolean interceptClass(String className, byte[] byteCode) {
return className.endsWith("SimpleClass");
}
@Override
public boolean interceptMethod(ClassNode cn, MethodNode mn) {
return true;
}
@Override
protected void doOnStart(Object source, Object[] arg, String executionId) {
long start = System.currentTimeMillis();
startMap.put(executionId, start);
File file = getFile(source, executionId);
trace(file, "#Kaynak : " + source);
trace(file, "#Baslangic Suresi : " + new Date(start));
trace(file, "#Degiskenler : ");
trace(file, toString(arg));
}
@Override
protected void doOnThrowableThrown(Object source, Throwable throwable, String executionId) {
}
@Override
protected void doOnThrowableUncatched(Object source, Throwable throwable, String executionId) {
long start = startMap.remove(executionId);
File file = getFile(source, executionId);
trace(file, "#Toplam Suresi : " + (System.currentTimeMillis() - start) + " ms");
trace(file, "#Thrown : ");
trace(file, toString(throwable));
}
@Override
protected void doOnFinish(Object source, Object result, String executionId) {
long start = startMap.remove(executionId);
File file = getFile(source, executionId);
trace(file, "#Toplam Suresi : " + (System.currentTimeMillis() - start) + " ms");
trace(file, "#Returned : ");
trace(file, toString(result));
}
private static void trace(File f, String s) {
if (s == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(f, true);
try {
fos.write(s.getBytes());
fos.write("\n".getBytes());
} finally {
fos.close();
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
private File getFile(Object source, String executionId) {
String loggingFolderPath = "JVM-" + ManagementFactory.getRuntimeMXBean().getStartTime() + "/" + Thread.currentThread().getName() + "-" + Thread.currentThread().getId() + "/" + executionId + "-";
if (source instanceof Method) {
Method m = (Method) source;
loggingFolderPath += m.getDeclaringClass().getName() + "." + m.getName() + "()";
} else if (source instanceof Constructor) {
Constructor c = (Constructor) source;
String className = c.getDeclaringClass().getName();
if (className != null && className.length() > 0) {
loggingFolderPath += className + ".init()";
} else {
loggingFolderPath += "init()";
}
} else {
loggingFolderPath += source;
}
loggingFolderPath += ".log";
loggingFolderPath = loggingFolderPath.replaceAll("[<>:]", "-");
File ret = new File(rootFile, loggingFolderPath);
try {
FileUtils.forceMkdir(ret.getParentFile());
} catch (IOException ex) {
throw new RuntimeException(ex);
}
return ret;
}
private static String toString(Object obj) {
if (obj == null) {
return null;
}
try {
return JsonCodec.getInstance().transform(obj);
} catch (Throwable th) {
return obj.toString();
}
}
}