1 23 24 package org.enhydra.xml.xmlc; 25 26 import java.io.PrintWriter ; 27 import java.io.StringWriter ; 28 29 32 public class StreamXMLCLogger implements XMLCLogger { 33 36 private PrintWriter fInfoWriter; 37 38 41 private PrintWriter fDebugWriter; 42 43 46 private PrintWriter fErrorWriter; 47 48 51 public StreamXMLCLogger() { 52 } 53 54 58 public StreamXMLCLogger(PrintWriter logWriter) { 59 fInfoWriter = logWriter; 60 fErrorWriter = logWriter; 61 } 62 63 67 public StreamXMLCLogger(PrintWriter infoWriter, 68 PrintWriter errorWriter, 69 PrintWriter debugWriter) { 70 fInfoWriter = infoWriter; 71 fErrorWriter = errorWriter; 72 fDebugWriter = debugWriter; 73 } 74 75 78 private void logExcept(PrintWriter writer, 79 String msg, 80 Throwable except) { 81 if (writer != null) { 82 StringWriter trace = new StringWriter (1024); 84 PrintWriter ptrace = new PrintWriter (trace, true); 85 except.printStackTrace(ptrace); 86 writer.println(msg + "\n" + except.toString() 87 + "\n" + trace); 88 writer.flush(); 89 } 90 } 91 92 95 public boolean infoEnabled() { 96 return (fInfoWriter != null); 97 } 98 99 102 public PrintWriter getInfoWriter() { 103 return fInfoWriter; 104 } 105 106 109 public void setInfoWriter(PrintWriter writer) { 110 fInfoWriter = writer; 111 } 112 113 116 public void logInfo(String msg) { 117 if (fInfoWriter != null) { 118 fInfoWriter.println(msg); 119 fInfoWriter.flush(); 120 } 121 } 122 123 126 public void logInfo(String msg, 127 Throwable except) { 128 if (fInfoWriter != null) { 129 logExcept(fInfoWriter, msg, except); 130 } 131 } 132 133 136 public boolean errorEnabled() { 137 return (fErrorWriter != null); 138 } 139 140 143 public PrintWriter getErrorWriter() { 144 return fErrorWriter; 145 } 146 147 150 public void setErrorWriter(PrintWriter writer) { 151 fErrorWriter = writer; 152 } 153 154 157 public void logError(String msg) { 158 if (fErrorWriter != null) { 159 fErrorWriter.println(msg); 160 fErrorWriter.flush(); 161 } 162 } 163 164 167 public void logError(String msg, 168 Throwable except) { 169 if (fErrorWriter != null) { 170 logExcept(fErrorWriter, msg, except); 171 } 172 } 173 174 177 public boolean debugEnabled() { 178 return (fDebugWriter != null); 179 } 180 181 184 public PrintWriter getDebugWriter() { 185 return fDebugWriter; 186 } 187 188 191 public void setDebugWriter(PrintWriter writer) { 192 fDebugWriter = writer; 193 } 194 195 198 public void logDebug(String msg) { 199 if (fDebugWriter != null) { 200 fDebugWriter.println(msg); 201 fDebugWriter.flush(); 202 } 203 } 204 205 208 public void logDebug(String msg, 209 Throwable except) { 210 if (fDebugWriter != null) { 211 logExcept(fDebugWriter, msg, except); 212 } 213 } 214 215 218 public void close() { 219 if (fInfoWriter != null) { 220 fInfoWriter.close(); 221 } 222 if (fErrorWriter != null) { 223 fErrorWriter.close(); 224 } 225 if (fDebugWriter != null) { 226 fDebugWriter.close(); 227 } 228 } 229 } 230 | Popular Tags |