/*
* Copyright (C) 2006 http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
package org.chaidb.db.helper;
/**
* This class implements a logging mechanism to be used by applications
* to log events. Log messages are associated with levels akin to the
* unix syslog. If the messages are at a level higher than the threshold
* they will be logged, otherwise will be ignored.
*
*/
import java.io.FileWriter;
import java.io.IOException;
public class Log {
/**
* log level
*/
protected static short threshold = 0;
/**
* The log output stream
*/
protected static FileWriter log = null;
/**
* Flag indicating whether logging is turned on of off
*/
public static boolean isLoggingOn = true;
/**
* If verbose, log messages are also written to the standard output
*/
protected static boolean verbose = false;
/**
* Carriage Return + Line feed
*/
private static final String CRLF = "\r\n";
//------>>> predefined log levels <<<----
//Should be in ascending order of criticality
/**
* System is unusable
*/
public static final short LOG_EMERG = 0;
/**
* Action must be taken immediately
*/
public static final short LOG_ALERT = 1;
/**
* Critical Conditions
*/
public static final short LOG_CRIT = 2;
/**
* Error Conditions
*/
public static final short LOG_ERR = 3;
/**
* Warning Conditions
*/
public static final short LOG_WARNING = 4;
/**
* Normal, but significant condition
*/
public static final short LOG_NOTICE = 5;
/**
* Informational Message
*/
public static final short LOG_INFO = 6;
/**
* debug-Level Messages
*/
public static final short LOG_DEBUG = 7;
/**
* sets the log file
*
* @param logFile The log file name
* @throws IOException Thrown when the file cannot be found/written
* to.
*/
public static void setLogFile(String logFile) throws IOException {
if (log != null) log.close();
log = new FileWriter(logFile, true);
}
/**
* sets the threshold for the log
*
* @param threshold The threshold in terms of log level. Only messages
* of Criticality higher than the threshold will be logged.
*/
public static void setThreshold(short threshold) {
Log.threshold = threshold;
}
/**
* turns logging on, or off
*
* @param b A boolean flag indicating whether logging is to be turned on
* (true) or off(false).
*/
public static void setLogging(boolean b) {
isLoggingOn = b;
}
/**
* turns the verbose mode on or off
*
* @param v A boolean value indicating whether the verbose mode is
* turned on(true) or off(false)
*/
public static void setVerbose(boolean v) {
verbose = v;
}
/**
* verbose log messages are written to standard output as well as to the
* log file
*
* @param level The message log level as a short value
* @param logMessage The log message
*/
public static int verboseLogMessage(short level, String logMessage) {
if (verbose) System.out.println(logMessage);
return write(level, logMessage);
}
/**
* logs the message with the given log level
*
* @param level The message log level as a short value
* @param logMessage The log message
*/
public static synchronized int write(short level, String logMessage) {
if (level > threshold) return -1;
try {
log.write(logMessage);
log.write(CRLF);
log.flush();
} catch (IOException ioe) {
return -1;
}
return 0;
}
/**
* close the log file
*/
public synchronized static void closeLog() throws IOException {
log.close();
log = null;
}
}