1 52 53 package com.go.trove.log; 54 55 import java.io.*; 56 import java.util.*; 57 import java.lang.ref.WeakReference ; 58 59 70 public class LogEvent extends EventObject { 71 72 public static final int DEBUG_TYPE = 1; 73 74 75 public static final int INFO_TYPE = 2; 76 77 78 public static final int WARN_TYPE = 3; 79 80 81 public static final int ERROR_TYPE = 4; 82 83 private int mType; 84 private Date mTimestamp; 85 private String mMessage; 86 private Throwable mThrowable; 87 private String mThreadName; 88 private transient WeakReference mThread; 90 91 public LogEvent(Log log, int type, 92 String message, Throwable throwable, 93 Thread thread, Date timestamp) { 94 super(log); 95 96 if (type < DEBUG_TYPE || type > ERROR_TYPE) { 97 throw new IllegalArgumentException 98 ("Type out of range: " + type); 99 } 100 101 mType = type; 102 103 if (message == null) { 104 if (throwable != null) { 105 mMessage = throwable.getMessage(); 106 } 107 } 108 else { 109 mMessage = message; 110 } 111 112 mThrowable = throwable; 113 114 if (thread == null) { 115 mThread = new WeakReference (Thread.currentThread()); 116 } 117 else { 118 mThread = new WeakReference (thread); 119 } 120 121 if (timestamp == null) { 122 mTimestamp = new Date(); 123 } 124 else { 125 mTimestamp = timestamp; 126 } 127 } 128 129 public LogEvent(Log log, int type, 130 String message, Thread thread, Date timestamp) { 131 this(log, type, message, null, thread, timestamp); 132 } 133 134 public LogEvent(Log log, int type, 135 Throwable throwable, Thread thread, Date timestamp) { 136 this(log, type, null, throwable, thread, timestamp); 137 } 138 139 public LogEvent(Log log, int type, 140 String message, Thread thread) { 141 this(log, type, message, null, thread, null); 142 } 143 144 public LogEvent(Log log, int type, 145 Throwable throwable, Thread thread) { 146 this(log, type, null, throwable, thread, null); 147 } 148 149 public LogEvent(Log log, int type, 150 String message, Throwable throwable) { 151 this(log, type, message, throwable, null, null); 152 } 153 154 public LogEvent(Log log, int type, String message) { 155 this(log, type, message, null, null, null); 156 } 157 158 public LogEvent(Log log, int type, Throwable throwable) { 159 this(log, type, null, throwable, null, null); 160 } 161 162 public Log getLogSource() { 163 return (Log)getSource(); 164 } 165 166 170 public int getType() { 171 return mType; 172 } 173 174 177 public Date getTimestamp() { 178 return mTimestamp; 179 } 180 181 184 public String getMessage() { 185 return mMessage; 186 } 187 188 191 public Throwable getException() { 192 return mThrowable; 193 } 194 195 198 public String getExceptionStackTrace() { 199 Throwable t = getException(); 200 if (t == null) { 201 return null; 202 } 203 StringWriter sw = new StringWriter(); 204 PrintWriter pw = new PrintWriter(sw); 205 t.printStackTrace(pw); 206 return sw.toString(); 207 } 208 209 212 public String getThreadName() { 213 if (mThreadName == null) { 214 Thread t = getThread(); 215 if (t != null) { 216 mThreadName = t.getName(); 217 } 218 } 219 return mThreadName; 220 } 221 222 226 public Thread getThread() { 227 return (Thread )mThread.get(); 228 } 229 230 public String toString() { 231 String msg; 232 if (getMessage() == null) { 233 msg = "null"; 234 } 235 else { 236 msg = '"' + getMessage() + '"'; 237 } 238 239 return 240 getClass().getName() + "[" + 241 getTimestamp() + ',' + 242 getThreadName() + ',' + 243 msg + 244 "] from " + getSource(); 245 } 246 247 private void writeObject(ObjectOutputStream out) throws IOException { 248 getThreadName(); 249 out.defaultWriteObject(); 250 } 251 252 private void readObject(ObjectInputStream in) 253 throws IOException, ClassNotFoundException { 254 in.defaultReadObject(); 255 } 256 } 257 | Popular Tags |