home *** CD-ROM | disk | FTP | other *** search
- package com.commerceone.util.debug;
-
- import com.commerceone.util.contract.Contract;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.io.Writer;
-
- public class Debug implements Logger {
- private boolean useFileAndNum;
- private boolean loggingEnabled;
- private PrintWriter logWriter;
- private OutputStream logStream;
- private String logPrefix;
- private int logLevel;
- public static Debug log = new Debug();
-
- public synchronized void setLogStream(OutputStream os) {
- Contract.require(os != null);
- this.logStream = os;
- this.logWriter = new PrintWriter(os, true);
- }
-
- public OutputStream getLogStream() {
- return this.logStream;
- }
-
- public void debug(String msg) {
- this.logIt(0, this.useFileAndNum, msg);
- }
-
- public static void initDebug() {
- log = new Debug();
- }
-
- public void debug(Object obj) {
- this.debug(obj.toString());
- }
-
- public String getPrefix() {
- return this.logPrefix;
- }
-
- public synchronized void setLogWriter(Writer w) {
- Contract.require(w != null);
- this.logWriter = new PrintWriter(w, true);
- }
-
- public PrintWriter getLogWriter() {
- return this.logWriter;
- }
-
- public int resetLevel() {
- int oldlevel = this.logLevel;
- this.logLevel = 0;
- return oldlevel;
- }
-
- public int setLevel(int iLevel) {
- int oldlevel = this.logLevel;
- this.logLevel = iLevel;
- return oldlevel;
- }
-
- public int getLevel() {
- return this.logLevel;
- }
-
- public synchronized void printStackTrace(Throwable t) {
- t.printStackTrace(this.logWriter);
- }
-
- public synchronized void printStackTrace() {
- this.logWriter.println(new StackTrace(200));
- }
-
- public boolean isEnabled() {
- return this.loggingEnabled;
- }
-
- public void fatal(String msg) {
- this.logIt(5, this.useFileAndNum, msg);
- }
-
- public void fatal(Object obj) {
- this.fatal(obj.toString());
- }
-
- protected Debug() {
- this((OutputStream)System.err, "Debug", true);
- }
-
- protected Debug(RegexFilterWriter rfw) {
- this(rfw, "Debug", true);
- }
-
- protected Debug(RegexFilterWriter rfw, String prefix, boolean useFileAndNum) {
- this.useFileAndNum = true;
- this.loggingEnabled = true;
- this.logWriter = null;
- this.logStream = null;
- this.logPrefix = "Debug";
- this.logLevel = 0;
- this.logStream = rfw.getOutputStream();
- this.logPrefix = prefix;
- this.setLogParams(prefix, useFileAndNum);
- this.setLogWriter(new PrintWriter(rfw, true));
- }
-
- protected Debug(OutputStream os) {
- this(os, "Debug", true);
- }
-
- public void info(String msg) {
- this.logIt(1, this.useFileAndNum, msg);
- }
-
- protected Debug(OutputStream os, String prefix, boolean useFileAndNum) {
- this.useFileAndNum = true;
- this.loggingEnabled = true;
- this.logWriter = null;
- this.logStream = null;
- this.logPrefix = "Debug";
- this.logLevel = 0;
- this.logStream = os;
- this.logPrefix = prefix;
- this.setLogParams(prefix, useFileAndNum);
- this.setLogWriter(new PrintWriter(os, true));
- }
-
- public void warn(String msg) {
- this.logIt(2, this.useFileAndNum, msg);
- }
-
- public void warn(Object obj) {
- this.warn(obj.toString());
- }
-
- public void info(Object obj) {
- this.info(obj.toString());
- }
-
- private synchronized void logIt(int level, boolean useFileAndNum, String msg) {
- if (this.loggingEnabled && this.logLevel <= level) {
- this.logWriter.println((useFileAndNum ? (new StackTrace(1, 2)).getFileAndLineNum() + ": " : "") + this.logPrefix + "." + Logger.dbgNames[level] + ": " + msg);
- }
- }
-
- public void crit(String msg) {
- this.logIt(4, this.useFileAndNum, msg);
- }
-
- public void crit(Object obj) {
- this.crit(obj.toString());
- }
-
- public void setEnabled(boolean state) {
- this.loggingEnabled = state;
- }
-
- public synchronized void setLogParams(String newPrefix, boolean useFileAndNum) {
- Contract.require(newPrefix != null);
- if (newPrefix.compareTo("") != 0) {
- this.logPrefix = newPrefix;
- }
-
- this.useFileAndNum = useFileAndNum;
- }
-
- public void error(String msg) {
- this.logIt(3, this.useFileAndNum, msg);
- }
-
- public void error(Object obj) {
- this.error(obj.toString());
- }
-
- public static synchronized Debug getDefaultInstance() {
- return log;
- }
- }
-