001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util;
038:
039: import java.io.*;
040:
041: import java.util.Arrays;
042: import java.util.Date;
043:
044: /** Logging class to record errors or unexpected behavior to a file. The file is created in the current directory,
045: * and is only used if the log is enabled. All logs can be enabled at once with the ENABLE_ALL field.
046: * @version $Id: Log.java 4255 2007-08-28 19:17:37Z mgricken $
047: */
048: public class Log {
049: public static final boolean ENABLE_ALL = false;
050:
051: /** Whether this particular log is enabled in development mode. */
052: protected volatile boolean _isEnabled;
053:
054: /** The filename of this log. */
055: protected volatile String _name;
056:
057: /** The file object for this log. */
058: protected volatile File _file;
059:
060: /** PrintWriter to print messages to a file. */
061: protected volatile PrintWriter _writer;
062:
063: /** Creates a new Log with the given name. If enabled is true, a file is created in the current directory with the
064: * given name.
065: * @param name File name for the log
066: * @param isEnabled Whether to actively use this log
067: */
068: public Log(String name, boolean isEnabled) {
069: this (new File(name), isEnabled);
070: }
071:
072: public Log(File f, boolean isEnabled) {
073: _file = f;
074: _name = f.getName();
075: _isEnabled = isEnabled;
076: _init();
077: }
078:
079: /** Creates the log file, if enabled. */
080: protected void _init() {
081: if (_writer == null) {
082: if (_isEnabled || ENABLE_ALL) {
083: try {
084: FileWriter w = new FileWriter(_file
085: .getAbsolutePath(), true);
086: _writer = new PrintWriter(w);
087:
088: log("Log '" + _name + "' opened: " + (new Date()));
089: } catch (IOException ioe) {
090: throw new RuntimeException("Could not create log: "
091: + ioe);
092: }
093: }
094: }
095: }
096:
097: /** Sets whether this log is enabled. Only has an effect if the code is in development mode.
098: * @param isEnabled Whether to print messages to the log file
099: */
100: public void setEnabled(boolean isEnabled) {
101: _isEnabled = isEnabled;
102: }
103:
104: /** Returns whether this log is currently enabled. */
105: public boolean isEnabled() {
106: return (_isEnabled || ENABLE_ALL);
107: }
108:
109: /** Prints a message to the log, if enabled.
110: * @param message Message to print.
111: */
112: public synchronized void log(String message) {
113: if (isEnabled()) {
114: if (_writer == null) {
115: _init();
116: }
117: _writer.println((new Date()) + ": " + message);
118: _writer.flush();
119: }
120: }
121:
122: /** Converts a stack trace (StackTraceElement[]) to string form */
123: public static String traceToString(StackTraceElement[] trace) {
124: final StringBuilder traceImage = new StringBuilder();
125: for (StackTraceElement e : trace)
126: traceImage.append("\n" + e.toString());
127: return traceImage.toString();
128: }
129:
130: /** Prints a message and exception stack trace to the log, if enabled.
131: * @param s Message to print
132: * @param trace Stack track to log
133: */
134: public synchronized void log(String s, StackTraceElement[] trace) {
135: if (isEnabled())
136: log(s + traceToString(trace));
137: }
138:
139: /** Prints a message and exception stack trace to the log, if enabled.
140: * @param s Message to print
141: * @param t Throwable to log
142: */
143: public synchronized void log(String s, Throwable t) {
144: if (isEnabled()) {
145: StringWriter sw = new StringWriter();
146: PrintWriter pw = new PrintWriter(sw);
147: t.printStackTrace(pw);
148: log(s + "\n" + sw.toString());
149: }
150: }
151: }
|