Mercurial > hg4j
comparison src/org/tmatesoft/hg/internal/StreamLogFacility.java @ 295:981f9f50bb6c
Issue 11: Error log facility. SessionContext to share common facilities
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
|---|---|
| date | Fri, 16 Sep 2011 05:35:32 +0200 |
| parents | |
| children | 909306e412e2 |
comparison
equal
deleted
inserted
replaced
| 294:32890bab7209 | 295:981f9f50bb6c |
|---|---|
| 1 /* | |
| 2 * Copyright (c) 2011 TMate Software Ltd | |
| 3 * | |
| 4 * This program is free software; you can redistribute it and/or modify | |
| 5 * it under the terms of the GNU General Public License as published by | |
| 6 * the Free Software Foundation; version 2 of the License. | |
| 7 * | |
| 8 * This program is distributed in the hope that it will be useful, | |
| 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 11 * GNU General Public License for more details. | |
| 12 * | |
| 13 * For information on how to redistribute this software under | |
| 14 * the terms of a license other than GNU General Public License | |
| 15 * contact TMate Software at support@hg4j.com | |
| 16 */ | |
| 17 package org.tmatesoft.hg.internal; | |
| 18 | |
| 19 import java.io.PrintStream; | |
| 20 | |
| 21 import org.tmatesoft.hg.util.LogFacility; | |
| 22 | |
| 23 /** | |
| 24 * Primitive implementation of {@link LogFacility} that directs all output to specified {@link PrintStream}. | |
| 25 * | |
| 26 * @author Artem Tikhomirov | |
| 27 * @author TMate Software Ltd. | |
| 28 */ | |
| 29 public class StreamLogFacility implements LogFacility { | |
| 30 | |
| 31 private final boolean isDebug; | |
| 32 private final boolean isInfo; | |
| 33 protected final boolean timestamp; | |
| 34 protected final PrintStream outStream; | |
| 35 | |
| 36 public StreamLogFacility(boolean pringDebug, boolean printInfo, boolean needTimestamp, PrintStream out) { | |
| 37 isDebug = pringDebug; | |
| 38 isInfo = printInfo; | |
| 39 timestamp = needTimestamp; | |
| 40 outStream = out; | |
| 41 } | |
| 42 | |
| 43 public boolean isDebug() { | |
| 44 return isDebug; | |
| 45 } | |
| 46 | |
| 47 public boolean isInfo() { | |
| 48 return isInfo; | |
| 49 } | |
| 50 | |
| 51 public void debug(Class<?> src, String format, Object... args) { | |
| 52 if (!isDebug) { | |
| 53 return; | |
| 54 } | |
| 55 printf("DEBUG", src, format, args); | |
| 56 } | |
| 57 | |
| 58 public void info(Class<?> src, String format, Object... args) { | |
| 59 if (!isInfo) { | |
| 60 return; | |
| 61 } | |
| 62 printf("INFO", src, format, args); | |
| 63 } | |
| 64 | |
| 65 public void warn(Class<?> src, String format, Object... args) { | |
| 66 printf("WARN", src, format, args); | |
| 67 } | |
| 68 | |
| 69 public void error(Class<?> src, String format, Object... args) { | |
| 70 printf("ERROR", src, format, args); | |
| 71 } | |
| 72 | |
| 73 public void debug(Class<?> src, Throwable th, String message) { | |
| 74 if (!isDebug) { | |
| 75 return; | |
| 76 } | |
| 77 printf("DEBUG", src, th, message); | |
| 78 } | |
| 79 | |
| 80 public void info(Class<?> src, Throwable th, String message) { | |
| 81 if (!isInfo) { | |
| 82 return; | |
| 83 } | |
| 84 printf("INFO", src, th, message); | |
| 85 } | |
| 86 | |
| 87 public void warn(Class<?> src, Throwable th, String message) { | |
| 88 printf("WARN", src, th, message); | |
| 89 } | |
| 90 | |
| 91 public void error(Class<?> src, Throwable th, String message) { | |
| 92 printf("ERROR", src, th, message); | |
| 93 } | |
| 94 | |
| 95 protected void printf(String level, Class<?> src, String format, Object... args) { | |
| 96 String msg = String.format(format, args); | |
| 97 if (timestamp) { | |
| 98 outStream.printf(isDebug ? "%tT.%1$tL " : "%tT ", System.currentTimeMillis()); | |
| 99 } | |
| 100 if (isDebug) { | |
| 101 String cn = src.getName(); | |
| 102 if (cn.startsWith("org.tmatesoft.hg.")) { | |
| 103 cn = "oth." + cn.substring("org.tmatesoft.hg.".length()); | |
| 104 } | |
| 105 outStream.printf("(%s) ", cn); | |
| 106 } | |
| 107 outStream.printf("%s: %s", level, msg); | |
| 108 if (format.length() == 0 || format.charAt(format.length() - 1) != '\n') { | |
| 109 outStream.println(); | |
| 110 } | |
| 111 } | |
| 112 protected void printf(String level, Class<?> src, Throwable th, String msg) { | |
| 113 if (msg != null || timestamp || isDebug || th == null) { | |
| 114 printf(level, src, msg == null ? "" : msg, (Object[]) null); | |
| 115 } | |
| 116 if (th != null) { | |
| 117 if (isDebug || isInfo) { | |
| 118 // full stack trace | |
| 119 th.printStackTrace(outStream); | |
| 120 } else { | |
| 121 // just title of the exception | |
| 122 outStream.printf("%s: %s\n", th.getClass().getName(), th.getMessage()); | |
| 123 } | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 // alternative to hardcore System.out where SessionContext is not available now (or ever) | |
| 128 public static LogFacility newDefault() { | |
| 129 return new StreamLogFacility(true, true, true, System.out); | |
| 130 } | |
| 131 } |
