001: /**
002: * com.mckoi.util.LogWriter 19 Aug 2000
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.util;
024:
025: import java.io.*;
026:
027: /**
028: * A Writer that writes information to a log file that archives old log
029: * entries when it goes above a certain size.
030: *
031: * @author Tobias Downer
032: */
033:
034: public class LogWriter extends Writer {
035:
036: /**
037: * The log file.
038: */
039: private final File log_file;
040:
041: /**
042: * The maximum size of the log before it is archived.
043: */
044: private final long max_size;
045:
046: /**
047: * The number of backup archives of log files.
048: */
049: private final int archive_count;
050:
051: /**
052: * Current size of the log file.
053: */
054: private long log_file_size;
055:
056: /**
057: * The log file FileWriter.
058: */
059: private Writer out;
060:
061: /**
062: * Constructs the log writer. The 'base_name' is the name of log file.
063: * 'max_size' is the maximum size the file can grow to before it is
064: * copied to a log archive.
065: */
066: public LogWriter(File base_name, long max_size, int archive_count)
067: throws IOException {
068:
069: if (archive_count < 1) {
070: throw new Error("'archive_count' must be 1 or greater.");
071: }
072:
073: this .log_file = base_name;
074: this .max_size = max_size;
075: this .archive_count = archive_count;
076:
077: // Does the file exist?
078: if (base_name.exists()) {
079: log_file_size = base_name.length();
080: } else {
081: log_file_size = 0;
082: }
083: out = new BufferedWriter(new FileWriter(base_name.getPath(),
084: true));
085:
086: }
087:
088: /**
089: * Checks the size of the file, and if it has reached or exceeded the
090: * maximum limit then archive the log.
091: */
092: private void checkLogSize() throws IOException {
093: if (log_file_size > max_size) {
094: // Flush to the log file,
095: out.flush();
096: // Close it,
097: out.close();
098: out = null;
099: // Delete the top archive,
100: File top = new File(log_file.getPath() + "."
101: + archive_count);
102: top.delete();
103: // Rename backup archives,
104: for (int i = archive_count - 1; i > 0; --i) {
105: File source = new File(log_file.getPath() + "." + i);
106: File dest = new File(log_file.getPath() + "." + (i + 1));
107: source.renameTo(dest);
108: }
109: log_file.renameTo(new File(log_file.getPath() + ".1"));
110:
111: // Create the new empty log file,
112: out = new BufferedWriter(new FileWriter(log_file.getPath(),
113: true));
114: log_file_size = 0;
115: }
116: }
117:
118: // ---------- Implemented from Writer ----------
119:
120: public synchronized void write(int c) throws IOException {
121: out.write(c);
122: ++log_file_size;
123: }
124:
125: public synchronized void write(char cbuf[], int off, int len)
126: throws IOException {
127: out.write(cbuf, off, len);
128: log_file_size += len;
129: }
130:
131: public synchronized void write(String str, int off, int len)
132: throws IOException {
133: out.write(str, off, len);
134: log_file_size += len;
135: }
136:
137: public synchronized void flush() throws IOException {
138: out.flush();
139: checkLogSize();
140: }
141:
142: public synchronized void close() throws IOException {
143: out.flush();
144: out.close();
145: }
146:
147: }
|