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