• Main Page
  • Related Pages
  • Packages
  • Classes
  • Files
  • File List

utils/Log.java

Go to the documentation of this file.
00001 
00002 /**
00003  *  Utilities to handle base64 encoding, log and octet buffers
00004  */
00005 package utils;
00006 
00007 import java.io.PrintStream;
00008 import java.text.SimpleDateFormat;
00009 import java.util.Calendar;
00010 
00011 /**
00012  *  Common application message logger facility (static implementation)
00013  *  
00014  *  @author Mikica B Kocic
00015  */
00016 public enum Log
00017 {
00018     /** Log all messages   */  ALL     ( 0xFFFF, "All" ),
00019     /** Log audio messages */  AUDIO   ( 0x0100, "Audio" ),
00020     /** Log verbose PDUs   */  VERB    ( 0x0080, "Verbose" ),
00021     /** Log binary PDUs    */  PDU     ( 0x0040, "PDU" ),
00022     /** Log debug messages */  DEBUG   ( 0x0020, "Debug" ),
00023     /** Log attn messages  */  ATTN    ( 0x0010, "Attention" ),
00024     /** Log program trace  */  TRACE   ( 0x0008, "Trace" ),
00025     /** Log information    */  INFO    ( 0x0004, "Info" ),
00026     /** Log warnings       */  WARN    ( 0x0002, "Warning" ),
00027     /** Log errors         */  ERROR   ( 0x0001, "Error" );
00028     
00029     public interface AttentionContext
00030     {
00031         public abstract void attention( String message );
00032     }
00033     
00034     /**
00035      *  Log channel bitmap
00036      */
00037     private int mask;
00038     
00039     /**
00040      *  Log channel description
00041      */
00042     private String desc;
00043 
00044     /**
00045      *  Current channels to be logged. By default only the Error channel is enabled.
00046      */
00047     private static Log showChannels = ERROR;
00048 
00049     /**
00050      *  Print stream for standard messages
00051      */
00052     public static PrintStream out = System.out;
00053     
00054     /**
00055      *  Print stream for error messages
00056      */
00057     public static PrintStream err = System.err;
00058     
00059     /**
00060      *  Instance of the interface for attention messages
00061      */
00062     public static AttentionContext attn = null;
00063 
00064     /**
00065      *  Private constructor that forbids instantiation by the user
00066      */
00067     private Log( int mask, String desc ) 
00068     {
00069         this.mask = mask;
00070         this.desc = desc;
00071         }
00072 
00073     /**
00074      *  Enables/disables the Log channel 
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      *  Returns if log channel is enabled
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      *  Logs message with prefix and time-stamp
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      * Logs a warning message.
00125      */
00126     public static void error( String string ) 
00127     {
00128         println( ERROR, string );
00129     }
00130     
00131     /**
00132      * Logs a warning message.
00133      */
00134     public static void warn( String string ) 
00135     {
00136         println( WARN, string );
00137     }
00138 
00139     /**
00140      * Logs a informational message.
00141      */
00142     public static void info( String string )
00143     {
00144         println( INFO, string );
00145     }
00146 
00147     /**
00148      * Logs a debug message.
00149      */
00150     public static void debug( String string )
00151     {
00152         println( DEBUG, string );
00153     }
00154 
00155     /**
00156      * Logs a attention message.
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      * Logs a program trace message.
00177      */
00178     public static void trace( String string )
00179     {
00180         println( TRACE, string );
00181     }
00182 
00183     /**
00184      * Logs a protocol data unit
00185      */
00186     public static void pdu( String string )
00187     {
00188         println( PDU, string );
00189     }
00190 
00191     /**
00192      * Logs a verbose message.
00193      */
00194     public static void verb( String string )
00195     {
00196         println( VERB, string );
00197     }
00198 
00199     /**
00200      * Logs a audio message.
00201      */
00202     public static void audio( String string )
00203     {
00204         println( AUDIO, string );
00205     }
00206 
00207     /**
00208      * Prints where this message was called from, via a stack trace.
00209      */
00210     public static void where ()
00211     {
00212         Exception e = new Exception( "Called from:" );
00213         e.printStackTrace ();
00214     }
00215     
00216     /**
00217      *  Returns current callee's name of the method, file, location and
00218      *  description of the exception.
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" ) ) // This is us. 
00235             {
00236                 /* Next is callee 
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                     /* Next is the list of callee's callee
00250                     ++i;
00251                     for( int j = 0; i < stackTrace.length && stackTrace[i].getFileName() != null; ++i, ++j ) 
00252                     {
00253                         sb.append( j == 0 ? "\nStack: " : ", " );
00254                         sb.append( stackTrace[i].getMethodName() )
00255                           .append( " (" )
00256                           .append( stackTrace[i].getFileName() )
00257                           .append( ":" )
00258                           .append( stackTrace[i].getLineNumber() )
00259                           .append( ")" );
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      *  Converts a byte array into a hex string, using the specified separator.
00276      *
00277      *  @param array     The byte array
00278      *  @param length    The length to print
00279      *  @param separator The separator (may be null)
00280      *  @return          The hex character string
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      *  Converts a byte array into a hex string, using the specified separator.
00310      */
00311     public static String toHex( byte[] array, String separator )
00312     {
00313         return array == null ? "" : toHex( array, array.length, separator );
00314     }
00315     
00316     /**
00317      *  Converts a byte array into a hex string.
00318      */
00319     public static String toHex( byte[] array )
00320     {
00321         return array == null ? "" : toHex( array, array.length, null );
00322     }
00323     
00324     /**
00325      *  Gets current time stamp -- date only
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      *  Gets current time stamp -- time only
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      *  Gets current time stamp -- date and time in ISO format 
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      *  Gets current time stamp -- time with milliseconds
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      *  Escapes HTML reserved characters (as we are logging HTML tagged messages)
00366      *  It also replaces form-feed characters with HTML line-breaks. 
00367      */
00368     public static String EscapeHTML( String str )
00369     {
00370         return str.replaceAll( "&",  "&amp;"  ) 
00371                   .replaceAll( "<",  "&lt;"   )
00372                   .replaceAll( ">",  "&gt;"   ) 
00373                   .replaceAll( "\"", "&quot;" )
00374                   .replaceAll( "\f", "<br/>" );
00375     }
00376 }

Generated on Thu Dec 16 2010 14:44:42 for VoIP Kryptofon by  doxygen 1.7.2