KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > util > logger > LogWriter


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Created on 05.10.2003
19  */

20
21 package freecs.util.logger;
22
23 import java.io.IOException JavaDoc;
24 import java.nio.ByteBuffer JavaDoc;
25 import java.nio.charset.Charset JavaDoc;
26 import java.util.Calendar JavaDoc;
27 import java.util.HashMap JavaDoc;
28
29 import freecs.Server;
30 import freecs.util.ObjectBuffer;
31
32 /**
33  * @author Manfred Andres
34  *
35  * freecs.util
36  */

37 public class LogWriter extends Thread JavaDoc {
38     public static LogWriter instance = new LogWriter();
39     private HashMap JavaDoc logDestinations = new HashMap JavaDoc();
40     public ObjectBuffer logQueue = new ObjectBuffer(1000);
41     private Charset JavaDoc cs = Charset.forName(System.getProperty("file.encoding", "iso-8859-1"));
42
43     private boolean stopped = false;
44     Calendar JavaDoc cal = Calendar.getInstance ();
45
46     private LogWriter () {
47         this.setPriority(Thread.MAX_PRIORITY);
48         this.start();
49     }
50
51     /**
52      * retrieve a logDestination to write to
53      * @param path the path to the logfile
54      * @return the LogDestination object
55      */

56     LogDestination getLogDestination (String JavaDoc path) {
57         LogDestination ld = (LogDestination) logDestinations.get(path);
58         if (ld == null) {
59             // TODO: check for file, socket,..) and construct the correct LogDestination
60
ld = new LogFile (path);
61             logDestinations.put(path, ld);
62         }
63         return ld;
64     }
65
66     /**
67      * this method is used to write messages to a logdestination
68      * @param path the path for this logdestination
69      * @param message the message to be logged
70      */

71     public void addLogMessage (String JavaDoc path, String JavaDoc message) {
72         LogDestination ld = getLogDestination(path);
73         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(
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     /**
83      *
84      * @param type
85      * @param message
86      */

87     public void addLogMessage (int type, String JavaDoc message) {
88         LogDestination ld = getLogDestination(Server.LOGFILE[type]);
89         LogEntry le = new LogEntry (ld, cs.encode(message));
90         addLogElement(le);
91     }
92
93     /**
94      * Add the constructed LogEntry to the LogWriter.instance's log Queue
95      * @param le the LogEntry
96      */

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 JavaDoc 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 JavaDoc e) {
136             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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 JavaDoc[] 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 JavaDoc 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 JavaDoc ioe) {
171             Server.debug (this, "writeToFile: ", ioe, Server.MSG_ERROR, Server.LVL_MAJOR);
172             return true;
173         } catch (Exception JavaDoc 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 JavaDoc buf;
183         
184         LogEntry (LogDestination ld, ByteBuffer JavaDoc buf) {
185             this.ld = ld;
186             this.buf = buf;
187         }
188     }
189 }
Popular Tags