001: // serverLog.java
002: // -------------------------------------
003: // (C) by Michael Peter Christen; mc@anomic.de
004: // first published on http://www.anomic.de
005: // Frankfurt, Germany, 2004
006: //last major change: $LastChangedDate: 2008-01-31 23:40:47 +0000 (Do, 31 Jan 2008) $ by $LastChangedBy: orbiter $
007: //Revision: $LastChangedRevision: 4424 $
008: //
009: // This program is free software; you can redistribute it and/or modify
010: // it under the terms of the GNU General Public License as published by
011: // the Free Software Foundation; either version 2 of the License, or
012: // (at your option) any later version.
013: //
014: // This program is distributed in the hope that it will be useful,
015: // but WITHOUT ANY WARRANTY; without even the implied warranty of
016: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: // GNU General Public License for more details.
018: //
019: // You should have received a copy of the GNU General Public License
020: // along with this program; if not, write to the Free Software
021: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: //
023: // Using this software in any meaning (reading, learning, copying, compiling,
024: // running) means that you agree that the Author(s) is (are) not responsible
025: // for cost, loss of data or any harm that may be caused directly or indirectly
026: // by usage of this softare or this documentation. The usage of this software
027: // is on your own risk. The installation and usage (starting/running) of this
028: // software may allow other people or application to access your computer and
029: // any attached devices and is highly dependent on the configuration of the
030: // software which must be done by the user of the software; the author(s) is
031: // (are) also not responsible for proper configuration and usage of the
032: // software, even if provoked by documentation provided together with
033: // the software.
034: //
035: // Any changes to this file according to the GPL as documented in the file
036: // gpl.txt aside this file in the shipment you received can be done to the
037: // lines that follows this copyright notice here, but changes must not be
038: // done inside the copyright notive above. A re-distribution must contain
039: // the intact and unchanged copyright notice.
040: // Contributions and changes to the program code must be marked as such.
041:
042: package de.anomic.server.logging;
043:
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.FileNotFoundException;
047: import java.io.IOException;
048: import java.util.logging.Level;
049: import java.util.logging.LogManager;
050: import java.util.logging.Logger;
051:
052: public final class serverLog {
053:
054: // log-level categories
055: public static final int LOGLEVEL_ZERO = Level.OFF.intValue(); // no output at all
056: public static final int LOGLEVEL_SEVERE = Level.SEVERE.intValue(); // system-level error, internal cause, critical and not fixeable (i.e. inconsistency)
057: public static final int LOGLEVEL_WARNING = Level.WARNING.intValue(); // uncritical service failure, may require user activity (i.e. input required, wrong authorization)
058: public static final int LOGLEVEL_CONFIG = Level.CONFIG.intValue(); // regular system status information (i.e. start-up messages)
059: public static final int LOGLEVEL_INFO = Level.INFO.intValue(); // regular action information (i.e. any httpd request URL)
060: public static final int LOGLEVEL_FINE = Level.FINE.intValue(); // in-function status debug output
061: public static final int LOGLEVEL_FINER = Level.FINER.intValue(); // in-function status debug output
062: public static final int LOGLEVEL_FINEST = Level.FINEST.intValue(); // in-function status debug output
063:
064: // these categories are also present as character tokens
065: public static final char LOGTOKEN_ZERO = 'Z';
066: public static final char LOGTOKEN_SEVERE = 'E';
067: public static final char LOGTOKEN_WARNING = 'W';
068: public static final char LOGTOKEN_CONFIG = 'S';
069: public static final char LOGTOKEN_INFO = 'I';
070: public static final char LOGTOKEN_FINE = 'D';
071: public static final char LOGTOKEN_FINER = 'D';
072: public static final char LOGTOKEN_FINEST = 'D';
073:
074: private final Logger theLogger;
075:
076: public serverLog(String appName) {
077: this .theLogger = Logger.getLogger(appName);
078: //this.theLogger.setLevel(Level.FINEST); // set a default level
079: }
080:
081: public void setLevel(Level newLevel) {
082: this .theLogger.setLevel(newLevel);
083: }
084:
085: public void logSevere(String message) {
086: this .theLogger.severe(message);
087: }
088:
089: public void logSevere(String message, Throwable thrown) {
090: this .theLogger.log(Level.SEVERE, message, thrown);
091: }
092:
093: public boolean isSevere() {
094: return this .theLogger.isLoggable(Level.SEVERE);
095: }
096:
097: public void logWarning(String message) {
098: this .theLogger.warning(message);
099: }
100:
101: public void logWarning(String message, Throwable thrown) {
102: this .theLogger.log(Level.WARNING, message, thrown);
103: }
104:
105: public boolean isWarning() {
106: return this .theLogger.isLoggable(Level.WARNING);
107: }
108:
109: public void logConfig(String message) {
110: this .theLogger.config(message);
111: }
112:
113: public void logConfig(String message, Throwable thrown) {
114: this .theLogger.log(Level.CONFIG, message, thrown);
115: }
116:
117: public boolean isConfig() {
118: return this .theLogger.isLoggable(Level.CONFIG);
119: }
120:
121: public void logInfo(String message) {
122: this .theLogger.info(message);
123: }
124:
125: public void logInfo(String message, Throwable thrown) {
126: this .theLogger.log(Level.INFO, message, thrown);
127: }
128:
129: public boolean isInfo() {
130: return this .theLogger.isLoggable(Level.INFO);
131: }
132:
133: public void logFine(String message) {
134: this .theLogger.fine(message);
135: }
136:
137: public void logFine(String message, Throwable thrown) {
138: this .theLogger.log(Level.FINE, message, thrown);
139: }
140:
141: public boolean isFine() {
142: return this .theLogger.isLoggable(Level.FINE);
143: }
144:
145: public void logFiner(String message) {
146: this .theLogger.finer(message);
147: }
148:
149: public void logFiner(String message, Throwable thrown) {
150: this .theLogger.log(Level.FINER, message, thrown);
151: }
152:
153: public boolean isFiner() {
154: return this .theLogger.isLoggable(Level.FINER);
155: }
156:
157: public void logFinest(String message) {
158: this .theLogger.finest(message);
159: }
160:
161: public void logFinest(String message, Throwable thrown) {
162: this .theLogger.log(Level.FINEST, message, thrown);
163: }
164:
165: public boolean isFinest() {
166: return this .theLogger.isLoggable(Level.FINEST);
167: }
168:
169: public boolean isLoggable(Level level) {
170: return this .theLogger.isLoggable(level);
171: }
172:
173: // static log messages: log everything
174: public static void logSevere(String appName, String message) {
175: Logger.getLogger(appName).severe(message);
176: }
177:
178: public static void logSevere(String appName, String message,
179: Throwable thrown) {
180: Logger.getLogger(appName).log(Level.SEVERE, message, thrown);
181: }
182:
183: public static void isSevere(String appName) {
184: Logger.getLogger(appName).isLoggable(Level.SEVERE);
185: }
186:
187: public static void logWarning(String appName, String message) {
188: Logger.getLogger(appName).warning(message);
189: }
190:
191: public static void logWarning(String appName, String message,
192: Throwable thrown) {
193: Logger.getLogger(appName).log(Level.WARNING, message, thrown);
194: }
195:
196: public static void isWarning(String appName) {
197: Logger.getLogger(appName).isLoggable(Level.WARNING);
198: }
199:
200: public static void logConfig(String appName, String message) {
201: Logger.getLogger(appName).config(message);
202: }
203:
204: public static void logConfig(String appName, String message,
205: Throwable thrown) {
206: Logger.getLogger(appName).log(Level.CONFIG, message, thrown);
207: }
208:
209: public static void isConfig(String appName) {
210: Logger.getLogger(appName).isLoggable(Level.CONFIG);
211: }
212:
213: public static void logInfo(String appName, String message) {
214: Logger.getLogger(appName).info(message);
215: }
216:
217: public static void logInfo(String appName, String message,
218: Throwable thrown) {
219: Logger.getLogger(appName).log(Level.INFO, message, thrown);
220: }
221:
222: public static void isInfo(String appName) {
223: Logger.getLogger(appName).isLoggable(Level.INFO);
224: }
225:
226: public static void logFine(String appName, String message) {
227: Logger.getLogger(appName).fine(message);
228: }
229:
230: public static void logFine(String appName, String message,
231: Throwable thrown) {
232: Logger.getLogger(appName).log(Level.FINE, message, thrown);
233: }
234:
235: public static void isFine(String appName) {
236: Logger.getLogger(appName).isLoggable(Level.FINE);
237: }
238:
239: public static void logFiner(String appName, String message) {
240: Logger.getLogger(appName).finer(message);
241: }
242:
243: public static void logFiner(String appName, String message,
244: Throwable thrown) {
245: Logger.getLogger(appName).log(Level.FINER, message, thrown);
246: }
247:
248: public static void isFiner(String appName) {
249: Logger.getLogger(appName).isLoggable(Level.FINER);
250: }
251:
252: public static void logFinest(String appName, String message) {
253: Logger.getLogger(appName).finest(message);
254: }
255:
256: public static void logFinest(String appName, String message,
257: Throwable thrown) {
258: Logger.getLogger(appName).log(Level.FINEST, message, thrown);
259: }
260:
261: public static void isFinest(String appName) {
262: Logger.getLogger(appName).isLoggable(Level.FINEST);
263: }
264:
265: public static final void configureLogging(File homePath,
266: File loggingConfigFile) throws SecurityException,
267: FileNotFoundException, IOException {
268: FileInputStream fileIn = null;
269: try {
270: System.out
271: .println("STARTUP: Trying to load logging configuration from file "
272: + loggingConfigFile.toString());
273: fileIn = new FileInputStream(loggingConfigFile);
274:
275: // loading the logger configuration from file
276: LogManager logManager = LogManager.getLogManager();
277: logManager.readConfiguration(fileIn);
278:
279: // creating the logging directory
280: String logPattern = logManager
281: .getProperty("java.util.logging.FileHandler.pattern");
282: int stripPos = logPattern.lastIndexOf('/');
283: if (stripPos < 0)
284: stripPos = logPattern
285: .lastIndexOf(File.pathSeparatorChar);
286: File log = new File(logPattern.substring(0, stripPos));
287: if (!log.isAbsolute())
288: log = new File(homePath, log.getPath());
289: if (!log.canRead())
290: log.mkdir();
291:
292: // TODO: changing the pattern settings for the file handlers
293:
294: // generating the root logger
295: /*Logger logger =*/Logger.getLogger("");
296:
297: // System.setOut(new PrintStream(new LoggerOutputStream(Logger.getLogger("STDOUT"),Level.FINEST)));
298: // System.setErr(new PrintStream(new LoggerOutputStream(Logger.getLogger("STDERR"),Level.SEVERE)));
299: } finally {
300: if (fileIn != null)
301: try {
302: fileIn.close();
303: } catch (Exception e) {
304: }
305: }
306: }
307:
308: public static final String format(String s, int n, int fillChar) {
309: int l = s.length();
310: if (l >= n)
311: return s;
312: StringBuffer sb = new StringBuffer(l + n);
313: for (int i = l + n; i > n; n--)
314: sb.insert(0, fillChar);
315: return sb.toString();
316: }
317:
318: public static final String arrayList(byte[] b, int start, int length) {
319: if (b == null)
320: return "NULL";
321: if (b.length == 0)
322: return "[]";
323: length = Math.min(length, b.length - start);
324: StringBuffer sb = new StringBuffer(b.length * 4);
325: sb.append('[').append(Integer.toString(b[start])).append(',');
326: for (int i = 1; i < length; i++)
327: sb.append(' ').append(Integer.toString(b[start + i]))
328: .append(',');
329: sb.append(']');
330: return sb.toString();
331: }
332:
333: public static final String table(byte[] b, int linewidth, int marker) {
334: if (b == null)
335: return "NULL";
336: if (b.length == 0)
337: return "[]";
338: StringBuffer sb = new StringBuffer(b.length * 4);
339: for (int i = 0; i < b.length; i++) {
340: if (i % linewidth == 0)
341: sb.append('\n').append("# ").append(
342: Integer.toHexString(i)).append(": ");
343: else
344: sb.append(',');
345: sb.append(' ').append(Integer.toString(0xff & b[i]));
346: if (i >= 65535)
347: break;
348: }
349: sb.append('\n');
350: return sb.toString();
351: }
352:
353: public static final boolean allZero(byte[] a) {
354: return allZero(a, 0, a.length);
355: }
356:
357: public static final boolean allZero(byte[] a, int astart,
358: int alength) {
359: for (int i = 0; i < alength; i++)
360: if (a[astart + i] != 0)
361: return false;
362: return true;
363: }
364:
365: }
|