home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / ccs_util.jar / com / commerceone / util / debug / Debug.class (.txt) next >
Encoding:
Java Class File  |  1999-12-09  |  5.4 KB  |  181 lines

  1. package com.commerceone.util.debug;
  2.  
  3. import com.commerceone.util.contract.Contract;
  4. import java.io.OutputStream;
  5. import java.io.PrintWriter;
  6. import java.io.Writer;
  7.  
  8. public class Debug implements Logger {
  9.    private boolean useFileAndNum;
  10.    private boolean loggingEnabled;
  11.    private PrintWriter logWriter;
  12.    private OutputStream logStream;
  13.    private String logPrefix;
  14.    private int logLevel;
  15.    public static Debug log = new Debug();
  16.  
  17.    public synchronized void setLogStream(OutputStream os) {
  18.       Contract.require(os != null);
  19.       this.logStream = os;
  20.       this.logWriter = new PrintWriter(os, true);
  21.    }
  22.  
  23.    public OutputStream getLogStream() {
  24.       return this.logStream;
  25.    }
  26.  
  27.    public void debug(String msg) {
  28.       this.logIt(0, this.useFileAndNum, msg);
  29.    }
  30.  
  31.    public static void initDebug() {
  32.       log = new Debug();
  33.    }
  34.  
  35.    public void debug(Object obj) {
  36.       this.debug(obj.toString());
  37.    }
  38.  
  39.    public String getPrefix() {
  40.       return this.logPrefix;
  41.    }
  42.  
  43.    public synchronized void setLogWriter(Writer w) {
  44.       Contract.require(w != null);
  45.       this.logWriter = new PrintWriter(w, true);
  46.    }
  47.  
  48.    public PrintWriter getLogWriter() {
  49.       return this.logWriter;
  50.    }
  51.  
  52.    public int resetLevel() {
  53.       int oldlevel = this.logLevel;
  54.       this.logLevel = 0;
  55.       return oldlevel;
  56.    }
  57.  
  58.    public int setLevel(int iLevel) {
  59.       int oldlevel = this.logLevel;
  60.       this.logLevel = iLevel;
  61.       return oldlevel;
  62.    }
  63.  
  64.    public int getLevel() {
  65.       return this.logLevel;
  66.    }
  67.  
  68.    public synchronized void printStackTrace(Throwable t) {
  69.       t.printStackTrace(this.logWriter);
  70.    }
  71.  
  72.    public synchronized void printStackTrace() {
  73.       this.logWriter.println(new StackTrace(200));
  74.    }
  75.  
  76.    public boolean isEnabled() {
  77.       return this.loggingEnabled;
  78.    }
  79.  
  80.    public void fatal(String msg) {
  81.       this.logIt(5, this.useFileAndNum, msg);
  82.    }
  83.  
  84.    public void fatal(Object obj) {
  85.       this.fatal(obj.toString());
  86.    }
  87.  
  88.    protected Debug() {
  89.       this((OutputStream)System.err, "Debug", true);
  90.    }
  91.  
  92.    protected Debug(RegexFilterWriter rfw) {
  93.       this(rfw, "Debug", true);
  94.    }
  95.  
  96.    protected Debug(RegexFilterWriter rfw, String prefix, boolean useFileAndNum) {
  97.       this.useFileAndNum = true;
  98.       this.loggingEnabled = true;
  99.       this.logWriter = null;
  100.       this.logStream = null;
  101.       this.logPrefix = "Debug";
  102.       this.logLevel = 0;
  103.       this.logStream = rfw.getOutputStream();
  104.       this.logPrefix = prefix;
  105.       this.setLogParams(prefix, useFileAndNum);
  106.       this.setLogWriter(new PrintWriter(rfw, true));
  107.    }
  108.  
  109.    protected Debug(OutputStream os) {
  110.       this(os, "Debug", true);
  111.    }
  112.  
  113.    public void info(String msg) {
  114.       this.logIt(1, this.useFileAndNum, msg);
  115.    }
  116.  
  117.    protected Debug(OutputStream os, String prefix, boolean useFileAndNum) {
  118.       this.useFileAndNum = true;
  119.       this.loggingEnabled = true;
  120.       this.logWriter = null;
  121.       this.logStream = null;
  122.       this.logPrefix = "Debug";
  123.       this.logLevel = 0;
  124.       this.logStream = os;
  125.       this.logPrefix = prefix;
  126.       this.setLogParams(prefix, useFileAndNum);
  127.       this.setLogWriter(new PrintWriter(os, true));
  128.    }
  129.  
  130.    public void warn(String msg) {
  131.       this.logIt(2, this.useFileAndNum, msg);
  132.    }
  133.  
  134.    public void warn(Object obj) {
  135.       this.warn(obj.toString());
  136.    }
  137.  
  138.    public void info(Object obj) {
  139.       this.info(obj.toString());
  140.    }
  141.  
  142.    private synchronized void logIt(int level, boolean useFileAndNum, String msg) {
  143.       if (this.loggingEnabled && this.logLevel <= level) {
  144.          this.logWriter.println((useFileAndNum ? (new StackTrace(1, 2)).getFileAndLineNum() + ": " : "") + this.logPrefix + "." + Logger.dbgNames[level] + ": " + msg);
  145.       }
  146.    }
  147.  
  148.    public void crit(String msg) {
  149.       this.logIt(4, this.useFileAndNum, msg);
  150.    }
  151.  
  152.    public void crit(Object obj) {
  153.       this.crit(obj.toString());
  154.    }
  155.  
  156.    public void setEnabled(boolean state) {
  157.       this.loggingEnabled = state;
  158.    }
  159.  
  160.    public synchronized void setLogParams(String newPrefix, boolean useFileAndNum) {
  161.       Contract.require(newPrefix != null);
  162.       if (newPrefix.compareTo("") != 0) {
  163.          this.logPrefix = newPrefix;
  164.       }
  165.  
  166.       this.useFileAndNum = useFileAndNum;
  167.    }
  168.  
  169.    public void error(String msg) {
  170.       this.logIt(3, this.useFileAndNum, msg);
  171.    }
  172.  
  173.    public void error(Object obj) {
  174.       this.error(obj.toString());
  175.    }
  176.  
  177.    public static synchronized Debug getDefaultInstance() {
  178.       return log;
  179.    }
  180. }
  181.