KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > util > LogWriter


1 /**
2  * com.mckoi.util.LogWriter 19 Aug 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.util;
26
27 import java.io.*;
28
29 /**
30  * A Writer that writes information to a log file that archives old log
31  * entries when it goes above a certain size.
32  *
33  * @author Tobias Downer
34  */

35
36 public class LogWriter extends Writer {
37
38   /**
39    * The log file.
40    */

41   private final File log_file;
42
43   /**
44    * The maximum size of the log before it is archived.
45    */

46   private final long max_size;
47
48   /**
49    * The number of backup archives of log files.
50    */

51   private final int archive_count;
52
53   /**
54    * Current size of the log file.
55    */

56   private long log_file_size;
57
58   /**
59    * The log file FileWriter.
60    */

61   private Writer out;
62
63   /**
64    * Constructs the log writer. The 'base_name' is the name of log file.
65    * 'max_size' is the maximum size the file can grow to before it is
66    * copied to a log archive.
67    */

68   public LogWriter(File base_name, long max_size, int archive_count)
69                                                           throws IOException {
70
71     if (archive_count < 1) {
72       throw new Error JavaDoc("'archive_count' must be 1 or greater.");
73     }
74
75     this.log_file = base_name;
76     this.max_size = max_size;
77     this.archive_count = archive_count;
78
79     // Does the file exist?
80
if (base_name.exists()) {
81       log_file_size = base_name.length();
82     }
83     else {
84       log_file_size = 0;
85     }
86     out = new BufferedWriter(new FileWriter(base_name.getPath(), true));
87
88   }
89
90   /**
91    * Checks the size of the file, and if it has reached or exceeded the
92    * maximum limit then archive the log.
93    */

94   private void checkLogSize() throws IOException {
95     if (log_file_size > max_size) {
96       // Flush to the log file,
97
out.flush();
98       // Close it,
99
out.close();
100       out = null;
101       // Delete the top archive,
102
File top = new File(log_file.getPath() + "." + archive_count);
103       top.delete();
104       // Rename backup archives,
105
for (int i = archive_count - 1; i > 0; --i) {
106         File source = new File(log_file.getPath() + "." + i);
107         File dest = new File(log_file.getPath() + "." + (i + 1));
108         source.renameTo(dest);
109       }
110       log_file.renameTo(new File(log_file.getPath() + ".1"));
111
112       // Create the new empty log file,
113
out = new BufferedWriter(new FileWriter(log_file.getPath(), 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 JavaDoc 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 }
148
Popular Tags