00001
00002
00003
00004
00005 package utils;
00006
00007 import java.io.PrintStream;
00008 import java.text.SimpleDateFormat;
00009 import java.util.Calendar;
00010
00011
00012
00013
00014
00015
00016 public enum Log
00017 {
00018 ALL ( 0xFFFF, "All" ),
00019 AUDIO ( 0x0100, "Audio" ),
00020 VERB ( 0x0080, "Verbose" ),
00021 PDU ( 0x0040, "PDU" ),
00022 DEBUG ( 0x0020, "Debug" ),
00023 ATTN ( 0x0010, "Attention" ),
00024 TRACE ( 0x0008, "Trace" ),
00025 INFO ( 0x0004, "Info" ),
00026 WARN ( 0x0002, "Warning" ),
00027 ERROR ( 0x0001, "Error" );
00028
00029 public interface AttentionContext
00030 {
00031 public abstract void attention( String message );
00032 }
00033
00034
00035
00036
00037 private int mask;
00038
00039
00040
00041
00042 private String desc;
00043
00044
00045
00046
00047 private static Log showChannels = ERROR;
00048
00049
00050
00051
00052 public static PrintStream out = System.out;
00053
00054
00055
00056
00057 public static PrintStream err = System.err;
00058
00059
00060
00061
00062 public static AttentionContext attn = null;
00063
00064
00065
00066
00067 private Log( int mask, String desc )
00068 {
00069 this.mask = mask;
00070 this.desc = desc;
00071 }
00072
00073
00074
00075
00076 public static void setEnabled( Log channel, boolean on )
00077 {
00078 synchronized( showChannels )
00079 {
00080 if ( on ) {
00081 showChannels.mask |= channel.mask;
00082 } else {
00083 showChannels.mask &= ~channel.mask;
00084 }
00085 }
00086 }
00087
00088
00089
00090
00091 public static boolean isEnabled( Log channel )
00092 {
00093 synchronized( showChannels )
00094 {
00095 return ( showChannels.mask & channel.mask ) == channel.mask;
00096 }
00097 }
00098
00099
00100
00101
00102 private static void println( Log channel, String message )
00103 {
00104 synchronized( showChannels )
00105 {
00106 if ( ( showChannels.mask & channel.mask ) != channel.mask ) {
00107 return;
00108 }
00109 }
00110
00111 String prefix = nowMillis() + " " + channel.desc
00112 + " [" + Thread.currentThread().getName () + "] ";
00113
00114 PrintStream os = out;
00115 if ( channel == ERROR || channel == WARN || channel == ATTN ) {
00116 os = err;
00117 }
00118
00119 os.println( prefix + message );
00120 os.flush ();
00121 }
00122
00123
00124
00125
00126 public static void error( String string )
00127 {
00128 println( ERROR, string );
00129 }
00130
00131
00132
00133
00134 public static void warn( String string )
00135 {
00136 println( WARN, string );
00137 }
00138
00139
00140
00141
00142 public static void info( String string )
00143 {
00144 println( INFO, string );
00145 }
00146
00147
00148
00149
00150 public static void debug( String string )
00151 {
00152 println( DEBUG, string );
00153 }
00154
00155
00156
00157
00158 public static void attn( String string )
00159 {
00160 if ( attn == null ) {
00161 println( ATTN, string );
00162 return;
00163 }
00164
00165 synchronized( showChannels )
00166 {
00167 if ( ( showChannels.mask & ATTN.mask ) != ATTN.mask ) {
00168 return;
00169 }
00170 }
00171
00172 attn.attention( string );
00173 }
00174
00175
00176
00177
00178 public static void trace( String string )
00179 {
00180 println( TRACE, string );
00181 }
00182
00183
00184
00185
00186 public static void pdu( String string )
00187 {
00188 println( PDU, string );
00189 }
00190
00191
00192
00193
00194 public static void verb( String string )
00195 {
00196 println( VERB, string );
00197 }
00198
00199
00200
00201
00202 public static void audio( String string )
00203 {
00204 println( AUDIO, string );
00205 }
00206
00207
00208
00209
00210 public static void where ()
00211 {
00212 Exception e = new Exception( "Called from:" );
00213 e.printStackTrace ();
00214 }
00215
00216
00217
00218
00219
00220 public static void exception( Log channel, Exception ex )
00221 {
00222 synchronized( showChannels )
00223 {
00224 if ( ( showChannels.mask & channel.mask ) != channel.mask ) {
00225 return;
00226 }
00227 }
00228
00229 StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace ();
00230
00231 for ( int i = 0; i < stackTrace.length; ++i )
00232 {
00233 if ( stackTrace[i].getClassName().endsWith( "Log" )
00234 && stackTrace[i].getMethodName().equals( "loc" ) )
00235 {
00236
00237
00238 if ( ++i < stackTrace.length )
00239 {
00240 StringBuffer sb = new StringBuffer ();
00241 sb.append( stackTrace[i].getMethodName() )
00242 .append( " (" )
00243 .append( stackTrace[i].getFileName() )
00244 .append( ":" )
00245 .append( stackTrace[i].getLineNumber() )
00246 .append( ") " )
00247 .append( ex.toString () );
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263 println( channel, sb.toString () );
00264 return;
00265 }
00266
00267 break;
00268 }
00269 }
00270
00271 println( channel, ex.toString () );
00272 }
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 public static String toHex( byte[] array, int length, String separator )
00283 {
00284 length = Math.min( length, array.length );
00285
00286 StringBuffer ret = new StringBuffer( 64 );
00287
00288 for( int i = 0; i < length; ++i )
00289 {
00290 int value = ( array[i] + 0x100 ) & 0xFF;
00291 int hi = value >> 4;
00292 int low = value & 0x0F;
00293
00294 final char[] hexChars = {
00295 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
00296 'A', 'B', 'C', 'D', 'E', 'F'
00297 };
00298
00299 ret.append( hexChars[hi] ).append( hexChars[low] );
00300
00301 if ( separator != null ) {
00302 ret.append( separator );
00303 }
00304 }
00305 return ret.toString ();
00306 }
00307
00308
00309
00310
00311 public static String toHex( byte[] array, String separator )
00312 {
00313 return array == null ? "" : toHex( array, array.length, separator );
00314 }
00315
00316
00317
00318
00319 public static String toHex( byte[] array )
00320 {
00321 return array == null ? "" : toHex( array, array.length, null );
00322 }
00323
00324
00325
00326
00327 public static String nowDate ()
00328 {
00329 Calendar cal = Calendar.getInstance ();
00330 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
00331 return sdf.format( cal.getTime() );
00332 }
00333
00334
00335
00336
00337 public static String nowTime ()
00338 {
00339 Calendar cal = Calendar.getInstance ();
00340 SimpleDateFormat sdf = new SimpleDateFormat( "HH:mm:ss" );
00341 return sdf.format( cal.getTime() );
00342 }
00343
00344
00345
00346
00347 public static String now ()
00348 {
00349 Calendar cal = Calendar.getInstance ();
00350 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
00351 return sdf.format( cal.getTime() );
00352 }
00353
00354
00355
00356
00357 public static String nowMillis ()
00358 {
00359 Calendar cal = Calendar.getInstance ();
00360 SimpleDateFormat sdf = new SimpleDateFormat( "HH:mm:ss.SSS" );
00361 return sdf.format( cal.getTime() );
00362 }
00363
00364
00365
00366
00367
00368 public static String EscapeHTML( String str )
00369 {
00370 return str.replaceAll( "&", "&" )
00371 .replaceAll( "<", "<" )
00372 .replaceAll( ">", ">" )
00373 .replaceAll( "\"", """ )
00374 .replaceAll( "\f", "<br/>" );
00375 }
00376 }