2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.util;
20 import gov.nasa.jpf.Config;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.OutputStream;
26 import java.io.PrintWriter;
27 import java.net.ConnectException;
28 import java.net.Socket;
29 import java.net.UnknownHostException;
30 import java.util.logging.Formatter;
31 import java.util.logging.Handler;
32 import java.util.logging.LogRecord;
33 import java.util.logging.Logger;
36 * log handler class that deals with output selection and formatting. This is the
37 * only handler we use for our own logging. We do our own little formatting
40 public class LogHandler extends Handler {
42 static class DefaultFormatter extends Formatter {
46 //..and potentially more
48 DefaultFormatter (Config conf) {
49 showName = conf.getBoolean("log.show_name", false);
50 showLevel = conf.getBoolean("log.show_level", true);
51 format = showName || showLevel;
54 DefaultFormatter (boolean showName, boolean showLevel){
55 this.showName = showName;
56 this.showLevel = showLevel;
57 format = showName || showLevel;
60 // we might want to parameterize this
62 public String format (LogRecord r) {
64 StringBuilder sb = new StringBuilder();
67 sb.append(r.getLevel().getName());
73 sb.append(r.getLoggerName());
77 String msg = r.getMessage();
78 Object[] params = r.getParameters();
83 sb.append(String.format(msg,params));
89 return r.getMessage();
94 public static String LOG_HOST = "localhost";
95 public static int LOG_PORT = 20000;
103 public LogHandler (Config conf) {
104 LOG_HOST = conf.getString("log.host", LOG_HOST);
105 LOG_PORT = conf.getInt("log.port", LOG_PORT);
107 String output = conf.getString("log.output", "out");
109 if (output.matches("[a-zA-Z0-9.]*:[0-9]*")) { // we assume that's a hostname:port spec
110 int idx = output.indexOf(':');
111 String host = output.substring(0, idx);
112 String port = output.substring(idx+1, output.length());
113 ostream = connectSocket( host, port);
114 } else if (output.equalsIgnoreCase("socket")){
115 ostream = connectSocket( LOG_HOST, Integer.toString(LOG_PORT));
116 } else if (output.equalsIgnoreCase("out") || output.equals("System.out")) {
117 ostream = System.out;
118 } else if (output.equalsIgnoreCase("err") || output.equals("System.err")) {
119 ostream = System.err;
121 ostream = openFile(output);
124 if (ostream == null) {
125 ostream = System.out;
128 setFormatter(new DefaultFormatter(conf));
132 protected LogHandler() {
133 // for derived classes
136 OutputStream connectSocket (String host, String portSpec) {
139 if ((host == null) || (host.length() == 0)) {
143 if (portSpec != null) {
145 port = Integer.parseInt(portSpec);
146 } catch (NumberFormatException x) {
156 socket = new Socket(host, port);
157 return socket.getOutputStream();
158 } catch (UnknownHostException uhx) {
159 //System.err.println("unknown log host: " + host);
160 } catch (ConnectException cex) {
161 //System.err.println("no log host detected);
162 } catch (IOException iox) {
163 //System.err.println(iox);
169 OutputStream openFile (String fileName) {
170 file = new File(fileName);
176 file.createNewFile();
177 return new FileOutputStream(file);
178 } catch (IOException iox) {
185 public void setOutput (OutputStream ostream) {
186 out = new PrintWriter(ostream, true);
190 public void close () throws SecurityException {
191 if ((ostream != System.err) && (ostream != System.out)) {
195 if (socket != null) {
198 } catch (IOException iox) {
199 // not much we can do
205 public void flush () {
210 public void publish (LogRecord r) {
211 String msg = getFormatter().format(r);
215 public void printStatus (Logger log) {
216 if (socket != null) {
217 log.config("logging to socket: " + socket);
218 } else if (file != null) {
219 log.config("logging to file: " + file.getAbsolutePath());
220 } else if (ostream == System.err) {
221 log.config("logging to System.err");
222 } else if (ostream == System.out) {
223 log.config("logging to System.out");
225 log.warning("unknown log destination");
230 // a dfault handler that doesn't need Config
231 public static class DefaultConsoleHandler extends LogHandler {
232 public DefaultConsoleHandler() {
233 ostream = System.out;
235 setFormatter(new DefaultFormatter(false,true));