blob: 6d6f592df172f411b97eb702711df95c6b9e1507 (
plain)
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
138
139
140
141
142
|
/* *******************************************************************
* Copyright (c) 2005 Contributors.
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0
* which accompanies this distribution and is available at
* http://eclipse.org/legal/epl-v10.html
*
* Contributors:
* Wes Isberg initial implementation
* ******************************************************************/
// START-SAMPLE tracing-traceJoinPoints Trace join points executed to log
package org.aspectj.lib.tracing;
import org.aspectj.lang.*;
import org.aspectj.lang.reflect.*;
import java.io.*;
/**
* Print join points being executed in context to a log.xml file.
* To use this, define the abstract pointcuts in a subaspect.
* @author Jim Hugunin, Wes Isberg
*/
public abstract aspect TraceJoinPoints
extends TraceJoinPointsBase {
// abstract protected pointcut entry();
PrintStream out;
int logs = 0;
int depth = 0;
boolean terminal = false;
/**
* Emit a message in the log, e.g.,
* <pre>TraceJoinPoints tjp = TraceJoinPoints.aspectOf();
* if (null != tjp) tjp.message("Hello, World!");</pre>
*/
public void message(String s) {
out.println("<message>" + prepareMessage(s) + "</message>");
}
protected void startLog() {
makeLogStream();
}
protected void completeLog() {
closeLogStream();
}
protected void logEnter(JoinPoint.StaticPart jp) {
if (terminal) out.println(">");
indent(depth);
out.print("<" + jp.getKind());
writeSig(jp);
writePos(jp);
depth += 1;
terminal = true;
}
protected void logExit(JoinPoint.StaticPart jp) {
depth -= 1;
if (terminal) {
getOut().println("/>");
} else {
indent(depth);
getOut().println("</" + jp.getKind() + ">");
}
terminal = false;
}
protected PrintStream getOut() {
if (null == out) {
String m = "not in the control flow of entry()";
throw new IllegalStateException(m);
}
return out;
}
protected void makeLogStream() {
try {
String name = "log" + logs++ + ".xml";
out = new PrintStream(new FileOutputStream(name));
} catch (IOException ioe) {
out = System.err;
}
}
protected void closeLogStream() {
PrintStream out = this.out;
if (null != out) {
out.close();
// this.out = null;
}
}
/** @return input String formatted for XML */
protected String prepareMessage(String s) { // XXX unimplemented
return s;
}
void message(String sink, String s) {
if (null == sink) {
message(s);
} else {
getOut().println("<message sink=" + quoteXml(sink)
+ " >" + prepareMessage(s) + "</message>");
}
}
void writeSig(JoinPoint.StaticPart jp) {
PrintStream out = getOut();
out.print(" sig=");
out.print(quoteXml(jp.getSignature().toShortString()));
}
void writePos(JoinPoint.StaticPart jp) {
SourceLocation loc = jp.getSourceLocation();
if (loc == null) return;
PrintStream out = getOut();
out.print(" pos=");
out.print(quoteXml(loc.getFileName() +
":" + loc.getLine() +
":" + loc.getColumn()));
}
protected String quoteXml(String s) { // XXX weak
return "\"" + s.replace('<', '_').replace('>', '_') + "\"";
}
protected void indent(int i) {
PrintStream out = getOut();
while (i-- > 0) out.print(" ");
}
}
// END-SAMPLE tracing-traceJoinPoints
|