KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > util > LogHandler


1 //
2
//Copyright (C) 2005 United States Government as represented by the
3
//Administrator of the National Aeronautics and Space Administration
4
//(NASA). All Rights Reserved.
5
//
6
//This software is distributed under the NASA Open Source Agreement
7
//(NOSA), version 1.3. The NOSA has been approved by the Open Source
8
//Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
//directory tree for the complete NOSA document.
10
//
11
//THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
//KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
//LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
//SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
//A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
//THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
//DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.util;
20
21 import gov.nasa.jpf.Config;
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.net.ConnectException JavaDoc;
29 import java.net.Socket JavaDoc;
30 import java.net.UnknownHostException JavaDoc;
31 import java.util.logging.Handler JavaDoc;
32 import java.util.logging.LogRecord JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34
35 /**
36  * log handler class that deals with output selection and formatting. This is the
37  * only handler we use for our own logging
38  */

39 public class LogHandler extends Handler JavaDoc {
40
41   public static final String JavaDoc LOG_HOST = "localhost";
42   public static final int LOG_PORT = 20000;
43   
44   File JavaDoc file;
45   Socket JavaDoc socket;
46   OutputStream JavaDoc ostream;
47   
48   PrintWriter JavaDoc out;
49   
50   public LogHandler (Config conf) {
51     String JavaDoc output = conf.getString("log.output", "out");
52     
53     if (output.matches("[a-zA-Z0-9.]*:[0-9]*")) { // we assume that's a hostname:port spec
54
int idx = output.indexOf(':');
55       String JavaDoc host = output.substring(0, idx);
56       String JavaDoc port = output.substring(idx+1, output.length());
57       ostream = connectSocket( host, port);
58     } else if (output.equalsIgnoreCase("out") || output.equals("System.out")) {
59       ostream = System.out;
60     } else if (output.equalsIgnoreCase("err") || output.equals("System.err")) {
61       ostream = System.err;
62     } else {
63       ostream = openFile(output);
64     }
65     
66     if (ostream == null) {
67       ostream = System.out;
68     }
69     
70     out = new PrintWriter JavaDoc(ostream, true);
71   }
72   
73   OutputStream JavaDoc connectSocket (String JavaDoc host, String JavaDoc portSpec) {
74     int port = -1;
75     
76     if ((host == null) || (host.length() == 0)) {
77       host = LOG_HOST;
78     }
79     
80     if (portSpec != null) {
81       try {
82         port = Integer.parseInt(portSpec);
83       } catch (NumberFormatException JavaDoc x) {
84         // just catch it
85
}
86     }
87     if (port == -1) {
88       port = LOG_PORT;
89     }
90     
91     
92     try {
93       socket = new Socket JavaDoc(host, port);
94       return socket.getOutputStream();
95     } catch (UnknownHostException JavaDoc uhx) {
96       //System.err.println("unknown log host: " + host);
97
} catch (ConnectException JavaDoc cex) {
98       //System.err.println("no log host detected);
99
} catch (IOException JavaDoc iox) {
100       //System.err.println(iox);
101
}
102
103     return null;
104   }
105   
106   OutputStream JavaDoc openFile (String JavaDoc fileName) {
107     file = new File JavaDoc(fileName);
108     
109     try {
110       if (file.exists()) {
111         file.delete();
112       }
113       file.createNewFile();
114       return new FileOutputStream JavaDoc(file);
115     } catch (IOException JavaDoc iox) {
116       // just catch it
117
}
118     
119     return null;
120   }
121   
122   public void close () throws SecurityException JavaDoc {
123     if ((ostream != System.err) && (ostream != System.out)) {
124       out.close();
125     }
126     
127     if (socket != null) {
128       try {
129         socket.close();
130       } catch (IOException JavaDoc iox) {
131         // not much we can do
132
}
133     }
134   }
135
136   public void flush () {
137     out.flush();
138   }
139
140   public void publish (LogRecord JavaDoc r) {
141     out.println(r.getMessage());
142   }
143
144   public void printStatus (Logger JavaDoc log) {
145     if (socket != null) {
146       log.config("logging to socket: " + socket);
147     } else if (file != null) {
148       log.config("logging to file: " + file.getAbsolutePath());
149     } else if (ostream == System.err) {
150       log.config("logging to System.err");
151     } else if (ostream == System.out) {
152       log.config("logging to System.out");
153     } else {
154       log.warning("unknown log destination");
155     }
156   }
157 }
158
Popular Tags