/
Trace.java
executable file
·274 lines (237 loc) · 8.03 KB
/
Trace.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
*
* Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
**/
package lucee.runtime.tag;
import java.io.IOException;
import lucee.commons.io.log.Log;
import lucee.commons.lang.StringUtil;
import lucee.runtime.PageSource;
import lucee.runtime.converter.ConverterException;
import lucee.runtime.converter.ScriptConverter;
import lucee.runtime.debug.DebugTrace;
import lucee.runtime.debug.DebugTraceImpl;
import lucee.runtime.debug.DebuggerImpl;
import lucee.runtime.engine.ThreadLocalPageContext;
import lucee.runtime.exp.Abort;
import lucee.runtime.exp.ApplicationException;
import lucee.runtime.exp.PageException;
import lucee.runtime.ext.tag.BodyTagImpl;
import lucee.runtime.functions.other.Dump;
import lucee.runtime.interpreter.VariableInterpreter;
import lucee.runtime.op.Caster;
import lucee.runtime.type.Struct;
import lucee.runtime.type.dt.DateTimeImpl;
import lucee.runtime.type.scope.Scope;
import lucee.runtime.type.trace.TraceObjectSupport;
public final class Trace extends BodyTagImpl {
private boolean abort = false;
private boolean follow = false;
private String category;
private boolean inline = false;
private String text;
private int type = Log.LEVEL_INFO;
private String var;
private Struct caller;
@Override
public void release() {
super.release();
abort = false;
category = null;
inline = false;
text = null;
type = Log.LEVEL_INFO;
var = null;
caller = null;
follow = false;
}
/**
* @param abort the abort to set
*/
public void setAbort(boolean abort) {
this.abort = abort;
}
public void setFollow(boolean follow) {
this.follow = follow;
}
/**
* @param category the category to set
*/
public void setCategory(String category) {
this.category = category;
}
/**
* @param inline the inline to set
*/
public void setInline(boolean inline) {
this.inline = inline;
}
/**
* @param text the text to set
*/
public void setText(String text) {
this.text = text;
}
/**
* @param type the type to set
* @throws ApplicationException
*/
public void setType(String strType) throws ApplicationException {
strType = strType.toLowerCase().trim();
if ("info".equals(strType)) type = Log.LEVEL_INFO;
if ("information".equals(strType)) type = Log.LEVEL_INFO;
else if ("warn".equals(strType)) type = Log.LEVEL_WARN;
else if ("warning".equals(strType)) type = Log.LEVEL_WARN;
else if ("error".equals(strType)) type = Log.LEVEL_ERROR;
else if ("fatal information".equals(strType)) type = Log.LEVEL_FATAL;
else if ("fatal-information".equals(strType)) type = Log.LEVEL_FATAL;
else if ("fatal_information".equals(strType)) type = Log.LEVEL_FATAL;
else if ("fatalinformation".equals(strType)) type = Log.LEVEL_FATAL;
else if ("fatal info".equals(strType)) type = Log.LEVEL_FATAL;
else if ("fatal-info".equals(strType)) type = Log.LEVEL_FATAL;
else if ("fatal_info".equals(strType)) type = Log.LEVEL_FATAL;
else if ("fatalinfo".equals(strType)) type = Log.LEVEL_FATAL;
else if ("fatal".equals(strType)) type = Log.LEVEL_FATAL;
else if ("debug".equals(strType)) type = Log.LEVEL_DEBUG;
else if ("debugging".equals(strType)) type = Log.LEVEL_DEBUG;
else if ("debuging".equals(strType)) type = Log.LEVEL_DEBUG;
else if ("trace".equals(strType)) type = Log.LEVEL_TRACE;
else throw new ApplicationException("invalid value [" + strType + "] for attribute [type], valid values are [Debug, Information, Warning, Error, Fatal Information]");
}
/**
* @param var the var to set
*/
public void setVar(String var) {
this.var = var;
}
public void setCaller(Struct caller) {
this.caller = caller;
}
/**
* @param var the var to set
*/
public void setVariable(String var) {
this.var = var;
}
@Override
public int doStartTag() {
return EVAL_BODY_INCLUDE;
}
@Override
public int doEndTag() throws PageException {
try {
_doEndTag();
}
catch (IOException e) {
}
return EVAL_PAGE;
}
public void _doEndTag() throws IOException, PageException {
PageSource ps = pageContext.getCurrentTemplatePageSource();
// var
String varValue = null;
Object value = null, traceValue = null;
if (!StringUtil.isEmpty(var)) {
try {
if (caller instanceof Scope) value = VariableInterpreter.getVariable(pageContext, var, (Scope) caller);
else value = pageContext.getVariable(var);
}
catch (PageException e) {
varValue = "(undefined)";
follow = false;
}
if (follow) {
// print.o(1);
if (StringUtil.isEmpty(text, true)) text = var;
// print.o(2);
traceValue = TraceObjectSupport.toTraceObject(pageContext.getDebugger(), value, type, category, text);
if (caller instanceof Scope) VariableInterpreter.setVariable(pageContext, var, traceValue, (Scope) caller);
else pageContext.setVariable(var, traceValue);
}
try {
varValue = new ScriptConverter().serialize(value);
}
catch (ConverterException e) {
if (value != null) varValue = "(" + Caster.toTypeName(value) + ")";
}
}
DebugTrace trace = ((DebuggerImpl) pageContext.getDebugger()).addTrace(type, category, text, ps, var, varValue);
DebugTrace[] traces = pageContext.getDebugger().getTraces(pageContext);
String total = "(1st trace)";
if (traces.length > 1) {
long t = 0;
for (int i = 0; i < traces.length; i++) {
t += traces[i].getTime();
}
total = "(" + t + ")";
}
boolean hasCat = !StringUtil.isEmpty(trace.getCategory());
boolean hasText = !StringUtil.isEmpty(trace.getText());
boolean hasVar = !StringUtil.isEmpty(var);
// inline
if (inline) {
lucee.runtime.format.TimeFormat tf = new lucee.runtime.format.TimeFormat(pageContext.getConfig().getLocale());
StringBuffer sb = new StringBuffer();
sb.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"white\">");
sb.append("<tr>");
// sb.append("<td><img src=\"/CFIDE/debug/images/Error_16x16.gif\" alt=\"Error type\">");
sb.append("<td>");
sb.append("<font color=\"orange\">");
sb.append("<b>");
sb.append(DebugTraceImpl.toType(trace.getType(), "INFO") + " - ");
sb.append("[CFTRACE " + tf.format(new DateTimeImpl(pageContext.getConfig()), "hh:mm:ss:l") + "]");
sb.append("[" + trace.getTime() + " ms " + total + "]");
sb.append("[" + trace.getTemplate() + " @ line: " + trace.getLine() + "]");
if (hasCat || hasText) sb.append(" -");
if (hasCat) sb.append(" [" + trace.getCategory() + "]");
if (hasText) sb.append(" <i>" + trace.getText() + " </i>");
sb.append("</b>");
sb.append("</font>");
sb.append("</td>");
sb.append("</tr>");
sb.append("</table>");
pageContext.forceWrite(sb.toString());
if (hasVar) Dump.call(pageContext, value, var);
}
// log
Log log = ThreadLocalPageContext.getLog(pageContext, "trace");
StringBuffer msg = new StringBuffer();
msg.append("[" + trace.getTime() + " ms " + total + "] ");
msg.append("[" + trace.getTemplate() + " @ line: " + trace.getLine() + "]");
if (hasCat || hasText || hasVar) msg.append("- ");
if (hasCat) msg.append("[" + trace.getCategory() + "] ");
if (hasVar) msg.append("[" + var + "=" + varValue + "] ");
if (hasText) msg.append(" " + trace.getText() + " ");
log.log(trace.getType(), "cftrace", msg.toString());
// abort
if (abort) throw new Abort(Abort.SCOPE_REQUEST);
}
@Override
public void doInitBody() {
}
@Override
public int doAfterBody() {
return SKIP_BODY;
}
/**
* sets if has body or not
*
* @param hasBody
*/
public void hasBody(boolean hasBody) {
}
}