package com.c2kernel.utils;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.JOptionPane;
import org.omg.CORBA.UserException;
import org.tanukisoftware.wrapper.WrapperManager;
import com.c2kernel.process.AbstractMain;
import com.c2kernel.process.Gateway;
import com.c2kernel.scripting.ScriptConsole;
import com.c2kernel.utils.server.SimpleTCPIPServer;
/**
*
- message string should always contain the class name and the method name: Logger.msg(1,"ItemFact::createDir() - LifeCycle DB created"); - use meaningfull abbreviation and also use the dash to separate the 'header' from the message! - each method should start with this 'method signature' debug: Logger.msg(1,"ItemFact::createDir() - path:" + path);
*
*/
public class Logger
{
/**
* logging level 0 (only error & warning) => no logging ; 9 => maximum logging
* add ten to output time before each message
*/
private static int mHighestLogLevel = 0;
private static HashMap logStreams = new HashMap();
static protected SimpleTCPIPServer mConsole = null;
static private void printMessage(String message, int msgLogLevel)
{
synchronized(logStreams) {
for (Iterator iter = logStreams.keySet().iterator(); iter.hasNext();) {
PrintStream element = (PrintStream)iter.next();
int logLevel = ((Integer)logStreams.get(element)).intValue();
if (logLevel < msgLogLevel || (logLevel > 9 && logLevel - 10 < msgLogLevel))
continue;
if (logLevel > 9)
{
Timestamp ts = new Timestamp(System.currentTimeMillis());
String sTime = ts.toString();
message = sTime + " - " + message;
}
try {
element.println(message);
} catch (Exception ex) {
iter.remove();
}
}
}
}
static private void printMessage(Throwable ex) {
StringWriter msgString = new StringWriter();
PrintWriter msg = new PrintWriter(msgString);
msg.print(ex instanceof Exception ? "EXCEPTION:" : "JVM ERROR:");
ex.printStackTrace(msg);
printMessage(msgString.toString(), 0);
}
static public boolean doLog(int logLevel)
{
return mHighestLogLevel >= logLevel;
}
/**
* Use this only for temporary messages while developing/debugging When the code is stable, change calls to debug to
* message/warning/error with an appropriate log level This makes it easier to manage debug calls in the source.
*
* @param msg -
* the string to write to the console, or log file if specified in cmd line
*/
static public void debug(String msg)
{
msg("DEBUG : " + msg);
}
static public void debug(int logLevel, String msg)
{
msg(logLevel, "DEBUG : " + msg);
}
/**
* Use Logger.message to report information that will be useful for debugging a release
*
* @param level -
* log level of this message. If the current log level has been on the cmd line to be less that this number, the log message
* will not be displayed
* @param msg -
* the string to write to the console, or log file if specified in cmd line
*/
static public void msg(int level, String msg)
{
printMessage(msg, level);
}
static public void msg(String msg)
{
printMessage(msg, 0);
}
static public void error(String msg)
{
printMessage("ERROR : " + msg, 0);
}
static public void error(Throwable ex)
{
printMessage(ex);
}
static public void warning(String msg)
{
printMessage("WARNING: " + msg, 0);
}
static public void die(String msg)
{
printMessage("FATAL : " + msg, 0);
if (AbstractMain.runningAsWrapper)
WrapperManager.stop(1);
else
System.exit(1);
}
static public void exceptionDialog(Exception ex)
{
String className = ex.getClass().getName();
className = className.substring(className.lastIndexOf('.') + 1);
String error = ex.getMessage();
if (ex instanceof UserException)
error = error.substring(error.indexOf(' ') + 1);
JOptionPane.showMessageDialog(null, error, className, JOptionPane.ERROR_MESSAGE);
}
/**
* @param console
*/
public static void addLogStream(PrintStream console, int logLevel) {
try {
console.println("***********************************************************");
console.println(" C2Kernel log started at level "+logLevel+" @"+new Timestamp(System.currentTimeMillis()).toString());
console.println("***********************************************************");
} catch (Exception ex) {
System.out.println("Exception accessing log stream");
ex.printStackTrace();
}
synchronized(logStreams) {
logStreams.put(console, new Integer(logLevel));
if (logLevel > 9) logLevel-=10;
if (logLevel > mHighestLogLevel) mHighestLogLevel = logLevel;
}
}
/**
* @param console
*/
public static void removeLogStream(PrintStream console) {
synchronized(logStreams) {
Integer logIntObj = (Integer)logStreams.get(console);
if (logIntObj == null) return; // not registered
int logLevel = (logIntObj).intValue();
logStreams.remove(console);
// recalculate lowest log level
if (logLevel == mHighestLogLevel || (logLevel > 9 && logLevel-10 == mHighestLogLevel)) {
mHighestLogLevel = -1;
for (Iterator iter = logStreams.values().iterator(); iter.hasNext();) {
Integer element = (Integer)iter.next();
int thisLogLevel = element.intValue()>9?element.intValue()-10:element.intValue();
if (thisLogLevel > mHighestLogLevel || mHighestLogLevel == -1)
mHighestLogLevel = thisLogLevel;
}
}
}
}
static public void initConsole(String id)
{
String portString = Gateway.getProperty(id+".Console.port");
int port = 0;
try {
port = Integer.parseInt(portString);
} catch (NumberFormatException ex) {
Logger.msg("No port defined for "+id+" console. Using any port.");
}
mConsole = new SimpleTCPIPServer(port, ScriptConsole.class, 5);
mConsole.startListening();
Gateway.setProperty(id+".Console.port", String.valueOf(mConsole.getPort()));
}
static public int getConsolePort() {
return mConsole.getPort();
}
static public void closeConsole()
{
if (mConsole != null)
mConsole.stopListening();
}
}