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.listener;
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.net.ServerSocket;
24 import java.net.Socket;
27 * simple logging facility that listens on a socket (e.g. for log output display)
29 public class LogConsole {
31 static int DEF_PORT = 20000; // keep this in sync with the gov.nasa.jpf.util.LogHandler
33 class ShutdownHook implements Runnable {
37 // not very threadsafe, but worst thing that can happen is we close twice
39 System.out.println("\nLogConsole killed, shutting down");
44 } catch (Throwable t) {
45 // not much we can do here anyway
62 Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHook()));
69 ss = new ServerSocket(port);
73 System.out.println("LogConsole listening on port: " + port);
76 BufferedReader in = new BufferedReader( new InputStreamReader(cs.getInputStream()));
79 System.out.println("LogConsole connected");
80 System.out.println("--------------------------------------------------------------------------------");
83 while ((msg = in.readLine()) != null) {
84 System.out.println(msg);
87 System.out.println("--------------------------------------------------------------------------------");
88 System.out.println("LogConsole disconnected");
89 } catch (IOException iox) {
90 System.err.println(iox);
97 System.out.println("LogConsole closing");
99 } catch (IOException iox) {
101 System.err.println("Error: LogConsole accept failed on port: " + port);
105 } catch (IOException iox) {
106 System.err.println("Error: LogConsole cannot listen on port: " + port);
112 public void showUsage () {
113 System.out.println("LogConsole: socket based console logger");
114 System.out.println(" usage: java gov.nasa.jpf.tools.LogConsole {flags} [<port>]");
115 System.out.println(" args: -help show this message");
116 System.out.println(" -autoclose close the application upon disconnect");
117 System.out.println(" <port> optional port number, default: " + DEF_PORT);
120 boolean processArgs (String[] args) {
121 for (int i=0; i<args.length; i++) {
122 if (args[i].charAt(0) == '-') {
123 if (args[i].equals("-autoclose")) {
126 } else if (args[i].equals("-help")) {
130 System.err.println("Warning: unknown argument (see -help for usage): " + args[i]);
133 if (args[i].matches("[0-9]+")) {
135 System.err.println("Error: only one port parameter allowed (see -help for usage): " + args[i]);
140 port = Integer.parseInt(args[i]);
141 } catch (NumberFormatException nfx) {
142 System.err.println("Error: illegal port spec: " + args[i]);
146 System.out.println("Error: unknown argument: " + args[i]);
155 public static void main (String[] args) {
156 LogConsole console = new LogConsole();
158 if (console.processArgs(args)) {