Source code: iiuf/log/Client.java
1 package iiuf.log;
2
3 import java.io.ObjectOutputStream;
4 import java.io.BufferedReader;
5 import java.io.InputStreamReader;
6 import java.io.PrintStream;
7 import java.io.OutputStream;
8 import java.io.IOException;
9 import java.net.InetAddress;
10 import java.net.Socket;
11 import java.util.HashMap;
12 import java.util.LinkedList;
13
14 import iiuf.util.Timer;
15
16 /**
17 Log client implementation.
18
19 Set the "log.host" property to set the host where the server is running (default is "localhost").
20 Set the "log.port" property to set the tcp port used for logging (default is 7659).
21 Set the "log.qsize" property to limit message buffering on the client side (default is 100, use -1 for unlimited buffering).
22
23
24 (c) 2000, 2001, IIUF, DIUF<p>
25
26 @author $Author: ohitz $
27 @version $Name: $ $Revision: 1.1 $
28 */
29 public class Client
30 extends
31 Thread
32 {
33 public static int PORT = 0x1DEB;
34
35 ObjectOutputStream out;
36 InetAddress server;
37 int port;
38 static Client client;
39 LinkedList queue = new LinkedList();
40 int MAX_QSIZE;
41
42 static class State {
43 StringBuffer b = new StringBuffer();
44 Thread thread;
45
46 State(Thread t) {
47 thread = t;
48 }
49
50 void append(int c) {
51 b.append((char)c);
52 if(c == '\n') {
53 String line = b.toString();
54 if(Client.client == null) {
55 Log.out.print(line);
56 }
57 else
58 Client.client.log(new LogMessage(thread, Const.LOG_INFO, Const.STDOUT, line));
59 b = new StringBuffer();
60 }
61 }
62 }
63
64 static class StdOutStream
65 extends
66 OutputStream
67 {
68 HashMap threads = new HashMap();
69
70 public synchronized void write(int c) {
71 Thread curr = Thread.currentThread();
72 State s = (State)threads.get(curr);
73 if(s == null) {
74 s = new State(curr);
75 threads.put(curr, s);
76 }
77 s.append(c);
78 }
79 }
80
81 static class ErrorStream
82 extends
83 OutputStream
84 {
85 HashMap threads = new HashMap();
86
87 class State
88 implements
89 Runnable
90 {
91 StringBuffer b = new StringBuffer();
92 Thread thread;
93 String exception = "";
94 boolean exceptionDump = false;
95 Timer timer = new Timer(this, false);
96
97 State(Thread t) {
98 thread = t;
99 }
100
101 void append(int c) {
102 b.append((char)c);
103 if(c == '\n') {
104 String line = b.toString();
105 if(exceptionDump) {
106 if(line.startsWith("\tat ")) {
107 exception += line;
108 timer.cancel();
109 timer.schedule(500);
110 }
111 else {
112 exception();
113 stderr(line);
114 }
115 }
116 else {
117 int idx = line.indexOf(':');
118 if(idx != -1) {
119 try {
120 if(Throwable.class.isAssignableFrom(Class.forName(line.substring(0, idx)))) {
121 exceptionDump = true;
122 exception += line;
123 }
124 else stderr(line);
125 } catch(Exception e) {
126 stderr(line);
127 }
128 }
129 else {
130 try {
131 if(Throwable.class.isAssignableFrom(Class.forName(line.trim()))) {
132 exceptionDump = true;
133 exception += line;
134 }
135 else stderr(line);
136 } catch(Exception e) {
137 stderr(line);
138 }
139 }
140 }
141 b = new StringBuffer();
142 }
143 }
144
145 public void run() {
146 if(exception.length() > 50)
147 exception();
148 }
149
150 void exception() {
151 if(Client.client == null)
152 Log.out.print(exception);
153 else
154 Client.client.log(new LogMessage(thread, Const.LOG_ERR, "Runtime Exception", exception));
155 exception = "";
156 exceptionDump = false;
157 }
158
159 void stderr(String line) {
160 if(Client.client == null)
161 Log.out.print(line);
162 else
163 Client.client.log(new LogMessage(thread, Const.LOG_WARNING, Const.STDERR, line));
164 }
165 }
166
167 public synchronized void write(int c) {
168 Thread curr = Thread.currentThread();
169 State s = (State)threads.get(curr);
170 if(s == null) {
171 s = new State(curr);
172 threads.put(curr, s);
173 }
174 s.append(c);
175 }
176 }
177
178 static {
179 Log.err = System.err;
180 Log.out = System.out;
181 System.setErr(new PrintStream(new ErrorStream()));
182 System.setOut(new PrintStream(new StdOutStream()));
183 }
184
185 public Client() {
186 this(getHost(), getPort());
187 }
188
189 private static InetAddress getHost() {
190 try {
191 return InetAddress.getByName(System.getProperty("log.host"));
192 } catch(Exception e) {
193 try {
194 return InetAddress.getLocalHost();
195 } catch(Exception ex) {
196 return null;
197 }
198 }
199 }
200
201 private static int getPort() {
202 try {
203 return Integer.parseInt(System.getProperty("log.port"));
204 } catch(Exception e) {
205 return PORT;
206 }
207 }
208
209 public Client(InetAddress server_, int port_) {
210 server = server_;
211 port = port_;
212 client = this;
213 MAX_QSIZE = 100;
214 try{MAX_QSIZE = Integer.parseInt(System.getProperty("log.qsize"));}
215 catch(Exception e) {}
216 start();
217 }
218
219 public void log(int priority, String message, Throwable exception) {
220 log(new LogMessage(Thread.currentThread(), priority, message, exception));
221 }
222
223 synchronized void log(LogMessage m) {
224 m.time = System.currentTimeMillis();
225 if(MAX_QSIZE > 0 && queue.size() >= MAX_QSIZE)
226 queue.removeFirst();
227 queue.add(m);
228 notify();
229 }
230
231 public synchronized void run() {
232 for(;;) {
233 try{wait(10000);}
234 catch(InterruptedException e) {e.printStackTrace(Log.err);}
235 if(out == null) connect();
236 if(out != null) {
237 try {
238 while(!queue.isEmpty()) {
239 out.writeObject(queue.getFirst());
240 queue.removeFirst();
241 }
242 out.flush();
243 } catch(Exception e) {
244 out = null;
245 }
246 }
247 }
248 }
249
250 private synchronized void connect() {
251 try {
252 out = new ObjectOutputStream(new Socket(server, port).getOutputStream());
253 } catch(Exception e) {
254 out = null;
255 }
256 }
257
258 public static void main(String[] argv) {
259 try {
260 if(argv.length > 2) {
261 Log.err.println("usage: " + Client.class.getName() + " [server [port]]");
262 System.exit(1);
263 }
264
265 Client c = argv.length == 0 ?
266 new Client() :
267 new Client(InetAddress.getByName(argv[0]), argv.length == 2 ? Integer.parseInt(argv[1]) : PORT);
268
269 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
270 for(;;) {
271 String line = in.readLine();
272 if(line == null || line.equals("")) {
273 break;
274 }
275 try{
276 int prio = Integer.parseInt(line.substring(0, 1));
277 if(prio < 0 || prio > 7)
278 throw new IllegalArgumentException("illegal priority value:" + prio);
279 c.log(prio, line.substring(1), null);
280 } catch(Exception e) {
281 c.log(Const.LOG_ERR, line, e);
282 }
283 }
284 } catch(Exception e) {
285 e.printStackTrace();
286 }
287 }
288 }
289
290 /*
291 $Log: Client.java,v $
292 Revision 1.1 2002/07/11 12:24:01 ohitz
293 Initial checkin
294
295 Revision 1.10 2001/04/11 12:04:44 schubige
296 adapted tinja stuff for semantic checks
297
298 Revision 1.9 2001/01/17 09:55:45 schubige
299 Logger update
300
301 Revision 1.8 2001/01/04 16:28:36 schubige
302 Header update for 2001 and DIUF
303
304 Revision 1.7 2000/11/09 07:48:43 schubige
305 early checkin for DCJava
306
307 Revision 1.6 2000/10/19 08:03:45 schubige
308 Intermediate graph component related checkin
309
310 Revision 1.5 2000/10/17 15:35:59 schubige
311 Added watcher preferences
312
313 Revision 1.4 2000/10/10 16:32:11 schubige
314 Added subtree display to TreeView, fixed some bugs
315
316 Revision 1.3 2000/10/09 07:29:15 schubige
317 Features, features, features
318
319 Revision 1.2 2000/10/09 06:47:55 schubige
320 Updated logger stuff
321
322 Revision 1.1 2000/10/05 14:59:30 schubige
323 Added loging stuff
324
325 */