KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > Log


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util;
35
36 import java.io.*;
37
38 import java.util.Arrays JavaDoc;
39 import java.util.Date JavaDoc;
40
41 /** Logging class to record errors or unexpected behavior to a file. The file is created in the current directory,
42   * and is only used if the log is enabled. All logs can be enabled at once with the ENABLE_ALL field.
43   * @version $Id: Log.java 4076 2007-01-19 23:01:04Z dlsmith $
44   */

45 public class Log {
46   public static final boolean ENABLE_ALL = false;
47
48   /** Whether this particular log is enabled in development mode. */
49   protected volatile boolean _isEnabled;
50
51   /** The filename of this log. */
52   protected volatile String JavaDoc _name;
53   
54   /** The file object for this log. */
55   protected volatile File _file;
56
57   /** PrintWriter to print messages to a file. */
58   protected volatile PrintWriter _writer;
59
60   /** Creates a new Log with the given name. If enabled is true, a file is created in the current directory with the
61     * given name.
62     * @param name File name for the log
63     * @param isEnabled Whether to actively use this log
64     */

65   public Log(String JavaDoc name, boolean isEnabled) { this(new File(name), isEnabled); }
66   
67   public Log(File f, boolean isEnabled) {
68     _file = f;
69     _name = f.getName();
70     _isEnabled = isEnabled;
71     _init();
72   }
73
74   /** Creates the log file, if enabled. */
75   protected void _init() {
76     if (_writer == null) {
77       if (_isEnabled || ENABLE_ALL) {
78         try {
79           FileWriter w = new FileWriter(_file.getAbsolutePath(), true);
80           _writer = new PrintWriter(w);
81
82           log("Log '" + _name + "' opened: " + (new Date JavaDoc()));
83         }
84         catch (IOException ioe) {
85           throw new RuntimeException JavaDoc("Could not create log: " + ioe);
86         }
87       }
88     }
89   }
90
91   /** Sets whether this log is enabled. Only has an effect if the code is in development mode.
92    * @param isEnabled Whether to print messages to the log file
93    */

94   public void setEnabled(boolean isEnabled) { _isEnabled = isEnabled; }
95
96   /** Returns whether this log is currently enabled. */
97   public boolean isEnabled() { return (_isEnabled || ENABLE_ALL); }
98
99   /** Prints a message to the log, if enabled.
100    * @param message Message to print.
101    */

102   public synchronized void log(String JavaDoc message) {
103     if (isEnabled()) {
104       if (_writer == null) {
105         _init();
106       }
107       _writer.println((new Date JavaDoc()) + ": " + message);
108       _writer.flush();
109     }
110   }
111
112   /** Converts a stack trace (StackTraceElement[]) to string form */
113   public static String JavaDoc traceToString(StackTraceElement JavaDoc[] trace) {
114     final StringBuilder JavaDoc traceImage = new StringBuilder JavaDoc();
115     for (StackTraceElement JavaDoc e: trace) traceImage.append("\n" + e.toString());
116     return traceImage.toString();
117   }
118     
119   /** Prints a message and exception stack trace to the log, if enabled.
120    * @param s Message to print
121    * @param trace Stack track to log
122    */

123   public synchronized void log(String JavaDoc s, StackTraceElement JavaDoc[] trace) {
124     if (isEnabled()) log(s + traceToString(trace));
125   }
126   
127   /** Prints a message and exception stack trace to the log, if enabled.
128    * @param s Message to print
129    * @param t Throwable to log
130    */

131   public synchronized void log(String JavaDoc s, Throwable JavaDoc t) {
132     if (isEnabled()) {
133       StringWriter sw = new StringWriter();
134       PrintWriter pw = new PrintWriter(sw);
135       t.printStackTrace(pw);
136       log(s + "\n" + sw.toString());
137     }
138   }
139 }
Popular Tags