public void enableFileMode() { if (!isFileModeActive()) { if (ntClient == null) throw new NullPointerException("NTClient cannot be null (Logger.enableFileMode())"); try { String folderPath = ntClient.getPreferences().getDataFolderPath(); if (folderPath == null) throw new Exception("No accessible data folder"); logFilePath = folderPath + "Debug_"; if (ntClient.isRestartingModeEnabled()) { logFilePath += "RUNNING.log"; // will be renamed after last run logFileWriter = ntClient.getFileWriter(logFilePath); if (ntClient.isFirstRun() && logFileWriter .fileExists()) { // This is a log file of an older unresumed running session, // let's close it: debug("Found unresumed \"RUNNING\" log file, renaming"); long lastChanged = logFileWriter.fileLastChanged(); logFileWriter.open( FileIO.FILE_EXISTS_STRATEGY_APPEND, FileIO.FILE_DOES_NOT_EXIST_STRATEGY_CREATE); logFileWriter.writeLine( "[LATER: " + StringUtils.formatDateTime(System.currentTimeMillis(), "/", ":", " ") + "] Session was never resumed, closing."); logFileWriter.rename( "Debug_" + StringUtils.formatDateTime(lastChanged, "-", "", "T") + ".log", FileIO.FILE_EXISTS_STRATEGY_REPLACE); logFileWriter.dispose(); // also calls close() // Again take the (new) log file: logFileWriter = ntClient.getFileWriter(logFilePath); } logFileWriter.open( FileIO.FILE_EXISTS_STRATEGY_APPEND, FileIO.FILE_DOES_NOT_EXIST_STRATEGY_CREATE); } else { logFilePath += StringUtils.formatDateTime(System.currentTimeMillis(), "-", "", "T") + ".log"; logFileWriter = ntClient.getFileWriter(logFilePath); logFileWriter.open( FileIO.FILE_EXISTS_STRATEGY_CREATE_RENAMED_FILE, FileIO.FILE_DOES_NOT_EXIST_STRATEGY_CREATE); } // write log buffer: logFileWriter.write(getBuffer(true)); // debug("Logger file mode enabled"); } catch (Exception e) { disableFileMode(); error(e, "Failed to enable file mode in Logger"); } } }
public void disableFileMode() { if (isFileModeActive()) { try { logFileWriter.close(); if (ntClient.isRestartingModeEnabled() && ntClient.isLastRun()) logFileWriter.rename( "Debug_" + StringUtils.formatDateTime(System.currentTimeMillis(), "-", "", "T") + ".log", FileIO.FILE_EXISTS_STRATEGY_CREATE_RENAMED_FILE); // rename file to seal it logFileWriter.dispose(); } catch (Exception ignore) { } finally { logFileWriter = null; logFilePath = null; } } }