1 |
dpavlin |
237 |
/* |
2 |
|
|
openisis - an open implementation of the CDS/ISIS database |
3 |
|
|
Version 0.8.x (patchlevel see file Version) |
4 |
|
|
Copyright (C) 2001-2003 by Erik Grziwotz, erik@openisis.org |
5 |
|
|
|
6 |
|
|
This library is free software; you can redistribute it and/or |
7 |
|
|
modify it under the terms of the GNU Lesser General Public |
8 |
|
|
License as published by the Free Software Foundation; either |
9 |
|
|
version 2.1 of the License, or (at your option) any later version. |
10 |
|
|
|
11 |
|
|
This library is distributed in the hope that it will be useful, |
12 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
|
|
Lesser General Public License for more details. |
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU Lesser General Public |
17 |
|
|
License along with this library; if not, write to the Free Software |
18 |
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
|
|
20 |
|
|
see README for more information |
21 |
|
|
EOH */ |
22 |
|
|
|
23 |
|
|
package org.openisis; |
24 |
|
|
|
25 |
|
|
import java.io.*; |
26 |
|
|
import java.util.Date; |
27 |
|
|
import java.text.SimpleDateFormat; |
28 |
|
|
|
29 |
|
|
/** |
30 |
|
|
Logger. |
31 |
|
|
<p> |
32 |
|
|
$Id: Log.java,v 1.1 2003/04/12 14:48:21 mawag Exp $ |
33 |
|
|
@version $Revision: 1.1 $ |
34 |
|
|
@author $Author: mawag $ |
35 |
|
|
*/ |
36 |
|
|
public class Log { |
37 |
|
|
|
38 |
|
|
/** Error log level */ |
39 |
|
|
public static final int ERROR = 1; |
40 |
|
|
/** Warning log level */ |
41 |
|
|
public static final int WARN = ERROR + 1; |
42 |
|
|
/** Info log level */ |
43 |
|
|
public static final int INFO = WARN + 1; |
44 |
|
|
/** Verbose log level */ |
45 |
|
|
public static final int VERBOSE = INFO + 1; |
46 |
|
|
|
47 |
|
|
private static SimpleDateFormat TheFormat = |
48 |
|
|
new SimpleDateFormat ("yyMMddHHmmss"); |
49 |
|
|
|
50 |
|
|
private static PrintStream TheStream = System.err; |
51 |
|
|
|
52 |
|
|
private static int TheLevel = VERBOSE; |
53 |
|
|
|
54 |
|
|
/** Set log level. Default level is VERBOSE. |
55 |
|
|
*/ |
56 |
|
|
public static void setLevel (int lvl) { |
57 |
|
|
TheLevel = lvl; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
private static void doPrint ( |
61 |
|
|
String pref, Object that, String msg, Throwable ex |
62 |
|
|
) { |
63 |
|
|
StringBuffer buf = new StringBuffer (128); |
64 |
|
|
buf.append (pref). |
65 |
|
|
append (' '). |
66 |
|
|
append (TheFormat.format (new Date ())). |
67 |
|
|
append (' '); |
68 |
|
|
if (null != that) { |
69 |
|
|
buf.append ('['). |
70 |
|
|
append (that). |
71 |
|
|
append (']'); |
72 |
|
|
} |
73 |
|
|
if (null != msg) { |
74 |
|
|
if (null != that) { |
75 |
|
|
buf.append (": "); |
76 |
|
|
} |
77 |
|
|
buf.append (msg); |
78 |
|
|
} |
79 |
|
|
if (null != ex) { |
80 |
|
|
if (null != that || null != msg) { |
81 |
|
|
buf.append (": "); |
82 |
|
|
} |
83 |
|
|
} |
84 |
|
|
synchronized (TheStream) { |
85 |
|
|
TheStream.print (buf.toString ()); |
86 |
|
|
if (null != ex) { |
87 |
|
|
ex.printStackTrace (TheStream); |
88 |
|
|
} |
89 |
|
|
else { |
90 |
|
|
TheStream.println (); |
91 |
|
|
} |
92 |
|
|
} |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
/** Log a verbose message. |
96 |
|
|
*/ |
97 |
|
|
public static void verbose (Object module, String msg, Throwable ex) { |
98 |
|
|
if (VERBOSE <= TheLevel) { |
99 |
|
|
doPrint (" ", module, msg, ex); |
100 |
|
|
} |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
/** Log an info message. |
104 |
|
|
*/ |
105 |
|
|
public static void info (Object module, String msg, Throwable ex) { |
106 |
|
|
if (INFO <= TheLevel) { |
107 |
|
|
doPrint ("*** ", module, msg, ex); |
108 |
|
|
} |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
/** Log a warning. |
112 |
|
|
*/ |
113 |
|
|
public static void warn (Object module, String msg, Throwable ex) { |
114 |
|
|
if (WARN <= TheLevel) { |
115 |
|
|
doPrint ("WARN", module, msg, ex); |
116 |
|
|
} |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
/** Log an error. |
120 |
|
|
*/ |
121 |
|
|
public static void error (Object module, String msg, Throwable ex) { |
122 |
|
|
if (ERROR <= TheLevel) { |
123 |
|
|
doPrint ("ERR ", module, msg, ex); |
124 |
|
|
} |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
/* |
128 |
|
|
Tester |
129 |
|
|
public static void main (String argv[]) { |
130 |
|
|
error (new Object (), "error", new Error ("fatal")); |
131 |
|
|
warn (null, "warning", null); |
132 |
|
|
info (null, null, null); |
133 |
|
|
verbose (new Exception (), null, new Exception ()); |
134 |
|
|
} |
135 |
|
|
*/ |
136 |
|
|
} |
137 |
|
|
|