Mercurial > jhg
comparison src/org/tmatesoft/hg/internal/StreamLogFacility.java @ 456:909306e412e2
Refactor LogFacility and SessionContext, better API for both
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
|---|---|
| date | Mon, 18 Jun 2012 16:54:00 +0200 |
| parents | 981f9f50bb6c |
| children |
comparison
equal
deleted
inserted
replaced
| 454:36fd1fd06492 | 456:909306e412e2 |
|---|---|
| 14 * the terms of a license other than GNU General Public License | 14 * the terms of a license other than GNU General Public License |
| 15 * contact TMate Software at support@hg4j.com | 15 * contact TMate Software at support@hg4j.com |
| 16 */ | 16 */ |
| 17 package org.tmatesoft.hg.internal; | 17 package org.tmatesoft.hg.internal; |
| 18 | 18 |
| 19 import static org.tmatesoft.hg.util.LogFacility.Severity.Info; | |
| 20 | |
| 19 import java.io.PrintStream; | 21 import java.io.PrintStream; |
| 20 | 22 |
| 21 import org.tmatesoft.hg.util.LogFacility; | 23 import org.tmatesoft.hg.util.LogFacility; |
| 22 | 24 |
| 23 /** | 25 /** |
| 27 * @author TMate Software Ltd. | 29 * @author TMate Software Ltd. |
| 28 */ | 30 */ |
| 29 public class StreamLogFacility implements LogFacility { | 31 public class StreamLogFacility implements LogFacility { |
| 30 | 32 |
| 31 private final boolean isDebug; | 33 private final boolean isDebug; |
| 32 private final boolean isInfo; | 34 private final Severity severity; |
| 33 protected final boolean timestamp; | 35 protected final boolean timestamp; |
| 34 protected final PrintStream outStream; | 36 protected final PrintStream outStream; |
| 35 | 37 |
| 36 public StreamLogFacility(boolean pringDebug, boolean printInfo, boolean needTimestamp, PrintStream out) { | 38 public StreamLogFacility(Severity level, boolean needTimestamp, PrintStream out) { |
| 37 isDebug = pringDebug; | 39 assert level != null; |
| 38 isInfo = printInfo; | 40 severity = level; |
| 41 isDebug = level == Severity.Debug; | |
| 39 timestamp = needTimestamp; | 42 timestamp = needTimestamp; |
| 40 outStream = out; | 43 outStream = out; |
| 41 } | 44 } |
| 42 | 45 |
| 43 public boolean isDebug() { | 46 public boolean isDebug() { |
| 44 return isDebug; | 47 return isDebug; |
| 45 } | 48 } |
| 46 | 49 |
| 47 public boolean isInfo() { | 50 public Severity getLevel() { |
| 48 return isInfo; | 51 return severity; |
| 49 } | 52 } |
| 50 | 53 |
| 51 public void debug(Class<?> src, String format, Object... args) { | 54 public void dump(Class<?> src, Severity severity, String format, Object... args) { |
| 52 if (!isDebug) { | 55 if (severity.ordinal() >= getLevel().ordinal()) { |
| 53 return; | 56 printf(severity, src, format, args); |
| 54 } | 57 } |
| 55 printf("DEBUG", src, format, args); | |
| 56 } | 58 } |
| 57 | 59 |
| 58 public void info(Class<?> src, String format, Object... args) { | 60 public void dump(Class<?> src, Severity severity, Throwable th, String message) { |
| 59 if (!isInfo) { | 61 if (severity.ordinal() >= getLevel().ordinal()) { |
| 60 return; | 62 printf(severity, src, th, message); |
| 61 } | 63 } |
| 62 printf("INFO", src, format, args); | |
| 63 } | 64 } |
| 64 | 65 |
| 65 public void warn(Class<?> src, String format, Object... args) { | 66 protected void printf(Severity level, 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); | 67 String msg = String.format(format, args); |
| 97 if (timestamp) { | 68 if (timestamp) { |
| 98 outStream.printf(isDebug ? "%tT.%1$tL " : "%tT ", System.currentTimeMillis()); | 69 outStream.printf(isDebug ? "%tT.%1$tL " : "%tT ", System.currentTimeMillis()); |
| 99 } | 70 } |
| 100 if (isDebug) { | 71 if (isDebug) { |
| 102 if (cn.startsWith("org.tmatesoft.hg.")) { | 73 if (cn.startsWith("org.tmatesoft.hg.")) { |
| 103 cn = "oth." + cn.substring("org.tmatesoft.hg.".length()); | 74 cn = "oth." + cn.substring("org.tmatesoft.hg.".length()); |
| 104 } | 75 } |
| 105 outStream.printf("(%s) ", cn); | 76 outStream.printf("(%s) ", cn); |
| 106 } | 77 } |
| 107 outStream.printf("%s: %s", level, msg); | 78 outStream.printf("%s: %s", level.toString().toUpperCase(), msg); |
| 108 if (format.length() == 0 || format.charAt(format.length() - 1) != '\n') { | 79 if (format.length() == 0 || format.charAt(format.length() - 1) != '\n') { |
| 109 outStream.println(); | 80 outStream.println(); |
| 110 } | 81 } |
| 111 } | 82 } |
| 112 protected void printf(String level, Class<?> src, Throwable th, String msg) { | 83 protected void printf(Severity level, Class<?> src, Throwable th, String msg) { |
| 113 if (msg != null || timestamp || isDebug || th == null) { | 84 if (msg != null || timestamp || isDebug || th == null) { |
| 114 printf(level, src, msg == null ? "" : msg, (Object[]) null); | 85 printf(level, src, msg == null ? "" : msg, (Object[]) null); |
| 115 } | 86 } |
| 116 if (th != null) { | 87 if (th != null) { |
| 117 if (isDebug || isInfo) { | 88 if (getLevel().ordinal() <= Info.ordinal()) { |
| 118 // full stack trace | 89 // full stack trace |
| 119 th.printStackTrace(outStream); | 90 th.printStackTrace(outStream); |
| 120 } else { | 91 } else { |
| 121 // just title of the exception | 92 // just title of the exception |
| 122 outStream.printf("%s: %s\n", th.getClass().getName(), th.getMessage()); | 93 outStream.printf("%s: %s\n", th.getClass().getName(), th.getMessage()); |
| 124 } | 95 } |
| 125 } | 96 } |
| 126 | 97 |
| 127 // alternative to hardcore System.out where SessionContext is not available now (or ever) | 98 // alternative to hardcore System.out where SessionContext is not available now (or ever) |
| 128 public static LogFacility newDefault() { | 99 public static LogFacility newDefault() { |
| 129 return new StreamLogFacility(true, true, true, System.out); | 100 return new StreamLogFacility(Severity.Debug, true, System.out); |
| 130 } | 101 } |
| 131 } | 102 } |
