1 package com.protomatter.syslog; 2 3 52 53 import java.io.*; 54 import java.net.*; 55 import java.util.*; 56 import java.text.*; 57 58 import com.protomatter.util.*; 59 60 66 public class OpenFileLog 67 extends BasicLogger 68 { 69 private boolean fileChanged = true; 70 71 private File file; 72 73 76 public OpenFileLog(File f) 77 { 78 this(); 79 this.file = f; 80 } 81 82 85 public OpenFileLog() 86 { 87 super(); 88 } 89 90 93 public void setFile(File f) 94 { 95 cleanup(); 96 file = f; 97 fileChanged = true; 98 } 99 100 103 public File getFile() 104 { 105 return this.file; 106 } 107 108 111 public final void log(SyslogMessage message) 112 { 113 StringBuffer b = new StringBuffer (256); 114 formatLogEntry(b, message); 115 116 try 117 { 118 PrintWriter out = new PrintWriter( 119 new FileWriter(file.getCanonicalPath(), true)); 120 if (fileChanged) 121 { 122 out.print(formatter.getLogHeader()); 123 fileChanged = false; 124 } 125 out.print(b); 126 out.flush(); 127 out.close(); 128 } 129 catch (IOException x) 130 { 131 System.err.println(MessageFormat.format( 132 Syslog.getResourceString(MessageConstants.OPENFLOG_CANNOT_OPEN_MESSAGE), 133 new Object [] { x.toString() })); 134 } 135 } 136 137 private void cleanup() 138 { 139 try 140 { 141 if (file != null) 142 { 143 PrintWriter out = new PrintWriter( 144 new FileWriter(file.getCanonicalPath(), true)); 145 out.print(formatter.getLogFooter()); 146 out.flush(); 147 out.close(); 148 } 149 } 150 catch (IOException x) 151 { 152 System.err.println(MessageFormat.format( 153 Syslog.getResourceString(MessageConstants.OPENFLOG_CANNOT_OPEN_MESSAGE), 154 new Object [] { x.toString() })); 155 } 156 } 157 158 161 public void shutdown() 162 { 163 cleanup(); 164 } 165 166 public void flush() 167 { 168 } 170 } 171 | Popular Tags |