KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > lib > SimpleLog


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.lib;
33
34 import java.io.File JavaDoc;
35 import java.io.FileWriter JavaDoc;
36 import java.io.PrintWriter JavaDoc;
37
38 import org.hsqldb.HsqlDateTime;
39
40 /**
41  * Simple log for recording abnormal events in persistence<p>
42  * Log levels, LOG_NONE, LOG_ERROR, and LOG_NORMAL are currently supported.<p>
43  * LOG_ERROR corresponds to property value 1 and logs main database events plus
44  * any major errors encountered in operation.
45  * LOG_NORMAL corresponds to property value 2 and logs additional normal events
46  * and minor errors.
47  *
48  * @author fredt@users
49  * @version 1.8.0
50  * @since 1.8.0
51  */

52 public class SimpleLog {
53
54     public static int LOG_NONE = 0;
55     public static int LOG_ERROR = 1;
56     public static int LOG_NORMAL = 2;
57     private PrintWriter JavaDoc writer;
58     private int level;
59
60     public SimpleLog(String JavaDoc path, int level, boolean useFile) {
61
62         this.level = level;
63
64         if (level != LOG_NONE) {
65             if (useFile) {
66                 File JavaDoc file = new File JavaDoc(path);
67
68                 makeLog(file);
69             } else {
70                 writer = new PrintWriter JavaDoc(System.out);
71             }
72         }
73     }
74
75     private void makeLog(File JavaDoc file) {
76
77         try {
78             FileUtil.makeParentDirectories(file);
79
80             writer = new PrintWriter JavaDoc(new FileWriter JavaDoc(file.getPath(), true),
81                                      true);
82         } catch (Exception JavaDoc e) {
83             writer = new PrintWriter JavaDoc(System.out);
84         }
85     }
86
87     public int getLevel() {
88         return level;
89     }
90
91     public PrintWriter JavaDoc getPrintWriter() {
92         return writer;
93     }
94
95     public synchronized void sendLine(int atLevel, String JavaDoc message) {
96
97         if (level >= atLevel) {
98             writer.println(HsqlDateTime.getSytemTimeString() + " " + message);
99         }
100     }
101
102     public synchronized void logContext(int atLevel, String JavaDoc message) {
103
104         if (level < atLevel) {
105             return;
106         }
107
108         String JavaDoc info = HsqlDateTime.getSytemTimeString();
109
110 //#ifdef JDBC3
111
Throwable JavaDoc temp = new Throwable JavaDoc();
112         StackTraceElement JavaDoc[] elements = temp.getStackTrace();
113
114         if (elements.length > 1) {
115             info += " " + elements[1].getClassName() + "."
116                     + elements[1].getMethodName();
117         }
118
119 //#endif
120
writer.println(info + " " + message);
121     }
122
123     public synchronized void logContext(Throwable JavaDoc t, String JavaDoc message) {
124
125         if (level == LOG_NONE) {
126             return;
127         }
128
129         String JavaDoc info = HsqlDateTime.getSytemTimeString();
130
131 //#ifdef JDBC3
132
Throwable JavaDoc temp = new Throwable JavaDoc();
133         StackTraceElement JavaDoc[] elements = temp.getStackTrace();
134
135         if (elements.length > 1) {
136             info += " " + elements[1].getClassName() + "."
137                     + elements[1].getMethodName();
138         }
139
140         elements = t.getStackTrace();
141
142         if (elements.length > 0) {
143             info += " " + elements[0].getClassName() + "."
144                     + elements[0].getMethodName();
145         }
146
147 //#endif
148
if (message == null) {
149             message = "";
150         }
151
152         writer.println(info + " " + t.toString() + " " + message);
153     }
154
155     public void flush() {
156
157         if (writer != null) {
158             writer.flush();
159         }
160     }
161
162     public void close() {
163
164         if (writer != null) {
165             writer.close();
166         }
167     }
168 }
169
Popular Tags