View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2001 Intersect Software Corporation
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.foundation;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.FileOutputStream;
23  import java.io.PrintStream;
24  import java.util.Date;
25  
26  /***
27  * NSLog is foundation's built-in logging facility: IMPLEMENTED, BUT NOT TESTED.
28  * By default, all groups are enabled, and debug level is DebugLevelOff.
29  *
30  * @author michael@mpowers.net
31  * @author $Author: cgruber $
32  * @version $Revision: 893 $
33  */
34  public class NSLog
35  {
36      public static long DebugGroupApplicationGeneration = 1L << 3;
37      public static long DebugGroupArchiving = 1L << 6;  
38      public static long DebugGroupAssociations = 1L << 19;  
39      public static long DebugGroupComponentBindings = 1L << 9;  
40      public static long DebugGroupControllers = 1L << 20;  
41      public static long DebugGroupComponents = 1L << 26;  
42      public static long DebugGroupDatabaseAccess = 1L << 16;  
43      public static long DebugGroupDeployment = 1L << 22;  
44      public static long DebugGroupEnterpriseObjects = 1L << 1;  
45      public static long DebugGroupFormatting = 1L << 10;  
46      public static long DebugGroupIO = 1L << 13;  
47      public static long DebugGroupJSPServlets = 1L << 27;  
48      public static long DebugGroupKeyValueCoding = 1L << 8;  
49      public static long DebugGroupModel = 1L << 15;  
50      public static long DebugGroupMultithreading = 1L << 4;  
51      public static long DebugGroupParsing = 1L << 23;  
52      public static long DebugGroupQualifiers = 1L << 11;  
53      public static long DebugGroupReflection = 1L << 24;  
54      public static long DebugGroupRequestHandling = 1L << 25;  
55      public static long DebugGroupResources = 1L << 5;  
56      public static long DebugGroupRules = 1L << 21;  
57      public static long DebugGroupSQLGeneration = 1L << 17;  
58      public static long DebugGroupTiming = 1L << 14;  
59      public static long DebugGroupUserInterface = 1L << 18;  
60      public static long DebugGroupValidation = 1L << 7;  
61      public static long DebugGroupWebObjects = 1L << 2;  
62      public static long DebugGroupWebServices = 1L << 2;  
63      
64      public static int DebugLevelOff = 0;  
65      public static int DebugLevelCritical = 1;  
66      public static int DebugLevelInformational = 2;  
67      public static int DebugLevelDetailed = 3;
68  
69      /***
70      * The logger to which debug statements should be 
71      * conditionally written.  By default, these messages
72      * appear on the standard error stream.
73      */     
74      public static Logger debug;
75      
76      /***
77      * The logger to which error messages should be written,
78      * which may not always be user-visible.  By default,
79      * these messages appear on the standard error stream.
80      */
81      public static Logger err;
82      
83      /***
84      * The logger to which user-visible messages should be written.
85      * By default, these messages appear on the standard output stream.
86      */
87      public static Logger out;
88      
89      private static long allowedGroups;
90      private static int allowedLevel;
91      
92      static
93      {
94          debug = new PrintStreamLogger( System.err );
95          err = new PrintStreamLogger( System.err );
96          out = new PrintStreamLogger( System.out );
97          
98          //TODO: need to initialize the debug level and groups based
99          // on the value of the NSDebugLevel and NSDebugGroup properties
100         allowedGroups = Long.MAX_VALUE;
101         allowedLevel = 0;
102     }
103 
104     /***
105     * Adds the specified group masks to those allowed for logging.
106     */
107     public static void allowDebugLoggingForGroups(long aDebugGroups)
108     {
109         allowedGroups = allowedGroups | aDebugGroups;
110     }
111     
112     /***
113     * Returns the current logging debug level.
114     */
115     public static int allowedDebugLevel()
116     {
117         return allowedLevel;
118     }
119     
120     /***
121     * Returns whether logging is allowed for the specified groups masks.
122     */
123     public static boolean debugLoggingAllowedForGroups(long aDebugGroups)
124     {
125         return ( allowedGroups == ( allowedGroups | aDebugGroups ) );
126     }
127     
128     /***
129     * Returns whether logging is allowed for the specified level.
130     */
131     public static boolean debugLoggingAllowedForLevel(int aDebugLevel)
132     {
133         return ( allowedLevel >= aDebugLevel );
134     }
135     
136     /***
137     * Returns whether logging allowed for the specified groups masks
138     * at the specified level.  Convenience method.
139     */
140     public static boolean debugLoggingAllowedForLevelAndGroups(int aDebugLevel,
141                                                                long aDebugGroups)
142     {
143         return ( ( allowedLevel >= aDebugLevel ) 
144               && ( allowedGroups == ( allowedGroups | aDebugGroups ) ) );
145     }
146     
147     /***
148     * Convenience to obtain a java PrintStream for the specified file path.
149     * Returns null if the stream could not be created.
150     */
151     public static PrintStream printStreamForPath(String aPath)
152     {
153         try
154         {
155             return new PrintStream( new FileOutputStream( aPath ) );
156         }
157         catch ( Throwable t )
158         {
159         }
160         return null;
161     }
162     
163     /***
164     * Removes the specified group masks from those allowed for logging.
165     */
166     public static void refuseDebugLoggingForGroups(long aDebugGroups)
167     {
168         allowedGroups = ( allowedGroups | aDebugGroups ) ^ aDebugGroups;
169     }
170     
171     /***
172     * Sets the allowed groups to only those specified by the mask.
173     */
174     public static void setAllowedDebugGroups(long aDebugGroups)
175     {
176         allowedGroups = aDebugGroups;
177     }
178     
179     /***
180     * Sets the current debug level.
181     */
182     public static void setAllowedDebugLevel(int aDebugLevel)
183     {
184         allowedLevel = aDebugLevel;
185     }
186     
187     /***
188     * Sets the current debug logger.
189     * Does nothing if logger is null.
190     */
191     public static void setDebug(NSLog.Logger logger)
192     {
193         if ( logger != null )
194         {
195             debug = logger;
196         }
197     }
198     
199     /***
200     * Sets the current error logger.
201     * Does nothing if logger is null.
202     */
203     public static void setErr(NSLog.Logger logger)
204     {
205         if ( logger != null )
206         {
207             err = logger;
208         }
209     }
210     
211     /***
212     * Sets the current output logger.
213     * Does nothing if logger is null.
214     */
215     public static void setOut(NSLog.Logger logger)
216     {
217         if ( logger != null )
218         {
219             out = logger;
220         }
221     }
222     
223     /***
224     * Convenience to write the throwable's stack trace
225     * to a string.
226     */
227     public static String throwableAsString(Throwable t)
228     {
229         ByteArrayOutputStream os = new ByteArrayOutputStream();
230         PrintStream ps = new PrintStream( os );
231         t.printStackTrace( ps ); 
232         return os.toString();
233     }
234     
235     /***
236     * The abstract superclass of all Logger implementations.
237     */
238     static abstract public class Logger
239     {
240         private boolean enabled;
241         private boolean verbose;
242         
243         /***
244         * Default constructor sets enabled
245         * and verbose to true.
246         */
247         public Logger()
248         {
249             enabled = true;
250             verbose = true;
251         }
252         
253         /***
254         * Convenience to append a Boolean.
255         */
256         public void appendln(boolean aValue)
257         {
258             appendln( new Boolean( aValue ) );
259         }
260         
261         /***
262         * Convenience to append a Byte.
263         */
264         public void appendln(byte aValue)
265         {
266             appendln( new Byte( aValue ) );
267         }
268         
269         /***
270         * Convenience to write a String 
271         * comprised of the byte array using
272         * the default encoding.
273         */
274         public void appendln(byte[] aValue)
275         {
276             appendln( new String( aValue ) );
277         }
278         
279         /***
280         * Convenience to append a Character.
281         */
282         public void appendln(char aValue)
283         {
284             appendln( new Character( aValue ) );
285         }
286         
287         /***
288         * Convenience to append a String
289         * comprised of the character array.
290         */
291         public void appendln(char[] aValue)
292         {
293             appendln( new String( aValue ) );
294         }
295         
296         /***
297         * Convenience to append a Double.
298         */
299         public void appendln(double aValue)
300         {
301             appendln( new Double( aValue ) );
302         }
303         
304         /***
305         * Convenience to append a Float.
306         */
307         public void appendln(float aValue)
308         {
309             appendln( new Float( aValue ) );
310         }
311         
312         /***
313         * Convenience to append a Integer.
314         */
315         public void appendln(int aValue)
316         {
317             appendln( new Integer( aValue ) );
318         }
319         
320         /***
321         * Convenience to append a Long.
322         */
323         public void appendln(long aValue)
324         {
325             appendln( new Long( aValue ) );
326         }
327         
328         /***
329         * Convenience to append a Short
330         */
331         public void appendln(short aValue)
332         {
333             appendln( new Short( aValue ) );
334         }
335         
336         /***
337         * Convenience to append a Throwable.
338         */
339         public void appendln(Throwable aValue)
340         {
341             appendln( NSLog.throwableAsString( aValue ) );
342         }
343         
344         /***
345         * Writes the object to the log.
346         */
347         public abstract void appendln(Object aValue);
348         
349         /***
350         * Appends a line to the log.
351         */
352         public abstract void appendln();
353         
354         /***
355         * Flushes any buffered output to the log.
356         */
357         public abstract void flush();
358         
359         /***
360         * Returns whether the logger is enabled,
361         * the meaning of which is defined by the
362         * implementing class.
363         */
364         public boolean isEnabled()
365         {
366             return enabled;
367         }
368         
369         /***
370         * Returns whether the logger is verbose,
371         * the meaning of which is defined by the
372         * implementing class.
373         */
374         public boolean isVerbose()
375         {
376             return verbose;
377         }
378         
379         /***
380         * Sets whether the logger is enabled,
381         * the meaning of which is defined by the
382         * implementing class.
383         */
384         public void setIsEnabled(boolean aBool)
385         {
386             enabled = aBool;
387         }
388         
389         /***
390         * Sets whether the logger is verbose,
391         * the meaning of which is defined by the
392         * implementing class.
393         */
394         public void setIsVerbose(boolean aBool)
395         {
396             verbose = aBool;
397         }
398     }
399     
400     /***
401     * The default implementation of Logger that writes to a Java
402     * PrintStream.  If not enabled, no output is written.
403     * If verbose, output is in format: "[time] <thread name> message".
404     */ 
405     static public class PrintStreamLogger extends Logger
406     {
407         private PrintStream thePrintStream;
408         
409         /***
410         * Constructor takes a PrintStream.
411         */
412         public PrintStreamLogger(PrintStream ps)
413         {
414             thePrintStream = ps;
415         }
416         
417         /***
418         * Sends a newline to the print stream.
419         */ 
420         public void appendln()
421         {
422             if ( isEnabled() )
423             {
424                 thePrintStream.println();
425             }
426         }
427         
428         /***
429         * Writes the throwable to the print stream.
430         */
431         public void appendln(Throwable aValue)
432         {
433             appendln( NSLog.throwableAsString( aValue ) );
434         }
435         
436         /***
437         * Writes aValue.toString to the print stream.
438         */
439         public void appendln(Object aValue)
440         {
441             if ( isEnabled() )
442             {
443                 if ( isVerbose() )
444                 {
445                     thePrintStream.print( '[' + new Date().toString() + "] <" +
446                         Thread.currentThread().getName() + "> " );
447                 }
448                 if ( aValue == null ) aValue = "null";
449                 thePrintStream.println( aValue.toString() );
450             }
451         }
452         
453         /***
454         * Flushes the print stream.
455         */
456         public void flush()
457         {
458             thePrintStream.flush();
459         }
460         
461         /***
462         * Returns the current print stream.
463         */
464         public PrintStream printStream()
465         {
466             return thePrintStream;
467         }
468         
469         /***
470         * Replaces the current print stream.
471         */
472         public void setPrintStream(PrintStream aStream)
473         {
474             thePrintStream = aStream;
475         }
476     }
477 }
478 
479 /*
480  * $Log$
481  * Revision 1.2  2006/02/16 13:15:00  cgruber
482  * Check in all sources in eclipse-friendly maven-enabled packages.
483  *
484  * Revision 1.1  2003/01/31 22:33:00  mpowers
485  * Contributing NSLog.
486  *
487  *
488  */
489