Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/vrspace/util/Logger.java


1   package org.vrspace.util;
2   
3   import java.util.*;
4   import java.io.*;
5   import java.lang.reflect.*;
6   
7   /**
8   First created Logger is accessible from static methods.
9   Other loggers are not tested.
10  */
11  public class Logger implements Runnable {
12    static boolean isLogHeader=true;
13    static boolean isLogInfo=true;
14    static boolean isLogDebug=true;
15    static boolean isLogError=true;
16    static boolean isLogWarning=true;
17    static boolean isVerbose=true;
18    static boolean stopOnError=false;
19    protected static int logLevel = Integer.MAX_VALUE;
20    //static PrintStream out;
21    static PrintWriter out = new PrintWriter( System.out );
22    //static StringBuffer buffer = new StringBuffer();
23    protected boolean active = true;
24    static Thread thread;
25    protected static boolean stop = false;
26  
27    public static final int ERROR = 0;
28    public static final int WARNING  = 1;
29    public static final int INFO = 2;
30    public static final int DEBUG = 3;
31    static final String[] desc = { "ERROR", "WARNING", "INFO", "DEBUG" };
32    static Calendar calendar = Calendar.getInstance();
33    protected static Logger logger = new Logger();
34    
35    public Logger() {
36      if (logger == null) {
37        //buffer=new StringBuffer();
38        logger=this;
39        (thread = new Thread( this, "Logger" )).start();
40      } else {
41        (new Thread( this, "Logger" )).start();
42      }
43    }
44    /**
45    Creates Logger writing to <b>out</b>
46    */
47    public Logger( PrintWriter out ) {
48      if (logger == null) {
49        //this.out=out;
50        this.out = new PrintWriter( out );
51        //buffer=new StringBuffer();
52        logger=this;
53        (thread = new Thread( this, "Logger" )).start();
54      } else {
55        (new Thread( this, "Logger" )).start();
56      }
57    }
58    /**
59     * Stop the main logger.
60     */
61    public static void stopStaticLogger() {
62      logger.active = false;
63    } 
64    /**
65     * Start the main logger if it hasn't been started yet.
66     */
67    public static void startStaticLogger() {
68      if ( ( logger == null ) || ( ! thread.isAlive() ) ) {
69        logger = null;
70        new Logger();
71      } 
72    }
73    /**
74    main loop
75    */
76    public void run() {
77      try {
78        while ( active ) {
79          try {
80            flush();
81            if ( stop ) {
82              System.exit(1);
83            }
84            Thread.sleep( 1000 );
85          } catch ( InterruptedException e ) {
86            active = false;
87          }
88        }
89      } catch ( Throwable e ) {
90        //Logger.logError( e );
91        e.printStackTrace();
92        active = false;
93      }
94      
95      //logDebug( "Logger Finished" );
96      flush();
97    }
98  
99    void flush() {
100     /*
101     if ( buffer.length() > 0 ) {
102       if ( isVerbose ) {
103         //Logger.logDebug(buffer.toString());
104         System.out.print(buffer.toString());
105       }
106       //if ( out instanceof PrintStream ) {
107       if ( out instanceof PrintWriter ) {
108         //out.println(buffer.toString());
109         out.print(buffer.toString());
110       }
111       buffer = new StringBuffer();
112     }
113     */
114     out.flush();
115   }
116   /*
117   static void log( String type, String line ) {
118     Calendar c=Calendar.getInstance();
119     buffer.append(type+" "+c.get(c.YEAR)+"/"+c.get(c.MONTH)+"/"+c.get(c.DATE)+" "+c.get(c.HOUR_OF_DAY)+":"+c.get(c.MINUTE)+":"+c.get(c.SECOND)+" "+line+"\n");
120   }
121   */
122   private static void logHeader( int severity ) {
123     calendar.setTime(new Date());
124     out.print( "[" );
125     out.print( calendar.get(Calendar.YEAR) );
126     out.print( "/" );
127     int tmp = calendar.get(Calendar.MONTH)+1;
128     if (tmp < 10) out.print("0");
129     out.print( tmp );
130     out.print( "/" );
131     tmp = calendar.get(Calendar.DATE);
132     if (tmp < 10) out.print("0");
133     out.print( tmp );
134     out.print( " " );
135     tmp = calendar.get(Calendar.HOUR_OF_DAY);
136     if (tmp < 10) out.print("0");
137     out.print( tmp );
138     out.print( ":" );
139     tmp = calendar.get(Calendar.MINUTE);
140     if (tmp < 10) out.print("0");
141     out.print( tmp );
142     out.print( ":" );
143     tmp = calendar.get(Calendar.SECOND);
144     if (tmp < 10) out.print("0");
145     out.print( tmp );
146     out.print( "." );
147     out.print( calendar.get(Calendar.MILLISECOND) );
148     out.print( "] " );
149     if ( severity < desc.length ) {
150       out.print( desc[severity] );
151       out.print( " " );
152     } else {
153       out.print( "CUSTOM(" );
154       out.print( severity );
155       out.print( ") " );
156     }
157     out.print( Thread.currentThread().getName() );
158     out.print( " " );
159     out.print( ": " );
160   }
161   /**
162   Log msg with specified severity, if severity is less or equal to current log level
163   */
164   public static void log( int severity, String msg ) {
165     if ( logger != null ) {
166       logger.logString( severity, msg );
167     } else {
168       out.println( msg );
169     }
170   }
171   /**
172   Log msg with specified severity, if severity is less or equal to current log level
173   */
174   public static void log( String msg, Throwable t ) {
175     if ( logger != null ) {
176       logger.logThrowable( msg, t );
177     } else {
178       out.println( msg );
179     }
180   }
181   /**
182   Log error
183   */
184   public static void log( Throwable t ) {
185     if ( logger != null ) {
186       logger.logThrowable( t );
187     } else {
188       t.printStackTrace( out );
189     }
190   }
191   protected void logString( int severity, String msg ) {
192     if ( severity <= logLevel ) {
193       synchronized( out ) {
194         if ( isLogHeader )
195           logHeader( severity );
196         out.println( msg );
197       }
198     }
199   }
200   protected void logThrowable( Throwable t ) {
201     synchronized ( out ) {
202       logHeader(ERROR);
203       t.printStackTrace( out );
204     }
205   }
206   protected void logThrowable( String msg, Throwable t ) {
207     synchronized ( out ) {
208       logHeader(ERROR);
209       out.println( msg );
210       logHeader(ERROR);
211       t.printStackTrace( out );
212     }
213   }
214   /**
215    * Include the log header?
216    */
217   public static void logHeader( boolean b ) {
218     isLogHeader = b;
219   }
220   /**
221   Log errors?
222   */
223   public static void logError( boolean b ) {
224     isLogError = b;
225   }
226   /**
227   Log info?
228   */
229   public static void logInfo( boolean b ) {
230     isLogInfo=b;
231   }
232   /**
233   Log debugging info?
234   */
235   public static void logDebug( boolean b ) {
236     isLogDebug=b;
237   }
238   /**
239   Log warnings?
240   */
241   public static void logWarning( boolean b ) {
242     isLogWarning=b;
243   }
244   /**
245   Set log level
246   */
247   public static void setLogLevel( int level ) {
248     logLevel = level;
249   }
250   /**
251   returns current log level
252   */
253   public static int getLogLevel() {
254     return logLevel;
255   }
256   /**
257   Exit jvm on error?
258   */
259   public static void stopOnError( boolean stop ) {
260     stopOnError=stop;
261   }
262   /**
263   Log <b>line</b> to info stream
264   */
265   public static void logInfo( String line ) {
266     if (isLogInfo) {
267       log( INFO, line );
268     }
269   }
270   /**
271   Log <b>line</b> to debug stream
272   */
273   public static void logDebug( String line ) {
274     if (isLogDebug) {
275       log( DEBUG, line );
276     }
277   }
278   /**
279   Log <b>line</b> to warning stream
280   */
281   public static void logWarning( String line ) {
282     if (isLogWarning) {
283       log( WARNING, line );
284     }
285   }
286   /**
287   Log <b>line</b> to error stream
288   */
289   public static void logError( String line ) {
290     //thread.interrupt();
291     if (isLogError) {
292       log( ERROR, line );
293     }
294     stop=stopOnError;
295   }
296   /**
297   Log <b>e</b> to error stream
298   */
299   public static void logError( Throwable e ) {
300     if (isLogError && e != null) {
301       //thread.interrupt();
302       log( e );
303       if ( isVerbose ) {
304         e.printStackTrace(System.out);
305       }
306       /**
307       if ( out instanceof PrintWriter ) {
308         //e.printStackTrace( out );
309         log( e );
310       }
311       */
312     }
313     stop=stopOnError;
314   }
315   public static void logError( String msg, Throwable e ) {
316     log( msg, e );
317   }
318 }