[6152] | 1 | /* |
---|
| 2 | Copyright 2006 by Sean Luke |
---|
| 3 | Licensed under the Academic Free License version 3.0 |
---|
| 4 | See the file "LICENSE" for more information |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | package ec.util; |
---|
| 9 | import java.io.*; |
---|
| 10 | import java.util.zip.*; |
---|
| 11 | |
---|
| 12 | /* |
---|
| 13 | * Log.java |
---|
| 14 | * Created: Sun Aug 8 18:53:49 1999 |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | /** |
---|
| 18 | * Defines a log to which Output outputs. |
---|
| 19 | * |
---|
| 20 | * Logs can be <i>restarted</i> after a computer outage by using the |
---|
| 21 | * information stored in the LogRestarter |
---|
| 22 | * <tt>restarter</tt>. |
---|
| 23 | * |
---|
| 24 | * @author Sean Luke |
---|
| 25 | * @version 1.0 |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | public class Log implements Serializable |
---|
| 30 | { |
---|
| 31 | // basic log features |
---|
| 32 | |
---|
| 33 | /** The log's writer */ |
---|
| 34 | public transient PrintWriter writer; // the actual writer. |
---|
| 35 | |
---|
| 36 | /** A filename, if the writer writes to a file */ |
---|
| 37 | public File filename; // the filename to write to, if any |
---|
| 38 | |
---|
| 39 | /** Should the log post announcements? */ |
---|
| 40 | public boolean postAnnouncements; // will the log post announcements? |
---|
| 41 | |
---|
| 42 | // stuff for restarting |
---|
| 43 | |
---|
| 44 | /** The log's restarter */ |
---|
| 45 | public LogRestarter restarter; // resets the log |
---|
| 46 | |
---|
| 47 | /** Should the log repost all announcements on restart */ |
---|
| 48 | public boolean repostAnnouncementsOnRestart; // repost all announcements? |
---|
| 49 | |
---|
| 50 | /** If the log writes to a file, should it append to the file on restart, |
---|
| 51 | or should it overwrite the file? */ |
---|
| 52 | public boolean appendOnRestart; // append to a file or replace it? |
---|
| 53 | |
---|
| 54 | public boolean isLoggingToSystemOut; |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | // values for specifying logs based on System.out or System.err |
---|
| 58 | |
---|
| 59 | /** Specifies that the log should write to stdout (System.out) */ |
---|
| 60 | public final static int D_STDOUT = 0; |
---|
| 61 | |
---|
| 62 | /** Specifies that the log should write to stderr (System.err) */ |
---|
| 63 | public final static int D_STDERR = 1; |
---|
| 64 | |
---|
| 65 | protected void finalize() throws Throwable |
---|
| 66 | { |
---|
| 67 | // rarely happens though... :-( |
---|
| 68 | super.finalize(); |
---|
| 69 | if (writer!=null && !isLoggingToSystemOut) writer.close(); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | /** Creates a log to a given filename; this file may or may not |
---|
| 73 | be appended to on restart, depending on _appendOnRestart. If |
---|
| 74 | and only if the file is <i>not</i> appended to on restart, then |
---|
| 75 | announcements are reposted on restart.*/ |
---|
| 76 | |
---|
| 77 | public Log(File _filename, |
---|
| 78 | boolean _postAnnouncements, |
---|
| 79 | boolean _appendOnRestart) throws IOException |
---|
| 80 | { |
---|
| 81 | this(_filename, _postAnnouncements, _appendOnRestart, false); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | /** Creates a log to a given filename; this file may or may not |
---|
| 85 | be appended to on restart, depending on _appendOnRestart. If |
---|
| 86 | and only if the file is <i>not</i> appended to on restart, then |
---|
| 87 | announcements are reposted on restart. The file can be compressed |
---|
| 88 | with gzip, but you may not both gzip AND appendOnRestart. If gzipped, |
---|
| 89 | then .gz is automagically appended to the file name.*/ |
---|
| 90 | |
---|
| 91 | public Log(File _filename, |
---|
| 92 | boolean _postAnnouncements, |
---|
| 93 | boolean _appendOnRestart, |
---|
| 94 | boolean gzip) throws IOException |
---|
| 95 | { |
---|
| 96 | postAnnouncements = _postAnnouncements; |
---|
| 97 | repostAnnouncementsOnRestart = !_appendOnRestart; |
---|
| 98 | appendOnRestart = _appendOnRestart; |
---|
| 99 | isLoggingToSystemOut = false; |
---|
| 100 | |
---|
| 101 | if (gzip) |
---|
| 102 | { |
---|
| 103 | filename = new File(_filename.getAbsolutePath() + ".gz"); |
---|
| 104 | if (appendOnRestart) throw new IOException("Cannot gzip and appendOnRestart at the same time: new Log(File,int,boolean,boolean,boolean)"); |
---|
| 105 | |
---|
| 106 | writer = new PrintWriter(new OutputStreamWriter(new GZIPOutputStream( |
---|
| 107 | new BufferedOutputStream( |
---|
| 108 | new FileOutputStream(filename))))); |
---|
| 109 | restarter = new LogRestarter() |
---|
| 110 | { |
---|
| 111 | public Log restart(Log l) throws IOException |
---|
| 112 | { |
---|
| 113 | return reopen(l); |
---|
| 114 | } |
---|
| 115 | public Log reopen(Log l) throws IOException |
---|
| 116 | { |
---|
| 117 | if (l.writer!=null && !l.isLoggingToSystemOut) l.writer.close(); |
---|
| 118 | l.writer = new PrintWriter(new OutputStreamWriter(new GZIPOutputStream( |
---|
| 119 | new BufferedOutputStream(new FileOutputStream(l.filename))))); |
---|
| 120 | return l; |
---|
| 121 | } |
---|
| 122 | }; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | else |
---|
| 126 | { |
---|
| 127 | filename = _filename; |
---|
| 128 | writer = new PrintWriter(new BufferedWriter(new FileWriter(filename))); |
---|
| 129 | restarter = new LogRestarter() |
---|
| 130 | { |
---|
| 131 | public Log restart(Log l) throws IOException |
---|
| 132 | { |
---|
| 133 | l.writer = new PrintWriter(new BufferedWriter(new FileWriter(l.filename.getPath(),l.appendOnRestart))); |
---|
| 134 | return l; |
---|
| 135 | } |
---|
| 136 | public Log reopen(Log l) throws IOException |
---|
| 137 | { |
---|
| 138 | if (l.writer!=null && !isLoggingToSystemOut) l.writer.close(); |
---|
| 139 | l.writer = new PrintWriter(new BufferedWriter(new FileWriter(l.filename))); |
---|
| 140 | return l; |
---|
| 141 | } |
---|
| 142 | }; |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | /** Creates a log on stdout (descriptor == Log.D_STDOUT) |
---|
| 147 | or stderr (descriptor == Log.D_STDERR). */ |
---|
| 148 | |
---|
| 149 | public Log(int descriptor, |
---|
| 150 | boolean _postAnnouncements) |
---|
| 151 | { |
---|
| 152 | filename = null; |
---|
| 153 | postAnnouncements = _postAnnouncements; |
---|
| 154 | repostAnnouncementsOnRestart = true; |
---|
| 155 | appendOnRestart = true; // doesn't matter |
---|
| 156 | isLoggingToSystemOut = true; |
---|
| 157 | if (descriptor == D_STDOUT) |
---|
| 158 | { |
---|
| 159 | writer = new PrintWriter(System.out); |
---|
| 160 | restarter = new LogRestarter() |
---|
| 161 | { |
---|
| 162 | public Log restart(Log l) throws IOException |
---|
| 163 | { |
---|
| 164 | l.writer = new PrintWriter(System.out); |
---|
| 165 | return l; |
---|
| 166 | } |
---|
| 167 | public Log reopen(Log l) throws IOException |
---|
| 168 | { |
---|
| 169 | return l; // makes no sense |
---|
| 170 | } |
---|
| 171 | }; |
---|
| 172 | } |
---|
| 173 | else // D_STDERR |
---|
| 174 | { |
---|
| 175 | writer = new PrintWriter(System.err); |
---|
| 176 | restarter = new LogRestarter() |
---|
| 177 | { |
---|
| 178 | public Log restart(Log l) throws IOException |
---|
| 179 | { |
---|
| 180 | l.writer = new PrintWriter(System.err); |
---|
| 181 | return l; |
---|
| 182 | } |
---|
| 183 | public Log reopen(Log l) throws IOException |
---|
| 184 | { |
---|
| 185 | return l; // makes no sense |
---|
| 186 | } |
---|
| 187 | }; |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | /** Creates a log on a given Writer and custom LogRestarter. In general, |
---|
| 193 | You should not use this to write to a file. Use Log(_filename... |
---|
| 194 | instead. */ |
---|
| 195 | |
---|
| 196 | public Log(Writer _writer, |
---|
| 197 | LogRestarter _restarter, |
---|
| 198 | boolean _postAnnouncements, |
---|
| 199 | boolean _repostAnnouncementsOnRestart) |
---|
| 200 | { |
---|
| 201 | filename = null; |
---|
| 202 | postAnnouncements = _postAnnouncements; |
---|
| 203 | repostAnnouncementsOnRestart = _repostAnnouncementsOnRestart; |
---|
| 204 | appendOnRestart = true; // doesn't matter |
---|
| 205 | /* |
---|
| 206 | * Unfortunately, we can't know if the specified Writer wraps |
---|
| 207 | * System.out. Err on the side of caution and assume we're not. |
---|
| 208 | */ |
---|
| 209 | isLoggingToSystemOut = false; |
---|
| 210 | writer = new PrintWriter(new BufferedWriter(_writer)); |
---|
| 211 | restarter = _restarter; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | /** Restarts a log after a system restart from checkpoint. Returns |
---|
| 216 | the restarted log -- note that it may not be the same log! */ |
---|
| 217 | |
---|
| 218 | public Log restart() throws IOException |
---|
| 219 | { |
---|
| 220 | return restarter.restart(this); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | /** Forces a file-based log to reopen, erasing its previous contents. |
---|
| 224 | non-file logs ignore this. */ |
---|
| 225 | |
---|
| 226 | public Log reopen() throws IOException |
---|
| 227 | { |
---|
| 228 | return restarter.reopen(this); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | } |
---|
| 232 | |
---|