001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.lib;
032:
033: import java.io.File;
034: import java.io.FileWriter;
035: import java.io.PrintWriter;
036:
037: import org.hsqldb.HsqlDateTime;
038:
039: /**
040: * Simple log for recording abnormal events in persistence<p>
041: * Log levels, LOG_NONE, LOG_ERROR, and LOG_NORMAL are currently supported.<p>
042: * LOG_ERROR corresponds to property value 1 and logs main database events plus
043: * any major errors encountered in operation.
044: * LOG_NORMAL corresponds to property value 2 and logs additional normal events
045: * and minor errors.
046: *
047: * @author fredt@users
048: * @version 1.8.0
049: * @since 1.8.0
050: */
051: public class SimpleLog {
052:
053: public static int LOG_NONE = 0;
054: public static int LOG_ERROR = 1;
055: public static int LOG_NORMAL = 2;
056: private PrintWriter writer;
057: private int level;
058:
059: public SimpleLog(String path, int level, boolean useFile) {
060:
061: this .level = level;
062:
063: if (level != LOG_NONE) {
064: if (useFile) {
065: File file = new File(path);
066:
067: makeLog(file);
068: } else {
069: writer = new PrintWriter(System.out);
070: }
071: }
072: }
073:
074: private void makeLog(File file) {
075:
076: try {
077: FileUtil.makeParentDirectories(file);
078:
079: writer = new PrintWriter(new FileWriter(file.getPath(),
080: true), true);
081: } catch (Exception e) {
082: writer = new PrintWriter(System.out);
083: }
084: }
085:
086: public int getLevel() {
087: return level;
088: }
089:
090: public PrintWriter getPrintWriter() {
091: return writer;
092: }
093:
094: public synchronized void sendLine(int atLevel, String message) {
095:
096: if (level >= atLevel) {
097: writer.println(HsqlDateTime.getSytemTimeString() + " "
098: + message);
099: }
100: }
101:
102: public synchronized void logContext(int atLevel, String message) {
103:
104: if (level < atLevel) {
105: return;
106: }
107:
108: String info = HsqlDateTime.getSytemTimeString();
109:
110: //#ifdef JDBC3
111: Throwable temp = new Throwable();
112: StackTraceElement[] 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 t, String message) {
124:
125: if (level == LOG_NONE) {
126: return;
127: }
128:
129: String info = HsqlDateTime.getSytemTimeString();
130:
131: //#ifdef JDBC3
132: Throwable temp = new Throwable();
133: StackTraceElement[] 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: }
|