1 9 package javolution.context; 10 11 import javolution.util.StandardLog; 12 import j2me.lang.CharSequence; 13 14 77 public abstract class LogContext extends Context { 78 79 85 public static final StandardLog STANDARD = new StandardLog(); 86 87 90 public static final LogContext NULL = new NullLog(); 91 92 97 public static final LogContext SYSTEM = new SystemLog(); 98 99 103 public static final LogContext SYSTEM_ERR = new SystemErrLog(); 104 105 108 private static volatile LogContext Default = STANDARD; 109 110 113 public LogContext() { 114 } 115 116 122 public staticContext current() { 123 for (Context ctx = Context.current(); ctx != null; ctx = ctx.getOuter()) { 124 if (ctx instanceof LogContext) 125 return (LogContext) ctx; 126 } 127 return LogContext.Default; 128 } 129 130 136 public static LogContext getDefault() { 137 return LogContext.Default; 138 } 139 140 145 public static void setDefault(LogContext defaultLog) { 146 LogContext.Default = defaultLog; 147 } 148 149 155 public static void info(CharSequence message) { 156 LogContext logContext = (LogContext) LogContext.current(); 157 logContext.logInfo(message); 158 } 159 160 166 public static void warning(CharSequence message) { 167 LogContext logContext = (LogContext) LogContext.current(); 168 logContext.logWarning(message); 169 } 170 171 176 public static void error(Throwable error) { 177 LogContext logContext = (LogContext) LogContext.current(); 178 logContext.logError(error, null); 179 } 180 181 188 public static void error(Throwable error, CharSequence message) { 189 LogContext logContext = (LogContext) LogContext.current(); 190 logContext.logError(error, message); 191 } 192 193 199 public abstract boolean isInfoLogged(); 200 201 206 public abstract void logInfo(CharSequence message); 207 208 214 public abstract boolean isWarningLogged(); 215 216 221 public abstract void logWarning(CharSequence message); 222 223 229 public abstract boolean isErrorLogged(); 230 231 237 public abstract void logError(Throwable error, CharSequence message); 238 239 protected void enterAction() { 241 } 243 244 protected void exitAction() { 246 } 248 249 252 private static class NullLog extends LogContext { 253 254 public boolean isInfoLogged() { 255 return false; 256 } 257 258 public boolean isWarningLogged() { 259 return false; 260 } 261 262 public boolean isErrorLogged() { 263 return false; 264 } 265 266 public void logInfo(CharSequence message) { 267 } 269 270 public void logWarning(CharSequence message) { 271 } 273 274 public void logError(Throwable error, CharSequence message) { 275 } 277 } 278 279 282 private static class SystemLog extends LogContext { 283 284 public boolean isInfoLogged() { 285 return true; 286 } 287 288 public boolean isWarningLogged() { 289 return true; 290 } 291 292 public boolean isErrorLogged() { 293 return true; 294 } 295 296 public void logInfo(CharSequence message) { 297 System.out.print("[info] "); 298 System.out.println(message); 299 } 300 301 public void logWarning(CharSequence message) { 302 System.err.print("[warning] "); 303 System.err.println(message); 304 } 305 306 public void logError(Throwable error, CharSequence message) { 307 System.err.print("[error] "); 308 System.err.print(error.getClass().getName()); 309 if (message != null) { 310 System.err.print(" - "); 311 System.err.println(message); 312 } else { 313 String errorMessage = error.getMessage(); 314 if (errorMessage != null) { 315 System.err.print(" - "); 316 System.err.println(errorMessage); 317 } else { 318 System.err.println(); 319 } 320 } 321 error.printStackTrace(); 322 } 323 } 324 325 328 private static class SystemErrLog extends LogContext { 329 330 public boolean isInfoLogged() { 331 return false; 332 } 333 334 public boolean isWarningLogged() { 335 return true; 336 } 337 338 public boolean isErrorLogged() { 339 return true; 340 } 341 342 public void logInfo(CharSequence message) { 343 } 345 346 public void logWarning(CharSequence message) { 347 System.err.print("[warning] "); 348 System.err.println(message); 349 } 350 351 public void logError(Throwable error, CharSequence message) { 352 System.err.print("[error] "); 353 System.err.print(error.getClass().getName()); 354 if (message != null) { 355 System.err.print(" - "); 356 System.err.println(message); 357 } else { 358 String errorMessage = error.getMessage(); 359 if (errorMessage != null) { 360 System.err.print(" - "); 361 System.err.println(errorMessage); 362 } else { 363 System.err.println(); 364 } 365 } 366 error.printStackTrace(); 367 } 368 } 369 } | Popular Tags |