1 package com.protomatter.syslog; 2 3 52 53 import java.io.PrintWriter ; 54 import java.io.IOException ; 55 import java.net.*; 56 import java.util.*; 57 import java.text.*; 58 59 import com.protomatter.util.*; 60 61 67 public class PrintWriterLog 68 extends BasicLogger 69 { 70 private PrintWriter out = null; 71 private boolean writerChanged = true; 72 private String streamName = null; 73 private boolean canDumpConfig = false; 74 75 80 public PrintWriterLog(PrintWriter writer) 81 { 82 this(); 83 out = writer; 84 } 85 86 91 public PrintWriterLog(String streamName) 92 { 93 this(); 94 if ("System.err".equals(streamName)) 95 { 96 out = new PrintWriter (System.err); 97 } 98 else if ("System.out".equals(streamName)) 99 { 100 out = new PrintWriter (System.out); 101 } 102 else 103 { 104 throw new IllegalArgumentException (MessageFormat.format( 105 Syslog.getResourceString(MessageConstants.PRINTWRITER_BAD_STREAM_NAME_MESSAGE), 106 new Object [] { "stream", "System.out", "System.err" })); 107 } 108 this.streamName = streamName; 109 this.canDumpConfig = true; 110 } 111 112 115 public String getStreamName() 116 { 117 return this.streamName; 118 } 119 120 123 public void setStreamName(String streamName) 124 { 125 this.streamName = streamName; 126 } 127 128 132 public PrintWriterLog() 133 { 134 super(); 135 } 136 137 140 public void setWriter(PrintWriter writer) 141 { 142 cleanup(); 143 out = writer; 144 writerChanged = true; 145 this.canDumpConfig = false; 146 } 147 148 151 public void log(SyslogMessage message) 152 { 153 StringBuffer b = new StringBuffer (); 154 formatLogEntry(b, message); 155 if (out != null) 156 { 157 if (writerChanged) 158 { 159 out.print(formatter.getLogHeader()); 160 writerChanged = false; 161 } 162 out.print(b); 163 out.flush(); 164 } 165 } 166 167 private void cleanup() 168 { 169 if (out != null) 170 { 171 out.print(formatter.getLogFooter()); 172 out.flush(); 173 out.close(); 174 out = null; 175 } 176 } 177 178 181 public synchronized void shutdown() 182 { 183 cleanup(); 184 } 185 186 public void flush() 187 { 188 if (out != null) 189 out.flush(); 190 } 191 } 192 | Popular Tags |