-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathLog.java
More file actions
166 lines (145 loc) · 6.2 KB
/
Log.java
File metadata and controls
166 lines (145 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*BEGIN_COPYRIGHT_BLOCK
*
* Copyright (c) 2001-2015, JavaPLT group at Rice University (drjava@rice.edu)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the names of DrJava, DrScala, the JavaPLT group, Rice University, nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software is Open Source Initiative approved Open Source Software.
* Open Source Initative Approved is a trademark of the Open Source Initiative.
*
* This file is part of DrScala. Download the current version of this project
* from http://www.drscala.org/.
*
* END_COPYRIGHT_BLOCK*/
package edu.rice.cs.util;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import edu.rice.cs.util.swing.Utilities;
/** Logging class to record errors or unexpected behavior to a file. The file is created in the current directory,
* and is only used if the log is enabled. All logs can be enabled at once with the ENABLE_ALL field.
* @version $Id: Log.java 5594 2012-06-21 11:23:40Z rcartwright $
*/
public class Log {
public static final boolean ENABLE_ALL = false;
/** Whether this particular log is enabled in development mode. */
protected volatile boolean _isEnabled;
/** The filename of this log. */
protected volatile String _name;
/** The file object for this log. */
protected volatile File _file;
/** PrintWriter to print messages to a file. */
protected volatile PrintWriter _writer;
public final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("d MMM yyyy H:mm:ss z");
/** Creates a new Log with the given name. If enabled is true, a file is created in the current directory with the
* given name.
* @param name File name for the log
* @param isEnabled Whether to actively use this log
*/
public Log(String name, boolean isEnabled) { this(new File(name), isEnabled); }
public Log(File f, boolean isEnabled) {
_file = f;
_name = f.getName();
_isEnabled = isEnabled;
DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
DATE_FORMAT.setLenient(false);
_init();
}
/** Creates the log file, if enabled. */
protected void _init() {
if (_writer == null) {
if (_isEnabled || ENABLE_ALL) {
try {
FileWriter w = new FileWriter(_file.getAbsolutePath(), true);
_writer = new PrintWriter(w);
log("Log '" + _name + "' opened: " + DATE_FORMAT.format(new Date()) + " with name " + _file.getAbsolutePath());
// Utilities.show("Opening log file: " + _file.getAbsolutePath());
}
catch (IOException ioe) {
throw new RuntimeException("Could not create log: " + ioe);
}
}
}
}
/** Sets whether this log is enabled. Only has an effect if the code is in development mode.
* @param isEnabled Whether to print messages to the log file
*/
public void setEnabled(boolean isEnabled) { _isEnabled = isEnabled; }
/** Returns whether this log is currently enabled. */
public boolean isEnabled() { return (_isEnabled || ENABLE_ALL); }
/** Prints a message to the log, if enabled.
* @param message Message to print.
*/
public synchronized void log(String message) {
if (isEnabled()) {
if (_writer == null) {
_init();
}
_writer.println("[" + Thread.currentThread().getId() + "]" + DATE_FORMAT.format(new Date()) + ": " + message);
_writer.flush();
}
}
/** Converts a stack trace (StackTraceElement[]) to string form */
public static String traceToString(StackTraceElement[] trace) {
final StringBuilder traceImage = new StringBuilder();
for (StackTraceElement e: trace) traceImage.append("\n\tat " + e.toString());
return traceImage.toString();
}
/** Prints a message and exception stack trace to the log, if enabled.
* @param s Message to print
* @param trace Stack track to log
*/
public synchronized void log(String s, StackTraceElement[] trace) {
if (isEnabled()) log(s + traceToString(trace));
}
/** Prints a message and exception stack trace to the log, if enabled.
* @param s Message to print
* @param t Throwable to log
*/
public synchronized void log(String s, Throwable t) {
if (isEnabled()) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
log(s + "\n" + sw.toString());
}
}
/** Closes a log file. */
public void close() {
_writer.close();
_writer = null;
}
/** Parses a date printed by Date.toString(); returns null if there is a parse error or if there is no date. */
public synchronized Date parse(String s) {
int pos = s.indexOf("GMT: ");
if (pos == -1) { return null; }
try {
return DATE_FORMAT.parse(s.substring(0,pos+3));
}
catch(ParseException pe) { return null; }
}
}