| 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.io.IOException; |
| 9 |
import java.io.PrintStream; |
| 10 |
import java.io.PrintWriter; |
| 11 |
import java.io.Writer; |
| 12 |
import java.text.SimpleDateFormat; |
| 13 |
import java.util.concurrent.atomic.AtomicReferenceArray; |
| 14 |
import org.h2.api.ErrorCode; |
| 15 |
import org.h2.engine.Constants; |
| 16 |
import org.h2.jdbc.JdbcException; |
| 17 |
import org.h2.store.fs.FileUtils; |
| 18 |
import org.h2.util.IOUtils; |
| 19 |
|
| 20 |
/** |
| 21 |
* The trace mechanism is the logging facility of this database. There is |
| 22 |
* usually one trace system per database. It is called 'trace' because the term |
| 23 |
* 'log' is already used in the database domain and means 'transaction log'. It |
| 24 |
* is possible to write after close was called, but that means for each write |
| 25 |
* the file will be opened and closed again (which is slower). |
| 26 |
*/ |
| 27 |
public class TraceSystem implements TraceWriter { |
| 28 |
|
| 29 |
/** |
| 30 |
* The parent trace level should be used. |
| 31 |
*/ |
| 32 |
public static final int PARENT = -1; |
| 33 |
|
| 34 |
/** |
| 35 |
* This trace level means nothing should be written. |
| 36 |
*/ |
| 37 |
public static final int OFF = 0; |
| 38 |
|
| 39 |
/** |
| 40 |
* This trace level means only errors should be written. |
| 41 |
*/ |
| 42 |
public static final int ERROR = 1; |
| 43 |
|
| 44 |
/** |
| 45 |
* This trace level means errors and informational messages should be |
| 46 |
* written. |
| 47 |
*/ |
| 48 |
public static final int INFO = 2; |
| 49 |
|
| 50 |
/** |
| 51 |
* This trace level means all type of messages should be written. |
| 52 |
*/ |
| 53 |
public static final int DEBUG = 3; |
| 54 |
|
| 55 |
/** |
| 56 |
* This trace level means all type of messages should be written, but |
| 57 |
* instead of using the trace file the messages should be written to SLF4J. |
| 58 |
*/ |
| 59 |
public static final int ADAPTER = 4; |
| 60 |
|
| 61 |
/** |
| 62 |
* The default level for system out trace messages. |
| 63 |
*/ |
| 64 |
public static final int DEFAULT_TRACE_LEVEL_SYSTEM_OUT = OFF; |
| 65 |
|
| 66 |
/** |
| 67 |
* The default level for file trace messages. |
| 68 |
*/ |
| 69 |
public static final int DEFAULT_TRACE_LEVEL_FILE = ERROR; |
| 70 |
|
| 71 |
/** |
| 72 |
* The default maximum trace file size. It is currently 64 MB. Additionally, |
| 73 |
* there could be a .old file of the same size. |
| 74 |
*/ |
| 75 |
private static final int DEFAULT_MAX_FILE_SIZE = 64 * 1024 * 1024; |
| 76 |
|
| 77 |
private static final int CHECK_SIZE_EACH_WRITES = 4096; |
| 78 |
|
| 79 |
private int levelSystemOut = DEFAULT_TRACE_LEVEL_SYSTEM_OUT; |
| 80 |
private int levelFile = DEFAULT_TRACE_LEVEL_FILE; |
| 81 |
private int levelMax; |
| 82 |
private int maxFileSize = DEFAULT_MAX_FILE_SIZE; |
| 83 |
private String fileName; |
| 84 |
private final AtomicReferenceArray<Trace> traces = |
| 85 |
new AtomicReferenceArray<>(Trace.MODULE_NAMES.length); |
| 86 |
private SimpleDateFormat dateFormat; |
| 87 |
private Writer fileWriter; |
| 88 |
private PrintWriter printWriter; |
| 89 |
/** |
| 90 |
* Starts at -1 so that we check the file size immediately upon open. This |
| 91 |
* Can be important if we open and close the trace file without managing to |
| 92 |
* have written CHECK_SIZE_EACH_WRITES bytes each time. |
| 93 |
*/ |
| 94 |
private int checkSize = -1; |
| 95 |
private boolean closed; |
| 96 |
private boolean writingErrorLogged; |
| 97 |
private TraceWriter writer = this; |
| 98 |
private PrintStream sysOut = System.out; |
| 99 |
|
| 100 |
/** |
| 101 |
* Create a new trace system object. |
| 102 |
* |
| 103 |
* @param fileName the file name |
| 104 |
*/ |
| 105 |
public TraceSystem(String fileName) { |
| 106 |
this.fileName = fileName; |
| 107 |
updateLevel(); |
| 108 |
} |
| 109 |
|
| 110 |
private void updateLevel() { |
| 111 |
levelMax = Math.max(levelSystemOut, levelFile); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Set the print stream to use instead of System.out. |
| 116 |
* |
| 117 |
* @param out the new print stream |
| 118 |
*/ |
| 119 |
public void setSysOut(PrintStream out) { |
| 120 |
this.sysOut = out; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Get or create a trace object for this module id. Trace modules with id |
| 125 |
* are cached. |
| 126 |
* |
| 127 |
* @param moduleId module id |
| 128 |
* @return the trace object |
| 129 |
*/ |
| 130 |
public Trace getTrace(int moduleId) { |
| 131 |
Trace t = traces.get(moduleId); |
| 132 |
if (t == null) { |
| 133 |
t = new Trace(writer, moduleId); |
| 134 |
if (!traces.compareAndSet(moduleId, null, t)) { |
| 135 |
t = traces.get(moduleId); |
| 136 |
} |
| 137 |
} |
| 138 |
return t; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Create a trace object for this module. Trace modules with names are not |
| 143 |
* cached. |
| 144 |
* |
| 145 |
* @param module the module name |
| 146 |
* @return the trace object |
| 147 |
*/ |
| 148 |
public Trace getTrace(String module) { |
| 149 |
return new Trace(writer, module); |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public boolean isEnabled(int level) { |
| 154 |
if (levelMax == ADAPTER) { |
| 155 |
return writer.isEnabled(level); |
| 156 |
} |
| 157 |
return level <= this.levelMax; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Set the trace file name. |
| 162 |
* |
| 163 |
* @param name the file name |
| 164 |
*/ |
| 165 |
public void setFileName(String name) { |
| 166 |
this.fileName = name; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Set the maximum trace file size in bytes. |
| 171 |
* |
| 172 |
* @param max the maximum size |
| 173 |
*/ |
| 174 |
public void setMaxFileSize(int max) { |
| 175 |
this.maxFileSize = max; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Set the trace level to use for System.out |
| 180 |
* |
| 181 |
* @param level the new level |
| 182 |
*/ |
| 183 |
public void setLevelSystemOut(int level) { |
| 184 |
levelSystemOut = level; |
| 185 |
updateLevel(); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Set the file trace level. |
| 190 |
* |
| 191 |
* @param level the new level |
| 192 |
*/ |
| 193 |
public void setLevelFile(int level) { |
| 194 |
if (level == ADAPTER) { |
| 195 |
String adapterClass = "org.h2.message.TraceWriterAdapter"; |
| 196 |
try { |
| 197 |
writer = (TraceWriter) Class.forName(adapterClass).getDeclaredConstructor().newInstance(); |
| 198 |
} catch (Throwable e) { |
| 199 |
e = DbException.get(ErrorCode.CLASS_NOT_FOUND_1, e, adapterClass); |
| 200 |
write(ERROR, Trace.DATABASE, adapterClass, e); |
| 201 |
return; |
| 202 |
} |
| 203 |
String name = fileName; |
| 204 |
if (name != null) { |
| 205 |
if (name.endsWith(Constants.SUFFIX_TRACE_FILE)) { |
| 206 |
name = name.substring(0, name.length() - Constants.SUFFIX_TRACE_FILE.length()); |
| 207 |
} |
| 208 |
int idx = Math.max(name.lastIndexOf('/'), name.lastIndexOf('\\')); |
| 209 |
if (idx >= 0) { |
| 210 |
name = name.substring(idx + 1); |
| 211 |
} |
| 212 |
writer.setName(name); |
| 213 |
} |
| 214 |
} |
| 215 |
levelFile = level; |
| 216 |
updateLevel(); |
| 217 |
} |
| 218 |
|
| 219 |
public int getLevelFile() { |
| 220 |
return levelFile; |
| 221 |
} |
| 222 |
|
| 223 |
private synchronized String format(String module, String s) { |
| 224 |
if (dateFormat == null) { |
| 225 |
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss "); |
| 226 |
} |
| 227 |
return dateFormat.format(System.currentTimeMillis()) + module + ": " + s; |
| 228 |
} |
| 229 |
|
| 230 |
@Override |
| 231 |
public void write(int level, int moduleId, String s, Throwable t) { |
| 232 |
write(level, Trace.MODULE_NAMES[moduleId], s, t); |
| 233 |
} |
| 234 |
|
| 235 |
@Override |
| 236 |
public void write(int level, String module, String s, Throwable t) { |
| 237 |
if (level <= levelSystemOut || level > this.levelMax) { |
| 238 |
// level <= levelSystemOut: the system out level is set higher |
| 239 |
// level > this.level: the level for this module is set higher |
| 240 |
sysOut.println(format(module, s)); |
| 241 |
if (t != null && levelSystemOut == DEBUG) { |
| 242 |
t.printStackTrace(sysOut); |
| 243 |
} |
| 244 |
} |
| 245 |
if (fileName != null) { |
| 246 |
if (level <= levelFile) { |
| 247 |
writeFile(format(module, s), t); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
private synchronized void writeFile(String s, Throwable t) { |
| 253 |
try { |
| 254 |
checkSize = (checkSize + 1) % CHECK_SIZE_EACH_WRITES; |
| 255 |
if (checkSize == 0) { |
| 256 |
closeWriter(); |
| 257 |
if (maxFileSize > 0 && FileUtils.size(fileName) > maxFileSize) { |
| 258 |
String old = fileName + ".old"; |
| 259 |
FileUtils.delete(old); |
| 260 |
FileUtils.move(fileName, old); |
| 261 |
} |
| 262 |
} |
| 263 |
if (!openWriter()) { |
| 264 |
return; |
| 265 |
} |
| 266 |
printWriter.println(s); |
| 267 |
if (t != null) { |
| 268 |
if (levelFile == ERROR && t instanceof JdbcException) { |
| 269 |
JdbcException se = (JdbcException) t; |
| 270 |
int code = se.getErrorCode(); |
| 271 |
if (ErrorCode.isCommon(code)) { |
| 272 |
printWriter.println(t); |
| 273 |
} else { |
| 274 |
t.printStackTrace(printWriter); |
| 275 |
} |
| 276 |
} else { |
| 277 |
t.printStackTrace(printWriter); |
| 278 |
} |
| 279 |
} |
| 280 |
printWriter.flush(); |
| 281 |
if (closed) { |
| 282 |
closeWriter(); |
| 283 |
} |
| 284 |
} catch (Exception e) { |
| 285 |
logWritingError(e); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
private void logWritingError(Exception e) { |
| 290 |
if (writingErrorLogged) { |
| 291 |
return; |
| 292 |
} |
| 293 |
writingErrorLogged = true; |
| 294 |
Exception se = DbException.get( |
| 295 |
ErrorCode.TRACE_FILE_ERROR_2, e, fileName, e.toString()); |
| 296 |
// print this error only once |
| 297 |
fileName = null; |
| 298 |
sysOut.println(se); |
| 299 |
se.printStackTrace(); |
| 300 |
} |
| 301 |
|
| 302 |
private boolean openWriter() { |
| 303 |
if (printWriter == null) { |
| 304 |
try { |
| 305 |
FileUtils.createDirectories(FileUtils.getParent(fileName)); |
| 306 |
if (FileUtils.exists(fileName) && !FileUtils.canWrite(fileName)) { |
| 307 |
// read only database: don't log error if the trace file |
| 308 |
// can't be opened |
| 309 |
return false; |
| 310 |
} |
| 311 |
fileWriter = IOUtils.getBufferedWriter( |
| 312 |
FileUtils.newOutputStream(fileName, true)); |
| 313 |
printWriter = new PrintWriter(fileWriter, true); |
| 314 |
} catch (Exception e) { |
| 315 |
logWritingError(e); |
| 316 |
return false; |
| 317 |
} |
| 318 |
} |
| 319 |
return true; |
| 320 |
} |
| 321 |
|
| 322 |
private synchronized void closeWriter() { |
| 323 |
if (printWriter != null) { |
| 324 |
printWriter.flush(); |
| 325 |
printWriter.close(); |
| 326 |
printWriter = null; |
| 327 |
} |
| 328 |
if (fileWriter != null) { |
| 329 |
try { |
| 330 |
fileWriter.close(); |
| 331 |
} catch (IOException e) { |
| 332 |
// ignore |
| 333 |
} |
| 334 |
fileWriter = null; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Close the writers, and the files if required. It is still possible to |
| 340 |
* write after closing, however after each write the file is closed again |
| 341 |
* (slowing down tracing). |
| 342 |
*/ |
| 343 |
public void close() { |
| 344 |
closeWriter(); |
| 345 |
closed = true; |
| 346 |
} |
| 347 |
|
| 348 |
@Override |
| 349 |
public void setName(String name) { |
| 350 |
// nothing to do (the file name is already set) |
| 351 |
} |
| 352 |
|
| 353 |
} |