1 package gov.nasa.jpf.util; 20 21 import gov.nasa.jpf.Config; 22 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.io.OutputStream ; 27 import java.io.FileOutputStream ; 28 import java.net.ConnectException ; 29 import java.net.Socket ; 30 import java.net.UnknownHostException ; 31 import java.util.logging.Handler ; 32 import java.util.logging.LogRecord ; 33 import java.util.logging.Logger ; 34 35 39 public class LogHandler extends Handler { 40 41 public static final String LOG_HOST = "localhost"; 42 public static final int LOG_PORT = 20000; 43 44 File file; 45 Socket socket; 46 OutputStream ostream; 47 48 PrintWriter out; 49 50 public LogHandler (Config conf) { 51 String output = conf.getString("log.output", "out"); 52 53 if (output.matches("[a-zA-Z0-9.]*:[0-9]*")) { int idx = output.indexOf(':'); 55 String host = output.substring(0, idx); 56 String 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 (ostream, true); 71 } 72 73 OutputStream connectSocket (String host, String 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 x) { 84 } 86 } 87 if (port == -1) { 88 port = LOG_PORT; 89 } 90 91 92 try { 93 socket = new Socket (host, port); 94 return socket.getOutputStream(); 95 } catch (UnknownHostException uhx) { 96 } catch (ConnectException cex) { 98 } catch (IOException iox) { 100 } 102 103 return null; 104 } 105 106 OutputStream openFile (String fileName) { 107 file = new File (fileName); 108 109 try { 110 if (file.exists()) { 111 file.delete(); 112 } 113 file.createNewFile(); 114 return new FileOutputStream (file); 115 } catch (IOException iox) { 116 } 118 119 return null; 120 } 121 122 public void close () throws SecurityException { 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 iox) { 131 } 133 } 134 } 135 136 public void flush () { 137 out.flush(); 138 } 139 140 public void publish (LogRecord r) { 141 out.println(r.getMessage()); 142 } 143 144 public void printStatus (Logger 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 |