001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.harness;
007:
008: import java.util.StringTokenizer;
009: import java.util.Date;
010:
011: import java.text.DateFormat;
012:
013: import java.io.ByteArrayOutputStream;
014: import java.io.PrintStream;
015: import java.io.FileOutputStream;
016:
017: public class LogView {
018:
019: static public final short NOTHING = 0;
020: static public final short ERROR = 3;
021: static public final short WARNING = 2;
022: static public final short MESSAGE = 1;
023:
024: LogView(String dir, String nm, short level) {
025: m_Name = nm;
026: m_Level = level;
027: m_DFmt = DateFormat.getInstance();
028:
029: // Yes, we know these are deprecated. This name is being constructed to
030: // avoid huge log files. We aren't interested in being that correct in terms
031: // of localized dates.
032:
033: Date dt = new Date();
034: String fn = dir + "/" + nm + (dt.getMonth() + 1) + "_"
035: + dt.getDate() + "_" + (dt.getYear() + 1900) + ".dbg";
036:
037: try {
038: m_Stream = new PrintStream(new FileOutputStream(fn, true),
039: true);
040: } catch (Exception ex) {
041: System.err.println("Error opening " + fn + ": " + ex);
042: System.err
043: .println("Writing debug output to standard error.");
044: m_Stream = System.err;
045: }
046: dateStamp("New test session.");
047: }
048:
049: public String getName() {
050: return m_Name;
051: }
052:
053: public short getLevel() {
054: return m_Level;
055: }
056:
057: void setLevel(short level) {
058: m_Level = level;
059: }
060:
061: public String getCurrentOutput() {
062: return m_Buf.toString();
063: }
064:
065: // we clearCurrentOutput after presenting it to the user. At
066: // that point, we can collectively stream output.
067:
068: public void clearCurrentOutput() {
069: if (m_Buf.length() > 0) {
070: dateStamp("Web event.");
071: m_Stream.println(m_Buf.toString());
072: m_Buf.setLength(0);
073: }
074: }
075:
076: void message(short level, Object o) {
077: if (m_Level > level) {
078: return;
079: }
080: String pfx = null;
081: switch (level) {
082: case ERROR:
083: pfx = "Error: ";
084: break;
085: case WARNING:
086: pfx = "Warning: ";
087: break;
088: case MESSAGE:
089: pfx = "Message: ";
090: break;
091: default:
092: pfx = "Level " + level + ": ";
093: break;
094: }
095: StringTokenizer toks = new StringTokenizer(o.toString(), "\r\n");
096: while (toks.hasMoreTokens()) {
097: m_Buf.append(pfx);
098: pfx = "\t";
099: m_Buf.append(toks.nextToken());
100: m_Buf.append("\n");
101: }
102: }
103:
104: void message(short level, Object o, Throwable t) {
105: if (m_Level > level) {
106: return;
107: }
108: message(level, o);
109: ByteArrayOutputStream bos = new ByteArrayOutputStream();
110: PrintStream ps = new PrintStream(bos);
111: ps.println("<<<<<<<< Stack Backtrace:");
112: t.printStackTrace(ps);
113: ps.println(">>>>>>>>");
114: ps.flush();
115: m_Buf.append(bos.toString());
116: }
117:
118: private void dateStamp(String msg) {
119: StringBuffer buf = new StringBuffer();
120: Date dt = new Date();
121:
122: buf.append("--- ");
123: buf.append(m_DFmt.format(dt));
124: buf.append(", ");
125: buf.append(msg);
126:
127: m_Stream.println("");
128: m_Stream.println(buf.toString());
129: }
130:
131: private String m_Name;
132: private short m_Level;
133: private StringBuffer m_Buf = new StringBuffer();
134: private PrintStream m_Stream;
135: private DateFormat m_DFmt;
136: }
|