1 30 31 32 package org.hsqldb.persist; 33 34 import org.hsqldb.Database; 35 import org.hsqldb.HsqlException; 36 import org.hsqldb.NumberSequence; 37 import org.hsqldb.Session; 38 import org.hsqldb.Table; 39 import org.hsqldb.Trace; 40 import org.hsqldb.lib.SimpleLog; 41 42 44 64 public class Logger { 65 66 public SimpleLog appLog; 67 68 71 private Log log; 72 73 77 private LockFile lf; 78 boolean needsCheckpoint; 79 private boolean logStatements; 80 private boolean syncFile = false; 81 82 public Logger() { 83 appLog = new SimpleLog(null, SimpleLog.LOG_NONE, false); 84 } 85 86 97 public void openLog(Database db) throws HsqlException { 98 99 needsCheckpoint = false; 100 101 String path = db.getPath(); 102 int loglevel = db.getProperties().getIntegerProperty( 103 HsqlDatabaseProperties.hsqldb_applog, 0); 104 105 if (loglevel != SimpleLog.LOG_NONE) { 106 appLog = new SimpleLog(path + ".app.log", loglevel, 107 !db.isFilesReadOnly()); 108 } 109 110 appLog.sendLine(SimpleLog.LOG_ERROR, "Database (re)opened"); 111 112 logStatements = false; 113 114 if (!db.isFilesReadOnly()) { 115 acquireLock(path); 116 } 117 118 log = new Log(db); 119 120 log.open(); 121 122 logStatements = !db.isFilesReadOnly(); 123 } 124 125 127 148 public boolean closeLog(int closemode) { 149 150 if (log == null) { 151 appLog.sendLine(SimpleLog.LOG_ERROR, "Database closed"); 152 appLog.close(); 153 154 return true; 155 } 156 157 try { 158 switch (closemode) { 159 160 case Database.CLOSEMODE_IMMEDIATELY : 161 log.shutdown(); 162 break; 163 164 case Database.CLOSEMODE_NORMAL : 165 log.close(false); 166 break; 167 168 case Database.CLOSEMODE_COMPACT : 169 case Database.CLOSEMODE_SCRIPT : 170 log.close(true); 171 break; 172 } 173 } catch (Throwable e) { 174 appLog.logContext(e, "error closing log"); 175 appLog.close(); 176 177 log = null; 178 179 return false; 180 } 181 182 appLog.sendLine(SimpleLog.LOG_ERROR, "Database closed"); 183 appLog.close(); 184 185 log = null; 186 187 return true; 188 } 189 190 200 public boolean hasLog() { 201 return log != null; 202 } 203 204 207 public DataFileCache getCache() throws HsqlException { 208 209 if (log == null) { 210 return null; 211 } else { 212 return log.getCache(); 213 } 214 } 215 216 219 public boolean hasCache() { 220 221 if (log == null) { 222 return false; 223 } else { 224 return log.hasCache(); 225 } 226 } 227 228 237 public synchronized void logConnectUser(Session session) 238 throws HsqlException { 239 240 if (logStatements) { 241 writeToLog(session, session.getUser().getConnectStatement()); 242 } 243 } 244 245 254 public synchronized void writeToLog(Session session, 255 String statement) 256 throws HsqlException { 257 258 if (logStatements && log != null) { 259 log.writeStatement(session, statement); 260 } 261 } 262 263 public synchronized void writeInsertStatement(Session session, 264 Table table, Object [] row) throws HsqlException { 265 266 if (logStatements) { 267 log.writeInsertStatement(session, table, row); 268 } 269 } 270 271 public synchronized void writeDeleteStatement(Session session, Table t, 272 Object [] row) throws HsqlException { 273 274 if (logStatements) { 275 log.writeDeleteStatement(session, t, row); 276 } 277 } 278 279 public synchronized void writeSequenceStatement(Session session, 280 NumberSequence s) throws HsqlException { 281 282 if (logStatements) { 283 log.writeSequenceStatement(session, s); 284 } 285 } 286 287 public synchronized void writeCommitStatement(Session session) 288 throws HsqlException { 289 290 if (logStatements) { 291 log.writeCommitStatement(session); 292 synchLog(); 293 } 294 } 295 296 299 public synchronized void synchLog() { 300 301 if (logStatements && syncFile) { 302 log.synchLog(); 303 } 304 } 305 306 public synchronized void synchLogForce() { 307 308 if (logStatements) { 309 log.synchLog(); 310 } 311 } 312 313 327 public synchronized void checkpoint(boolean mode) throws HsqlException { 328 329 if (logStatements) { 330 appLog.logContext(appLog.LOG_NORMAL, "start"); 331 332 needsCheckpoint = false; 333 334 log.checkpoint(mode); 335 appLog.logContext(appLog.LOG_NORMAL, "end"); 336 } 337 } 338 339 345 public synchronized void setLogSize(int megas) { 346 347 if (log != null) { 348 log.setLogSize(megas); 349 } 350 } 351 352 358 public synchronized void setScriptType(int i) throws HsqlException { 359 360 if (log != null) { 361 log.setScriptType(i); 362 } 363 } 364 365 378 public synchronized void setWriteDelay(int delay) { 379 380 if (log != null) { 381 syncFile = (delay == 0); 382 383 log.setWriteDelay(delay); 384 } 385 } 386 387 public int getWriteDelay() { 388 return log != null ? log.getWriteDelay() 389 : 0; 390 } 391 392 public int getLogSize() { 393 return log != null ? log.getLogSize() 394 : 0; 395 } 396 397 public int getScriptType() { 398 return log != null ? log.getScriptType() 399 : 0; 400 } 401 402 405 public DataFileCache openTextCache(Table table, String source, 406 boolean readOnlyData, 407 boolean reversed) 408 throws HsqlException { 409 return log.openTextCache(table, source, readOnlyData, reversed); 410 } 411 412 415 public void closeTextCache(Table table) throws HsqlException { 416 log.closeTextCache(table); 417 } 418 419 public boolean needsCheckpoint() { 420 return needsCheckpoint; 421 } 422 423 426 public void acquireLock(String path) throws HsqlException { 427 428 if (lf != null) { 429 return; 430 } 431 432 lf = LockFile.newLockFileLock(path); 433 } 434 435 public void releaseLock() { 436 437 try { 438 if (lf != null) { 439 lf.tryRelease(); 440 } 441 } catch (Exception e) { 442 if (Trace.TRACE) { 443 Trace.printSystemOut(e.toString()); 444 } 445 } 446 447 lf = null; 448 } 449 } 450 | Popular Tags |