001: //
002: //Copyright (C) 2005 United States Government as represented by the
003: //Administrator of the National Aeronautics and Space Administration
004: //(NASA). All Rights Reserved.
005: //
006: //This software is distributed under the NASA Open Source Agreement
007: //(NOSA), version 1.3. The NOSA has been approved by the Open Source
008: //Initiative. See the file NOSA-1.3-JPF at the top of the distribution
009: //directory tree for the complete NOSA document.
010: //
011: //THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
012: //KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
013: //LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
014: //SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
015: //A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
016: //THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
017: //DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
018: //
019: package gov.nasa.jpf.util;
020:
021: import gov.nasa.jpf.Config;
022:
023: import java.io.File;
024: import java.io.IOException;
025: import java.io.PrintWriter;
026: import java.io.OutputStream;
027: import java.io.FileOutputStream;
028: import java.net.ConnectException;
029: import java.net.Socket;
030: import java.net.UnknownHostException;
031: import java.util.logging.Handler;
032: import java.util.logging.LogRecord;
033: import java.util.logging.Logger;
034:
035: /**
036: * log handler class that deals with output selection and formatting. This is the
037: * only handler we use for our own logging
038: */
039: public class LogHandler extends Handler {
040:
041: public static final String LOG_HOST = "localhost";
042: public static final int LOG_PORT = 20000;
043:
044: File file;
045: Socket socket;
046: OutputStream ostream;
047:
048: PrintWriter out;
049:
050: public LogHandler(Config conf) {
051: String output = conf.getString("log.output", "out");
052:
053: if (output.matches("[a-zA-Z0-9.]*:[0-9]*")) { // we assume that's a hostname:port spec
054: int idx = output.indexOf(':');
055: String host = output.substring(0, idx);
056: String port = output.substring(idx + 1, output.length());
057: ostream = connectSocket(host, port);
058: } else if (output.equalsIgnoreCase("out")
059: || output.equals("System.out")) {
060: ostream = System.out;
061: } else if (output.equalsIgnoreCase("err")
062: || output.equals("System.err")) {
063: ostream = System.err;
064: } else {
065: ostream = openFile(output);
066: }
067:
068: if (ostream == null) {
069: ostream = System.out;
070: }
071:
072: out = new PrintWriter(ostream, true);
073: }
074:
075: OutputStream connectSocket(String host, String portSpec) {
076: int port = -1;
077:
078: if ((host == null) || (host.length() == 0)) {
079: host = LOG_HOST;
080: }
081:
082: if (portSpec != null) {
083: try {
084: port = Integer.parseInt(portSpec);
085: } catch (NumberFormatException x) {
086: // just catch it
087: }
088: }
089: if (port == -1) {
090: port = LOG_PORT;
091: }
092:
093: try {
094: socket = new Socket(host, port);
095: return socket.getOutputStream();
096: } catch (UnknownHostException uhx) {
097: //System.err.println("unknown log host: " + host);
098: } catch (ConnectException cex) {
099: //System.err.println("no log host detected);
100: } catch (IOException iox) {
101: //System.err.println(iox);
102: }
103:
104: return null;
105: }
106:
107: OutputStream openFile(String fileName) {
108: file = new File(fileName);
109:
110: try {
111: if (file.exists()) {
112: file.delete();
113: }
114: file.createNewFile();
115: return new FileOutputStream(file);
116: } catch (IOException iox) {
117: // just catch it
118: }
119:
120: return null;
121: }
122:
123: public void close() throws SecurityException {
124: if ((ostream != System.err) && (ostream != System.out)) {
125: out.close();
126: }
127:
128: if (socket != null) {
129: try {
130: socket.close();
131: } catch (IOException iox) {
132: // not much we can do
133: }
134: }
135: }
136:
137: public void flush() {
138: out.flush();
139: }
140:
141: public void publish(LogRecord r) {
142: out.println(r.getMessage());
143: }
144:
145: public void printStatus(Logger log) {
146: if (socket != null) {
147: log.config("logging to socket: " + socket);
148: } else if (file != null) {
149: log.config("logging to file: " + file.getAbsolutePath());
150: } else if (ostream == System.err) {
151: log.config("logging to System.err");
152: } else if (ostream == System.out) {
153: log.config("logging to System.out");
154: } else {
155: log.warning("unknown log destination");
156: }
157: }
158: }
|