1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.message; |
7 | |
8 | import java.text.MessageFormat; |
9 | import java.util.ArrayList; |
10 | |
11 | import org.h2.engine.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( |
212 | ArrayList<? extends ParameterInterface> parameters) { |
213 | if (parameters.size() == 0) { |
214 | return ""; |
215 | } |
216 | StatementBuilder buff = new StatementBuilder(); |
217 | int i = 0; |
218 | boolean params = false; |
219 | for (ParameterInterface p : parameters) { |
220 | if (p.isValueSet()) { |
221 | if (!params) { |
222 | buff.append(" {"); |
223 | params = true; |
224 | } |
225 | buff.appendExceptFirst(", "); |
226 | Value v = p.getParamValue(); |
227 | buff.append(++i).append(": ").append(v.getTraceSQL()); |
228 | } |
229 | } |
230 | if (params) { |
231 | buff.append('}'); |
232 | } |
233 | return buff.toString(); |
234 | } |
235 | |
236 | /** |
237 | * Write a SQL statement with trace level INFO to the trace system. |
238 | * |
239 | * @param sql the SQL statement |
240 | * @param params the parameters used, in the for {1:...} |
241 | * @param count the update count |
242 | * @param time the time it took to run the statement in ms |
243 | */ |
244 | public void infoSQL(String sql, String params, int count, long time) { |
245 | if (!isEnabled(TraceSystem.INFO)) { |
246 | return; |
247 | } |
248 | StringBuilder buff = new StringBuilder(sql.length() + params.length() + 20); |
249 | buff.append(lineSeparator).append("/*SQL"); |
250 | boolean space = false; |
251 | if (params.length() > 0) { |
252 | // This looks like a bug, but it is intentional: |
253 | // If there are no parameters, the SQL statement is |
254 | // the rest of the line. If there are parameters, they |
255 | // are appended at the end of the line. Knowing the size |
256 | // of the statement simplifies separating the SQL statement |
257 | // from the parameters (no need to parse). |
258 | space = true; |
259 | buff.append(" l:").append(sql.length()); |
260 | } |
261 | if (count > 0) { |
262 | space = true; |
263 | buff.append(" #:").append(count); |
264 | } |
265 | if (time > 0) { |
266 | space = true; |
267 | buff.append(" t:").append(time); |
268 | } |
269 | if (!space) { |
270 | buff.append(' '); |
271 | } |
272 | buff.append("*/"). |
273 | append(StringUtils.javaEncode(sql)). |
274 | append(StringUtils.javaEncode(params)). |
275 | append(';'); |
276 | sql = buff.toString(); |
277 | traceWriter.write(TraceSystem.INFO, module, sql, null); |
278 | } |
279 | |
280 | /** |
281 | * Write a message with trace level DEBUG to the trace system. |
282 | * |
283 | * @param s the message |
284 | * @param params the parameters |
285 | */ |
286 | public void debug(String s, Object... params) { |
287 | if (isEnabled(TraceSystem.DEBUG)) { |
288 | s = MessageFormat.format(s, params); |
289 | traceWriter.write(TraceSystem.DEBUG, module, s, null); |
290 | } |
291 | } |
292 | |
293 | /** |
294 | * Write a message with trace level DEBUG to the trace system. |
295 | * |
296 | * @param s the message |
297 | */ |
298 | public void debug(String s) { |
299 | if (isEnabled(TraceSystem.DEBUG)) { |
300 | traceWriter.write(TraceSystem.DEBUG, module, s, null); |
301 | } |
302 | } |
303 | |
304 | /** |
305 | * Write a message with trace level DEBUG to the trace system. |
306 | * @param t the exception |
307 | * @param s the message |
308 | */ |
309 | public void debug(Throwable t, String s) { |
310 | if (isEnabled(TraceSystem.DEBUG)) { |
311 | traceWriter.write(TraceSystem.DEBUG, module, s, t); |
312 | } |
313 | } |
314 | |
315 | |
316 | /** |
317 | * Write Java source code with trace level INFO to the trace system. |
318 | * |
319 | * @param java the source code |
320 | */ |
321 | public void infoCode(String java) { |
322 | if (isEnabled(TraceSystem.INFO)) { |
323 | traceWriter.write(TraceSystem.INFO, module, lineSeparator + |
324 | "/**/" + java, null); |
325 | } |
326 | } |
327 | |
328 | /** |
329 | * Write Java source code with trace level DEBUG to the trace system. |
330 | * |
331 | * @param java the source code |
332 | */ |
333 | void debugCode(String java) { |
334 | if (isEnabled(TraceSystem.DEBUG)) { |
335 | traceWriter.write(TraceSystem.DEBUG, module, lineSeparator + |
336 | "/**/" + java, null); |
337 | } |
338 | } |
339 | |
340 | } |