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.tools;
020:
021: import java.io.BufferedReader;
022: import java.io.IOException;
023: import java.io.InputStreamReader;
024: import java.net.ServerSocket;
025: import java.net.Socket;
026:
027: /**
028: * simple logging facility that listens on a socket (e.g. for log output display)
029: */
030: public class LogConsole {
031:
032: static int DEF_PORT = 20000; // keep this in sync with the gov.nasa.jpf.util.LogHandler
033:
034: class ShutdownHook implements Runnable {
035: public void run() {
036: if (running) {
037: // not very threadsafe, but worst thing that can happen is we close twice
038: killed = true;
039: System.out
040: .println("\nLogConsole killed, shutting down");
041: }
042: try {
043: cs.close();
044: ss.close();
045: } catch (Throwable t) {
046: // not much we can do here anyway
047: }
048: }
049: }
050:
051: boolean running;
052:
053: int port;
054: boolean autoclose;
055: boolean killed;
056:
057: ServerSocket ss;
058: Socket cs;
059:
060: public void run() {
061: running = true;
062: Runtime.getRuntime().addShutdownHook(
063: new Thread(new ShutdownHook()));
064:
065: if (port == 0) {
066: port = DEF_PORT;
067: }
068:
069: try {
070: ss = new ServerSocket(port);
071:
072: try {
073: do {
074: System.out.println("LogConsole listening on port: "
075: + port);
076:
077: cs = ss.accept();
078: BufferedReader in = new BufferedReader(
079: new InputStreamReader(cs.getInputStream()));
080: String msg;
081:
082: System.out.println("LogConsole connected");
083: System.out
084: .println("--------------------------------------------------------------------------------");
085: try {
086:
087: while ((msg = in.readLine()) != null) {
088: System.out.println(msg);
089: }
090:
091: System.out
092: .println("--------------------------------------------------------------------------------");
093: System.out.println("LogConsole disconnected");
094: } catch (IOException iox) {
095: System.err.println(iox);
096: }
097:
098: in.close();
099: cs.close();
100: } while (!autoclose);
101:
102: System.out.println("LogConsole closing");
103:
104: } catch (IOException iox) {
105: if (!killed) {
106: System.err
107: .println("Error: LogConsole accept failed on port: "
108: + port);
109: }
110: }
111:
112: } catch (IOException iox) {
113: System.err
114: .println("Error: LogConsole cannot listen on port: "
115: + port);
116: }
117:
118: running = false;
119: }
120:
121: public void showUsage() {
122: System.out.println("LogConsole: socket based console logger");
123: System.out
124: .println(" usage: java gov.nasa.jpf.tools.LogConsole {flags} [<port>]");
125: System.out
126: .println(" args: -help show this message");
127: System.out
128: .println(" -autoclose close the application upon disconnect");
129: System.out
130: .println(" <port> optional port number, default: "
131: + DEF_PORT);
132: }
133:
134: boolean processArgs(String[] args) {
135: for (int i = 0; i < args.length; i++) {
136: if (args[i].charAt(0) == '-') {
137: if (args[i].equals("-autoclose")) {
138: args[i] = null;
139: autoclose = true;
140: } else if (args[i].equals("-help")) {
141: showUsage();
142: return false;
143: } else {
144: System.err
145: .println("Warning: unknown argument (see -help for usage): "
146: + args[i]);
147: }
148: } else {
149: if (args[i].matches("[0-9]+")) {
150: if (port != 0) {
151: System.err
152: .println("Error: only one port parameter allowed (see -help for usage): "
153: + args[i]);
154: return false;
155: }
156:
157: try {
158: port = Integer.parseInt(args[i]);
159: } catch (NumberFormatException nfx) {
160: System.err.println("Error: illegal port spec: "
161: + args[i]);
162: return false;
163: }
164: } else {
165: System.out.println("Error: unknown argument: "
166: + args[i]);
167: return false;
168: }
169: }
170: }
171:
172: return true;
173: }
174:
175: public static void main(String[] args) {
176: LogConsole console = new LogConsole();
177:
178: if (console.processArgs(args)) {
179: console.run();
180: }
181: }
182: }
|