001: // You can redistribute this software and/or modify it under the terms of
002: // the Infozone Software License version 2 published by the Infozone Group
003: // (http://www.infozone-group.org).
004: //
005: // Copyright (C) @year@ by The Infozone Group. All rights reserved.
006: //
007: // $Id: LogWriter.java,v 1.1 2002/05/10 08:59:12 per_nyfelt Exp $
008:
009: package org.infozone.tools.logger;
010:
011: import java.io.*;
012: import java.util.*;
013:
014: /**
015: @author <a href="http://www.softwarebuero.de/">SMB</a>
016: @version $Revision: 1.1 $Date: 2002/05/10 08:59:12 $
017: */
018: public final class LogWriter {
019:
020: public static final int INFO = 1;
021: public static final int WARN = 2;
022: public static final int ERROR = 4;
023: public static final int DEBUG = 8;
024: public static final int DEBUG2 = 16;
025: public static final int DEBUG3 = 32;
026:
027: protected static final String INFO_PREFIX = "[info] ";
028: protected static final String WARN_PREFIX = "[warn] ";
029: protected static final String ERROR_PREFIX = "[error]";
030: protected static final String DEBUG_PREFIX = "[debug]";
031:
032: public static final int LINE_WIDTH = 70;
033:
034: protected Vector logTargets;
035:
036: protected int allLevels;
037:
038: public LogWriter() {
039: logTargets = new Vector();
040: }
041:
042: /**
043: This method allows to quickly find out if there is any log target that
044: would receive entries of the specified level.
045: */
046: public boolean hasTarget(int level) {
047: return (allLevels & level) > 0;
048: }
049:
050: public void addLogTarget(OutputStream _out, int _levels) {
051: // set also lower debug levels when higher levels are requested
052: _levels = ((_levels & DEBUG3) > 0) ? (_levels | DEBUG2)
053: : _levels;
054: _levels = ((_levels & DEBUG2) > 0) ? (_levels | DEBUG)
055: : _levels;
056:
057: addLogTarget(new PrintWriter(_out), _levels);
058: }
059:
060: public void addLogTarget(PrintWriter _writer, int _levels) {
061: // set also lower debug levels when higher levels are requested
062: _levels = ((_levels & DEBUG3) > 0) ? (_levels | DEBUG2)
063: : _levels;
064: _levels = ((_levels & DEBUG2) > 0) ? (_levels | DEBUG)
065: : _levels;
066:
067: logTargets.addElement(new LogTarget(_writer, _levels));
068: allLevels = allLevels | _levels;
069: }
070:
071: public void newEntry(Object sender, String msg, int levels) {
072: newEntry(sender, msg, null, levels);
073: }
074:
075: public void newEntry(Object sender, String msg, Throwable e,
076: int levels) {
077: //check if at least one of the specified levels is supported by
078: //any of the LogTargets
079: if ((allLevels & levels) > 0) {
080: for (int i = 0; i < logTargets.size(); i++) {
081: LogTarget logTarget = (LogTarget) logTargets
082: .elementAt(i);
083: if ((logTarget.levels & levels & INFO) > 0)
084: printToWriter(logTarget.writer, sender, msg,
085: INFO_PREFIX, e);
086: if ((logTarget.levels & levels & WARN) > 0)
087: printToWriter(logTarget.writer, sender, msg,
088: WARN_PREFIX, e);
089: if ((logTarget.levels & levels & ERROR) > 0)
090: printToWriter(logTarget.writer, sender, msg,
091: ERROR_PREFIX, e);
092: if ((logTarget.levels & levels & (DEBUG | DEBUG2 | DEBUG3)) > 0)
093: printToWriter(logTarget.writer, sender, msg,
094: DEBUG_PREFIX, e);
095: }
096: }
097: }
098:
099: protected void printToWriter(PrintWriter writer, Object sender,
100: String msg, String status, Throwable e) {
101: StringBuffer sb = new StringBuffer(120);
102: sb.append(status);
103: sb.append('(');
104: String threadNum = String.valueOf(Thread.currentThread()
105: .hashCode());
106: sb.append(threadNum.substring(threadNum.length() - 3));
107: sb.append(") ");
108: sb.append(rawClassName(sender));
109: sb.append(": ");
110: sb.append(msg);
111: writer.println(sb);
112: writer.flush();
113: if (e != null) {
114: // writer.print (" exception: " + e.getMessage());
115: printFormatedException(writer, e, LINE_WIDTH, " ");
116: }
117: }
118:
119: public static String rawClassName(Object obj) {
120: if (obj != null) {
121: String name = (obj instanceof Class) ? ((Class) obj)
122: .getName() : obj.getClass().getName();
123: int index = name.lastIndexOf('.');
124: return name.substring(index + 1);
125: } else
126: return "(null)";
127: }
128:
129: protected void printFormatedException(OutputStream out,
130: Throwable e, int lineWidth, String pre) {
131: printFormatedException(new PrintWriter(out), e, lineWidth, pre);
132: }
133:
134: protected void printFormatedException(PrintWriter out, Throwable e,
135: int lineWidth, String pre) {
136: StringWriter buff = new StringWriter();
137: e.printStackTrace(new PrintWriter(buff));
138: StringTokenizer st = new StringTokenizer(buff.toString(), "\n");
139: while (st.hasMoreTokens()) {
140: String line = st.nextToken();
141: boolean firstSubLine = true;
142: // while (line.length() > lineWidth) {
143: do {
144: int subLineLength = Math.min(lineWidth, line.length());
145: String subLine = line.substring(0, subLineLength);
146: line = line.substring(subLineLength);
147: if (!firstSubLine)
148: out.print(" ");
149: out.print(pre);
150: out.println(subLine);
151: firstSubLine = false;
152: } while (line.length() > 0);
153: }
154: out.flush();
155: }
156:
157: }
|