1 20 21 package freecs.util.logger; 22 23 import java.io.IOException ; 24 import java.nio.ByteBuffer ; 25 import java.nio.charset.Charset ; 26 import java.util.Calendar ; 27 import java.util.HashMap ; 28 29 import freecs.Server; 30 import freecs.util.ObjectBuffer; 31 32 37 public class LogWriter extends Thread { 38 public static LogWriter instance = new LogWriter(); 39 private HashMap logDestinations = new HashMap (); 40 public ObjectBuffer logQueue = new ObjectBuffer(1000); 41 private Charset cs = Charset.forName(System.getProperty("file.encoding", "iso-8859-1")); 42 43 private boolean stopped = false; 44 Calendar cal = Calendar.getInstance (); 45 46 private LogWriter () { 47 this.setPriority(Thread.MAX_PRIORITY); 48 this.start(); 49 } 50 51 56 LogDestination getLogDestination (String path) { 57 LogDestination ld = (LogDestination) logDestinations.get(path); 58 if (ld == null) { 59 ld = new LogFile (path); 61 logDestinations.put(path, ld); 62 } 63 return ld; 64 } 65 66 71 public void addLogMessage (String path, String message) { 72 LogDestination ld = getLogDestination(path); 73 StringBuffer sb = new StringBuffer ( 74 Server.formatDefaultTimeStamp(System.currentTimeMillis())); 75 sb.append (": "); 76 sb.append (message); 77 sb.append (System.getProperty("line.separator", "\r\n")); 78 LogEntry le = new LogEntry (ld, cs.encode (sb.toString())); 79 addLogElement(le); 80 } 81 82 87 public void addLogMessage (int type, String message) { 88 LogDestination ld = getLogDestination(Server.LOGFILE[type]); 89 LogEntry le = new LogEntry (ld, cs.encode(message)); 90 addLogElement(le); 91 } 92 93 97 private void addLogElement (LogEntry le) { 98 int cntr = 0; 99 boolean success = false; 100 while (!success) { 101 synchronized (this) { 102 success=logQueue.put(le); 103 this.notifyAll(); 104 } 105 cntr++; 106 if (cntr>5) { 107 System.out.print("LQ-full: "); 108 System.out.print(le.toString()); 109 } 110 } 111 } 112 113 public void stopLogging () { 114 stopped = true; 115 } 116 117 public void run() { 118 LogEntry le=null; 119 long lastMessage=0; 120 while (!stopped) try { 121 ObjectBuffer workingCopy; 122 synchronized (this) { 123 while (logQueue.isEmpty()) try { 124 this.wait (33); 125 } catch (InterruptedException ie) { } 126 workingCopy = logQueue; 127 logQueue = new ObjectBuffer(1000); 128 this.notifyAll(); 129 } 130 while (!workingCopy.isEmpty()) { 131 le = (LogEntry) workingCopy.get(); 132 if (writeToChannel(le)) 133 workingCopy.pop(); 134 } 135 } catch (Exception e) { 136 StringBuffer sb = new StringBuffer (this.toString()); 137 sb.append("run:"); 138 sb.append(e.getMessage()); 139 sb.append("("); 140 sb.append(e.getCause()); 141 sb.append(") StackTraceElements: "); 142 StackTraceElement [] st = e.getStackTrace(); 143 sb.append (st.length); 144 sb.append ("\r\n"); 145 for (int i = 0; i < st.length; i++) { 146 sb.append ("at "); 147 sb.append (st[i].getClassName()); 148 sb.append (" "); 149 sb.append (st[i].getMethodName()); 150 sb.append ("("); 151 sb.append (st[i].getLineNumber()); 152 sb.append (")\r\n"); 153 } 154 System.out.println (sb.toString()); 155 if (le!=null) 156 System.out.println (le.toString()); 157 e.printStackTrace(); 158 } finally { 159 try { 160 Thread.sleep (33); 161 } catch (InterruptedException ie) { } 162 } 163 } 164 165 private boolean writeToChannel (LogEntry le) { 166 try { 167 int written = le.ld.getChannel().write (le.buf); 168 if (!le.buf.hasRemaining()) 169 return true; 170 } catch (IOException ioe) { 171 Server.debug (this, "writeToFile: ", ioe, Server.MSG_ERROR, Server.LVL_MAJOR); 172 return true; 173 } catch (Exception e) { 174 Server.debug (this, "writeToFile: ", e, Server.MSG_ERROR, Server.LVL_MAJOR); 175 return true; 176 } 177 return false; 178 } 179 180 private class LogEntry { 181 final LogDestination ld; 182 final ByteBuffer buf; 183 184 LogEntry (LogDestination ld, ByteBuffer buf) { 185 this.ld = ld; 186 this.buf = buf; 187 } 188 } 189 } | Popular Tags |