KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > tools > LogConsole


1 //
2
//Copyright (C) 2005 United States Government as represented by the
3
//Administrator of the National Aeronautics and Space Administration
4
//(NASA). All Rights Reserved.
5
//
6
//This software is distributed under the NASA Open Source Agreement
7
//(NOSA), version 1.3. The NOSA has been approved by the Open Source
8
//Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
//directory tree for the complete NOSA document.
10
//
11
//THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
//KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
//LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
//SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
//A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
//THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
//DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.tools;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.net.ServerSocket JavaDoc;
25 import java.net.Socket JavaDoc;
26
27 /**
28  * simple logging facility that listens on a socket (e.g. for log output display)
29  */

30 public class LogConsole {
31   
32   static int DEF_PORT = 20000; // keep this in sync with the gov.nasa.jpf.util.LogHandler
33

34   class ShutdownHook implements Runnable JavaDoc {
35     public void run () {
36       if (running) {
37         // not very threadsafe, but worst thing that can happen is we close twice
38
killed = true;
39         System.out.println("\nLogConsole killed, shutting down");
40       }
41       try {
42         cs.close();
43         ss.close();
44       } catch (Throwable JavaDoc t) {
45         // not much we can do here anyway
46
}
47     }
48   }
49   
50   
51   boolean running;
52   
53   int port;
54   boolean autoclose;
55   boolean killed;
56   
57   ServerSocket JavaDoc ss;
58   Socket JavaDoc cs;
59   
60   public void run () {
61     running = true;
62     Runtime.getRuntime().addShutdownHook(new Thread JavaDoc(new ShutdownHook()));
63     
64     if (port == 0) {
65       port = DEF_PORT;
66     }
67     
68     try {
69       ss = new ServerSocket JavaDoc(port);
70       
71       try {
72         do {
73           System.out.println("LogConsole listening on port: " + port);
74
75           cs = ss.accept();
76           BufferedReader JavaDoc in = new BufferedReader JavaDoc( new InputStreamReader JavaDoc(cs.getInputStream()));
77           String JavaDoc msg;
78           
79           System.out.println("LogConsole connected");
80           System.out.println("--------------------------------------------------------------------------------");
81           try {
82             
83             while ((msg = in.readLine()) != null) {
84               System.out.println(msg);
85             }
86             
87             System.out.println("--------------------------------------------------------------------------------");
88             System.out.println("LogConsole disconnected");
89           } catch (IOException JavaDoc iox) {
90             System.err.println(iox);
91           }
92
93           in.close();
94           cs.close();
95         } while (!autoclose);
96
97         System.out.println("LogConsole closing");
98         
99       } catch (IOException JavaDoc iox) {
100         if (!killed) {
101           System.err.println("Error: LogConsole accept failed on port: " + port);
102         }
103       }
104       
105     } catch (IOException JavaDoc iox) {
106       System.err.println("Error: LogConsole cannot listen on port: " + port);
107     }
108     
109     running = false;
110   }
111
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);
118   }
119   
120   boolean processArgs (String JavaDoc[] args) {
121     for (int i=0; i<args.length; i++) {
122       if (args[i].charAt(0) == '-') {
123         if (args[i].equals("-autoclose")) {
124           args[i] = null;
125           autoclose = true;
126         } else if (args[i].equals("-help")) {
127           showUsage();
128           return false;
129         } else {
130           System.err.println("Warning: unknown argument (see -help for usage): " + args[i]);
131         }
132       } else {
133         if (args[i].matches("[0-9]+")) {
134           if (port != 0) {
135             System.err.println("Error: only one port parameter allowed (see -help for usage): " + args[i]);
136             return false;
137           }
138           
139           try {
140             port = Integer.parseInt(args[i]);
141           } catch (NumberFormatException JavaDoc nfx) {
142             System.err.println("Error: illegal port spec: " + args[i]);
143             return false;
144           }
145         } else {
146           System.out.println("Error: unknown argument: " + args[i]);
147           return false;
148         }
149       }
150     }
151     
152     return true;
153   }
154
155   public static void main (String JavaDoc[] args) {
156     LogConsole console = new LogConsole();
157     
158     if (console.processArgs(args)) {
159       console.run();
160     }
161   }
162 }
163
Popular Tags