private void checkLogSize() throws SQLException { for (String name : FileUtils.listFiles(getTestDir(""))) { if (name.startsWith("logfile") && name.endsWith(".log.db")) { long length = FileUtils.length(name); assertSmaller(length, MAX_LOG_SIZE * 1024 * 1024 * 2); } } }
private boolean openWriter() { if (printWriter == null) { try { FileUtils.createDirs(fileName); if (FileUtils.exists(fileName) && FileUtils.isReadOnly(fileName)) { // read only database: don't log error if the trace file // can't be opened return false; } fileWriter = IOUtils.getWriter(FileUtils.openFileOutputStream(fileName, true)); printWriter = new PrintWriter(fileWriter, true); } catch (Exception e) { logWritingError(e); return false; } } return true; }
private synchronized void closeWriter() { if (printWriter != null) { printWriter.flush(); printWriter.close(); printWriter = null; } if (fileWriter != null) { try { fileWriter.close(); } catch (IOException e) { // ignore } fileWriter = null; } if (fileName != null && FileUtils.length(fileName) == 0) { FileUtils.tryDelete(fileName); } }
private synchronized void writeFile(String s, Throwable t) { try { if (checkSize++ >= CHECK_SIZE_EACH_WRITES) { checkSize = 0; closeWriter(); if (maxFileSize > 0 && FileUtils.length(fileName) > maxFileSize) { String old = fileName + ".old"; if (FileUtils.exists(old)) { FileUtils.delete(old); } FileUtils.rename(fileName, old); } } if (!openWriter()) { return; } printWriter.println(s); if (t != null) { if (levelFile == ERROR && t instanceof JdbcSQLException) { JdbcSQLException se = (JdbcSQLException) t; int code = se.getErrorCode(); if (ErrorCode.isCommon(code)) { printWriter.println(t.toString()); } else { t.printStackTrace(printWriter); } } else { t.printStackTrace(printWriter); } } printWriter.flush(); if (closed) { closeWriter(); } } catch (Exception e) { logWritingError(e); } }
private void testFile(DataOutputStream out) throws IOException { File file = new File("test.txt"); if (file.exists()) { file.delete(); } RandomAccessFile write = new RandomAccessFile(file, "rws"); // RandomAccessFile write = new RandomAccessFile(file, "rwd"); int fileSize = 10 * 1024 * 1024; FileUtils.setLength(write, fileSize); write.seek(0); int i = 0; FileDescriptor fd = write.getFD(); while (true) { if (write.getFilePointer() >= fileSize) { break; } write.writeBytes(i + "\r\n"); fd.sync(); out.writeInt(i); out.flush(); i++; } write.close(); }