// XAAL toolkit // Copyright (C) 2009 Ville Karavirta // // 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 3 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. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . package xaal.util; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.util.Date; public class XaalErrorLog { public static final int DEBUG = 0; public static final int INFO = 1; public static final int WARN = 2; public static final int ERROR = 3; public static final int FATAL = 4; public static final int NONE = 5; private int level = DEBUG; private PrintWriter out; public XaalErrorLog(OutputStream out) { super(); this.out = new PrintWriter(out); } public XaalErrorLog(Writer out) { super(); this.out = new PrintWriter(out); } public void debug(String msg) { if (isDebugLevel()) out.println(new Date() + " unknown source [DEBUG] " + msg); } public void debug(Object loggingClass, String msg) { if (isDebugLevel()) out.println(new Date() + " " + loggingClass.getClass().getName() + " [DEBUG] " + msg); } public void debug(Object loggingClass, String msg, Throwable t) { if (isDebugLevel()) { out.println(new Date() + " " + loggingClass.getClass().getName() + " [DEBUG] " + msg); StackTraceElement[] st = t.getStackTrace(); for (int i = 0; i < st.length; i++) { out.println("\t" + st[i]); } } } public void info(String msg) { if (isInfoLevel()) out.println(new Date() + " unknown source [INFO] " + msg); } public void info(Object loggingClass, String msg) { if (isInfoLevel()) out.println(new Date() + " " + loggingClass.getClass().getName() + " [INFO] " + msg); } public void info(Object loggingClass, String msg, Throwable t) { if (isInfoLevel()) { out.println(new Date() + " " + loggingClass.getClass().getName() + " [INFO] " + msg); StackTraceElement[] st = t.getStackTrace(); for (int i = 0; i < st.length; i++) { out.println("\t" + st[i]); } } } public void warn(String msg) { if (isWarnLevel()) out.println(new Date() + " unknown source [WARN] " + msg); } public void warn(Object loggingClass, String msg) { if (isWarnLevel()) out.println(new Date() + " " + loggingClass.getClass().getName() + " [WARN] " + msg); } public void warn(Object loggingClass, String msg, Throwable t) { if (isWarnLevel()) { out.println(new Date() + " " + loggingClass.getClass().getName() + " [WARN] " + msg); StackTraceElement[] st = t.getStackTrace(); for (int i = 0; i < st.length; i++) { out.println("\t" + st[i]); } } } public void error(String msg) { if (isErrorLevel()) out.println(new Date() + " unknown source [ERROR] " + msg); } public void error(Object loggingClass, String msg) { if (isErrorLevel()) out.println(new Date() + " " + loggingClass.getClass().getName() + " [ERROR] " + msg); } public void error(Object loggingClass, String msg, Throwable t) { if (isErrorLevel()) { out.println(new Date() + " " + loggingClass.getClass().getName() + " [ERROR] " + msg); StackTraceElement[] st = t.getStackTrace(); for (int i = 0; i < st.length; i++) { out.println("\t" + st[i]); } } } public void fatal(String msg) { if (isFatalLevel()) out.println(new Date() + " unknown source [FATAL] " + msg); } public void fatal(Object loggingClass, String msg) { if (isFatalLevel()) out.println(new Date() + " " + loggingClass.getClass().getName() + " [FATAL] " + msg); } public void fatal(Object loggingClass, String msg, Throwable t) { if (isFatalLevel()) { out.println(new Date() + " " + loggingClass.getClass().getName() + " [FATAL] " + msg); StackTraceElement[] st = t.getStackTrace(); for (int i = 0; i < st.length; i++) { out.println("\t" + st[i]); } } } public void setLevel(int level) { if (level != DEBUG && level != INFO && level != WARN && level != ERROR && level != FATAL) this.level = NONE; else this.level = level; } public boolean isDebugLevel() { return (level <= DEBUG); } public boolean isInfoLevel() { return (level <= INFO); } public boolean isWarnLevel() { return (level <= WARN); } public boolean isErrorLevel() { return (level <= ERROR); } public boolean isFatalLevel() { return (level <= FATAL); } public Writer getWriter() { return out; } }