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