1 21 22 23 package com.ice.util; 24 25 import java.io.*; 26 import java.lang.*; 27 import java.util.*; 28 29 30 public class 31 FileLog extends Object 32 { 33 private static final String RCS_ID = "$Id: FileLog.java,v 1.2 2004/03/05 12:20:31 deniger Exp $"; 34 private static final String RCS_REV = "$Revision: 1.2 $"; 35 private static final String RCS_NAME = "$Name: $"; 36 37 private static final String DEFAULT_FILENAME = "log.txt"; 38 39 private static FileLog defaultLogger= null; 40 41 private String filename; 42 private FileWriter file; 43 private PrintWriter stream; 44 private boolean open; 45 private boolean echo; 46 private boolean autoFlush; 47 48 49 static public FileLog 50 getDefaultLogger() 51 { 52 return FileLog.defaultLogger; 53 } 54 55 static public FileLog 56 setDefaultLogger( FileLog logger ) 57 { 58 FileLog old = FileLog.defaultLogger; 59 FileLog.defaultLogger = logger; 60 return old; 61 } 62 63 static public void 64 checkDefaultLogger() 65 { 66 if ( FileLog.defaultLogger == null ) 67 { 68 FileLog.defaultLogger = 69 new FileLog( FileLog.DEFAULT_FILENAME ); 70 } 71 } 72 73 public 74 FileLog( String filename ) 75 { 76 this.open = false; 77 this.echo = false; 78 this.autoFlush = true; 79 this.filename = filename; 80 this.file = null; 81 this.stream = null; 82 } 83 84 public void 85 setLogFilename( String filename ) 86 { 87 this.filename = filename; 88 } 89 90 public void 91 setAutoFlush( boolean autoflush ) 92 { 93 this.autoFlush = autoflush; 94 } 95 96 public void 97 checkLogOpen() 98 { 99 if ( ! this.open ) 100 { 101 this.openLogFile(); 102 } 103 } 104 105 public void 106 openLogFile() 107 { 108 boolean isok = true; 109 110 try { 111 this.file = new FileWriter( this.filename ); 112 } 113 catch ( Exception ex ) 114 { 115 System.err.println 116 ( "error opening log file '" + this.filename 117 + "' - " + ex.getMessage() ); 118 this.file = null; 119 isok = false; 120 } 121 122 if ( isok ) 123 { 124 this.stream = new PrintWriter( this.file ); 125 this.open = true; 126 } 127 128 this.echo = false; 129 } 130 131 public void 132 closeLog() 133 { 134 if ( this.open ) 135 { 136 this.open = false; 137 if ( this.stream != null ) 138 { 139 this.stream.flush(); 140 this.stream.close(); 141 this.stream = null; 142 } 143 } 144 } 145 146 public void 147 setEcho( boolean setting ) 148 { 149 this.echo = setting; 150 } 151 152 public void 153 traceMsg( Throwable thrown, String msg ) 154 { 155 this.logMsg( msg ); 156 this.logMsg( thrown.getMessage() ); 157 158 if ( ! this.open ) 159 thrown.printStackTrace( System.err ); 160 else 161 thrown.printStackTrace( this.stream ); 162 163 if ( this.autoFlush && this.open ) 164 this.stream.flush(); 165 } 166 167 168 static public void 169 defLogMsg( String msg ) 170 { 171 FileLog.checkDefaultLogger(); 172 if ( FileLog.defaultLogger != null ) 173 FileLog.defaultLogger.logMsg( msg ); 174 } 175 176 public void 177 logMsg( String msg ) 178 { 179 this.checkLogOpen(); 180 181 if ( this.open ) 182 { 183 this.stream.println( msg ); 184 if ( this.autoFlush && this.open ) 185 this.stream.flush(); 186 } 187 188 if ( this.echo ) 189 { 190 System.out.println( msg ); 191 } 192 } 193 194 static public void 195 defLogMsgStdout( String msg ) 196 { 197 FileLog.checkDefaultLogger(); 198 if ( FileLog.defaultLogger != null ) 199 FileLog.defaultLogger.logMsgStdout( msg ); 200 } 201 202 public void 203 logMsgStdout( String msg ) 204 { 205 this.checkLogOpen(); 206 207 if ( this.open ) 208 { 209 this.stream.println( msg ); 210 if ( this.autoFlush && this.open ) 211 this.stream.flush(); 212 } 213 214 System.out.println( msg ); 215 } 216 217 static public void 218 defLogMsgStderr( String msg ) 219 { 220 FileLog.checkDefaultLogger(); 221 if ( FileLog.defaultLogger != null ) 222 FileLog.defaultLogger.logMsgStderr( msg ); 223 } 224 225 public void 226 logMsgStderr( String msg ) 227 { 228 this.checkLogOpen(); 229 230 if ( this.open ) 231 { 232 this.stream.println( msg ); 233 if ( this.autoFlush && this.open ) 234 this.stream.flush(); 235 } 236 237 System.err.println( msg ); 238 } 239 } 240 241 242 243 | Popular Tags |