diff -r f59d6eba666c netx/net/sourceforge/jnlp/util/ItwLogger.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/netx/net/sourceforge/jnlp/util/ItwLogger.java Tue Sep 10 22:00:03 2013 +0200 @@ -0,0 +1,311 @@ +/*Copyright (C) 2013 Red Hat, Inc. + +This file is part of IcedTea. + +IcedTea 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, version 2. + +IcedTea 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. + +You should have received a copy of the GNU General Public License +along with IcedTea; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. + */ +package net.sourceforge.jnlp.util; + +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.Date; +import net.sourceforge.jnlp.AppletLog; +import net.sourceforge.jnlp.runtime.JNLPRuntime; + +/* + * By default all messages are printed to syslog + * If febug is true, then they are printed also to appropriate stream + * all messages with reprint true are printed to its stream always + * if force is true, then all messages are reprinted to streams + * + */ +public class ItwLogger { + + public static enum StdStream { + + OUT, ERR, BOTH + } + private static final String NULL_OBJECT = "Trying to log null object"; + private static final boolean force = false; + private /*final*/ PrintStream err; + private /*final*/ PrintStream out; + private static ItwLogger logger; + + private ItwLogger() { + this(System.out, System.err); + } + /* + * for testing puposes! + */ + + public PrintStream getOut() { + return out; + } + /* + * for testing puposes! + */ + + public PrintStream getErr() { + return err; + } + /* + * for testing puposes! + */ + + public void setOut(PrintStream out) { + this.out = out; + } + /* + * for testing puposes! + */ + + public void setErr(PrintStream err) { + this.err = err; + } + + /** + * for testing purposes the logger with custom streams canbe created + * otherwise only getLogger()'s singleton can be called + */ + public ItwLogger(PrintStream out, PrintStream err) { + if (out == null || err == null) { + throw new IllegalArgumentException("No stream canbe null"); + } + this.err = err; + this.out = out; + } + + synchronized public static ItwLogger getLogger() { + if (logger == null) { + logger = new ItwLogger(); + } + return logger; + } + + private void logToSystemLogger(StdStream stream, Object o) { + String s = getHeader(stream); + String message = NULL_OBJECT; + if (o != null) { + if (o instanceof Throwable) { + message = exceptionToString((Throwable) o); + } else { + message = o.toString(); + } + } + String delimiter = " "; + if (message.contains("\n")) { + delimiter = "\n"; + } + write(s + delimiter + message); + } + + public static String exceptionToString(Throwable t) { + if (t == null) { + //return "Exception was null"; + return null; + } + String s = "Error during processing of exception"; + try { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + t.printStackTrace(pw); + s = sw.toString(); + pw.close(); + sw.close(); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + return s; + } + + private void write(String string) { + if (force) { + System.out.println(string); + } + //connection to syslog or /var/log/messages not yet implemented + + } + + public void log(Throwable o) { + log(false, null, o); + + } + + public void log(Object o) { + log(false, StdStream.OUT, o); + + } + + public void log(boolean reprint, Object o) { + log(reprint, StdStream.OUT, o); + + } + + public void logErrorStream(Object o) { + log(false, StdStream.ERR, o); + + } + + public void logErrorStream(boolean reprint, Object o) { + log(reprint, StdStream.ERR, o); + + } + + public void log(StdStream stream, Object o) { + log(false, stream, o); + + } + + public void log(boolean reprint, StdStream stream, Object o) { + //log to system + logToSystemLogger(stream, o); + //reuse old logger - finally will log something + //on/off wia itw-settings (enableLogging) + logToUserFile(stream, o); + //log to streams if should + if (reprint || isDebug() || force) { + logToStreams(stream, o); + } + } + + private void logToStreams(StdStream stream, Object o) { + if (o == null) { + logToStream(stream, getHeader(stream) + " " + NULL_OBJECT); + } else if (o instanceof Throwable) { + printException((Throwable) o); + } else { + logToStream(stream, o.toString()); + } + } + + private void logToUserFile(StdStream stream, Object o) { + if (o == null) { + AppletLog.log(getHeader(stream) + " " + NULL_OBJECT); + } else if (o instanceof Throwable) { + AppletLog.log((Throwable) o); + } else { + AppletLog.log(o.toString()); + } + } + + public void logToStream(StdStream stream, String o) { + if (stream == null) { + return; + } + switch (stream) { + case OUT: + printOutLn(o); + break; + case ERR: + printErrorLn(o); + break; + case BOTH: + printBothLn(o); + break; + } + } + + public boolean isDebug() { + return JNLPRuntime.isDebug(); + } + + public void printErrorLn(String e) { + err.println(e); + + } + + public void printOutLn(String e) { + out.println(e); + + } + + public void printBothLn(String e) { + printOutLn(e); + printErrorLn(e); + } + + public void printError(String e) { + err.print(e); + + } + + public void printOut(String e) { + out.print(e); + + } + + public void printBoth(String e) { + printOut(e); + printError(e); + } + + private void printException(Throwable e) { + e.printStackTrace(err); + + } + + private String getHeader(StdStream stream) { + StringBuilder sb = new StringBuilder(); + if (JNLPRuntime.isWebstartApplication()) { + sb.append("[ITW-JAVAWS]"); + } else { + sb.append("[ITW]"); + } + if (stream != null) { + sb.append('[').append(stream.toString()).append(']'); + } + sb.append('[').append(new Date().toString()).append(']'); + sb.append('[').append(getCallerClass()).append(']'); + return sb.toString(); + + } + + private static String getCallerClass() { + StackTraceElement[] stack = Thread.currentThread().getStackTrace(); + //0 is always thread + //1..? is ItwLogger itself + //pick up first after. + StackTraceElement result = stack[0]; + String baseClass = stack[1].getClassName(); + int i = 1; + for (; i < stack.length; i++) { + result = stack[i];//at least moving up + if (stack[i].getClassName().contains(ItwLogger.class.getName())) { + continue; + } else { + break; + } + } + return result.toString(); + } +}