01: package org.uispec4j.utils;
02:
03: import java.io.PrintWriter;
04: import java.io.StringWriter;
05:
06: /**
07: * Simple log class used sporadically for debugging sessions
08: */
09: ///CLOVER:OFF
10: public class Log {
11: private static LogWriter writer = new NullWriter();
12: public static final int THREAD_NAME_SIZE = 45;
13:
14: public static void reset() {
15: writer.reset();
16: }
17:
18: public static void dump() {
19: writer.dump();
20: }
21:
22: public static void write(String message) {
23: writer.write(Utils.normalize(Thread.currentThread().getName(),
24: THREAD_NAME_SIZE));
25: writer.write(" ");
26: writer.write(message);
27: writer.write(Utils.LINE_SEPARATOR);
28: }
29:
30: public static void writeStack(String name) {
31: writer.writeStack(name);
32: }
33:
34: private static interface LogWriter {
35: void reset();
36:
37: void write(String message);
38:
39: void dump();
40:
41: void writeStack(String name);
42: }
43:
44: private static abstract class AbstractLogWriter implements
45: LogWriter {
46: public void writeStack(String name) {
47: Exception e = new Exception();
48: StringWriter stringWriter = new StringWriter();
49: PrintWriter writer = new PrintWriter(stringWriter);
50: e.printStackTrace(writer);
51: write(name + Utils.LINE_SEPARATOR + stringWriter.toString());
52: }
53: }
54:
55: private static class SystemOutWriter extends AbstractLogWriter {
56: public void reset() {
57: }
58:
59: public void write(String message) {
60: System.out.print(message);
61: }
62:
63: public void dump() {
64: }
65: }
66:
67: private static class StringBufferWriter extends AbstractLogWriter {
68: private StringBuffer buffer = new StringBuffer();
69:
70: public void reset() {
71: buffer.setLength(0);
72: }
73:
74: public void write(String message) {
75: buffer.append(message);
76: }
77:
78: public void dump() {
79: System.out.println(buffer.toString());
80: }
81: }
82:
83: private static class NullWriter implements LogWriter {
84: public void reset() {
85: }
86:
87: public void write(String message) {
88: }
89:
90: public void dump() {
91: }
92:
93: public void writeStack(String name) {
94: }
95: }
96: }
|