File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\message\Trace.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.message;
8
9 import java.text.MessageFormat;
10 import java.util.ArrayList;
11 import org.h2.constant.SysProperties;
12 import org.h2.expression.ParameterInterface;
13 import org.h2.util.StatementBuilder;
14 import org.h2.util.StringUtils;
15 import org.h2.value.Value;
16
17 /**
18 * This class represents a trace module.
19 */
20 public class Trace {
21
22 /**
23 * The trace module name for commands.
24 */
25 public static final String COMMAND = "command";
26
27 /**
28 * The trace module name for constraints.
29 */
30 public static final String CONSTRAINT = "constraint";
31
32 /**
33 * The trace module name for databases.
34 */
35 public static final String DATABASE = "database";
36
37 /**
38 * The trace module name for functions.
39 */
40 public static final String FUNCTION = "function";
41
42 /**
43 * The trace module name for file locks.
44 */
45 public static final String FILE_LOCK = "fileLock";
46
47 /**
48 * The trace module name for indexes.
49 */
50 public static final String INDEX = "index";
51
52 /**
53 * The trace module name for the JDBC API.
54 */
55 public static final String JDBC = "jdbc";
56
57 /**
58 * The trace module name for locks.
59 */
60 public static final String LOCK = "lock";
61
62 /**
63 * The trace module name for schemas.
64 */
65 public static final String SCHEMA = "schema";
66
67 /**
68 * The trace module name for sequences.
69 */
70 public static final String SEQUENCE = "sequence";
71
72 /**
73 * The trace module name for settings.
74 */
75 public static final String SETTING = "setting";
76
77 /**
78 * The trace module name for tables.
79 */
80 public static final String TABLE = "table";
81
82 /**
83 * The trace module name for triggers.
84 */
85 public static final String TRIGGER = "trigger";
86
87 /**
88 * The trace module name for users.
89 */
90 public static final String USER = "user";
91
92 /**
93 * The trace module name for the page store.
94 */
95 public static final String PAGE_STORE = "pageStore";
96
97 private final TraceWriter traceWriter;
98 private final String module;
99 private final String lineSeparator;
100 private int traceLevel = TraceSystem.PARENT;
101
102 Trace(TraceWriter traceWriter, String module) {
103 this.traceWriter = traceWriter;
104 this.module = module;
105 this.lineSeparator = SysProperties.LINE_SEPARATOR;
106 }
107
108 /**
109 * Set the trace level of this component. This setting overrides the parent
110 * trace level.
111 *
112 * @param level the new level
113 */
114 public void setLevel(int level) {
115 this.traceLevel = level;
116 }
117
118 private boolean isEnabled(int level) {
119 if (this.traceLevel == TraceSystem.PARENT) {
120 return traceWriter.isEnabled(level);
121 }
122 return level <= this.traceLevel;
123 }
124
125 /**
126 * Check if the trace level is equal or higher than INFO.
127 *
128 * @return true if it is
129 */
130 public boolean isInfoEnabled() {
131 return isEnabled(TraceSystem.INFO);
132 }
133
134 /**
135 * Check if the trace level is equal or higher than DEBUG.
136 *
137 * @return true if it is
138 */
139 public boolean isDebugEnabled() {
140 return isEnabled(TraceSystem.DEBUG);
141 }
142
143 /**
144 * Write a message with trace level ERROR to the trace system.
145 *
146 * @param t the exception
147 * @param s the message
148 */
149 public void error(Throwable t, String s) {
150 if (isEnabled(TraceSystem.ERROR)) {
151 traceWriter.write(TraceSystem.ERROR, module, s, t);
152 }
153 }
154
155 /**
156 * Write a message with trace level ERROR to the trace system.
157 *
158 * @param t the exception
159 * @param s the message
160 * @param params the parameters
161 */
162 public void error(Throwable t, String s, Object... params) {
163 if (isEnabled(TraceSystem.ERROR)) {
164 s = MessageFormat.format(s, params);
165 traceWriter.write(TraceSystem.ERROR, module, s, t);
166 }
167 }
168
169 /**
170 * Write a message with trace level INFO to the trace system.
171 *
172 * @param s the message
173 */
174 public void info(String s) {
175 if (isEnabled(TraceSystem.INFO)) {
176 traceWriter.write(TraceSystem.INFO, module, s, null);
177 }
178 }
179
180 /**
181 * Write a message with trace level INFO to the trace system.
182 *
183 * @param s the message
184 * @param params the parameters
185 */
186 public void info(String s, Object... params) {
187 if (isEnabled(TraceSystem.INFO)) {
188 s = MessageFormat.format(s, params);
189 traceWriter.write(TraceSystem.INFO, module, s, null);
190 }
191 }
192
193 /**
194 * Write a message with trace level INFO to the trace system.
195 *
196 * @param t the exception
197 * @param s the message
198 */
199 void info(Throwable t, String s) {
200 if (isEnabled(TraceSystem.INFO)) {
201 traceWriter.write(TraceSystem.INFO, module, s, t);
202 }
203 }
204
205 /**
206 * Format the parameter list.
207 *
208 * @param parameters the parameter list
209 * @return the formatted text
210 */
211 public static String formatParams(ArrayList<? extends ParameterInterface> parameters) {
212 if (parameters.size() == 0) {
213 return "";
214 }
215 StatementBuilder buff = new StatementBuilder();
216 int i = 0;
217 boolean params = false;
218 for (ParameterInterface p : parameters) {
219 if (p.isValueSet()) {
220 if (!params) {
221 buff.append(" {");
222 params = true;
223 }
224 buff.appendExceptFirst(", ");
225 Value v = p.getParamValue();
226 buff.append(++i).append(": ").append(v.getTraceSQL());
227 }
228 }
229 if (params) {
230 buff.append('}');
231 }
232 return buff.toString();
233 }
234
235 /**
236 * Write a SQL statement with trace level INFO to the trace system.
237 *
238 * @param sql the SQL statement
239 * @param params the parameters used, in the for {1:...}
240 * @param count the update count
241 * @param time the time it took to run the statement in ms
242 */
243 public void infoSQL(String sql, String params, int count, long time) {
244 if (!isEnabled(TraceSystem.INFO)) {
245 return;
246 }
247 StringBuilder buff = new StringBuilder(sql.length() + params.length() + 20);
248 buff.append(lineSeparator).append("/*SQL");
249 boolean space = false;
250 if (params.length() > 0) {
251 // This looks like a bug, but it is intentional:
252 // If there are no parameters, the SQL statement is
253 // the rest of the line. If there are parameters, they
254 // are appended at the end of the line. Knowing the size
255 // of the statement simplifies separating the SQL statement
256 // from the parameters (no need to parse).
257 space = true;
258 buff.append(" l:").append(sql.length());
259 }
260 if (count > 0) {
261 space = true;
262 buff.append(" #:").append(count);
263 }
264 if (time > 0) {
265 space = true;
266 buff.append(" t:").append(time);
267 }
268 if (!space) {
269 buff.append(' ');
270 }
271 buff.append("*/").
272 append(StringUtils.javaEncode(sql)).
273 append(StringUtils.javaEncode(params)).
274 append(';');
275 sql = buff.toString();
276 traceWriter.write(TraceSystem.INFO, module, sql, null);
277 }
278
279 /**
280 * Write a message with trace level DEBUG to the trace system.
281 *
282 * @param s the message
283 * @param params the parameters
284 */
285 public void debug(String s, Object... params) {
286 if (isEnabled(TraceSystem.DEBUG)) {
287 s = MessageFormat.format(s, params);
288 traceWriter.write(TraceSystem.DEBUG, module, s, null);
289 }
290 }
291
292 /**
293 * Write a message with trace level DEBUG to the trace system.
294 *
295 * @param s the message
296 */
297 public void debug(String s) {
298 if (isEnabled(TraceSystem.DEBUG)) {
299 traceWriter.write(TraceSystem.DEBUG, module, s, null);
300 }
301 }
302
303 /**
304 * Write a message with trace level DEBUG to the trace system.
305 * @param t the exception
306 * @param s the message
307 */
308 public void debug(Throwable t, String s) {
309 if (isEnabled(TraceSystem.DEBUG)) {
310 traceWriter.write(TraceSystem.DEBUG, module, s, t);
311 }
312 }
313
314
315 /**
316 * Write Java source code with trace level INFO to the trace system.
317 *
318 * @param java the source code
319 */
320 public void infoCode(String java) {
321 if (isEnabled(TraceSystem.INFO)) {
322 traceWriter.write(TraceSystem.INFO, module, lineSeparator + "/**/" + java, null);
323 }
324 }
325
326 /**
327 * Write Java source code with trace level DEBUG to the trace system.
328 *
329 * @param java the source code
330 */
331 void debugCode(String java) {
332 if (isEnabled(TraceSystem.DEBUG)) {
333 traceWriter.write(TraceSystem.DEBUG, module, lineSeparator + "/**/" + java, null);
334 }
335 }
336
337 }
338