getReporter()1 /** *************************************************************************
2  * Copyright (c) 2000:
3  * University of Alberta,
4  * Deptartment of Computing Science
5  * Computer Poker Research Group
6  *
7  * See "Liscence.txt"
8  ************************************************************************** */
9 package org.alberta.poker.util;
10 
11 import java.io.*;
12 import java.util.*;
13 
14 /**
15  * Handles reporting messages to stdout or stderr, and logging messages to
16  * files. Defaults to printing output to stderr.
17  *
18  * @author Aaron Davidson
19  * @version 1.3.5
20  */
21 public class Reporter {
22 
23     private boolean console = true;
24     private Vector listeners = new Vector();
25     private boolean active = true;
26     private boolean logOn;
27     private boolean debug = false;
28     private String logfile;
29     private volatile RandomAccessFile lf;
30     private static BufferedReader reader;
31     private static Reporter gRep = null;
32 
33     public static synchronized Reporter getReporter() {
34         if (gRep == null) {
35             gRep = new Reporter();
36         }
37         return gRep;
38     }
39 
40     public Reporter() {
41         // assumes reporter has already been statically initialized
42     }
43 
44     public Reporter(String logfile) {
45         setLogFile(logfile);
46     }
47 
48     public synchronized void setLogFile(String logfile) {
49         //System.out.println("SETTING REPORTS TO: " + logfile);
50         logOn = (logfile != null);
51         this.logfile = logfile;
52         try {
53             if (lf != null) {
54                 lf.close();
55             }
56             if (logOn) {
57                 lf = new RandomAccessFile(logfile, "rw");
58                 lf.seek(lf.length());
59             }
60         } catch (IOException ie) {
61             ie.printStackTrace();
62         }
63     }
64 
65     static {
66         reader = new BufferedReader(new InputStreamReader(System.in));
67     }
68 
69     public void setDebug(boolean on) {
70         debug = on;
71     }
72 
73     public void setActive(boolean t) {
74         active = t;
75     }
76 
77     public void debug(String s) {
78         if (debug) {
79             report(s);
80         }
81     }
82 
83     public void debugb(String s) {
84         if (debug) {
85             reportb(s);
86         }
87     }
88 
89     public void addReportListener(ReportListener rl) {
90         listeners.addElement(rl);
91     }
92 
93     public void removeReportListener(ReportListener rl) {
94         listeners.removeElement(rl);
95     }
96 
97     private void callListeners(String s) {
98         for (int i = 0; i < listeners.size(); i++) {
99             ((ReportListener) listeners.elementAt(i)).reportMessage(s);
100         }
101     }
102 
103     /**
104      * Non-negative value indicating priority threshold for reporting messages.
105      */
106     public int level = 0;
107 
108     /**
109      * disable the output to console
110      */
111     public void consoleOff() {
112         console = false;
113     }
114 
115     /**
116      * Set the output to console
117      */
118     public void consoleOn() {
119         console = true;
120     }
121 
122     /**
123      * Print a message to the current output stream.
124      *
125      * @param s the string to print.
126      */
127     public synchronized void println(String s) {
128         report(s);
129     }
130 
131     /**
132      * Print a message to the current output stream. Does not print a line feed
133      * at the end of the string.
134      *
135      * @param s the string to print.
136      */
137     public synchronized void print(String s) {
138         reportb(s);
139     }
140 
141     /**
142      * Print a message to the current output stream.
143      *
144      * @param s the string to print.
145      */
146     public synchronized void report(String s) {
147         if (active) {
148             if (console) {
149                 System.err.println(s);
150             }
151             if (logOn) {
152                 appendLog(s + "\n");
153             }
154             callListeners(s + "\n");
155         }
156     }
157 
158     /**
159      * Print a message to the current output stream. Does not print a line feed
160      * at the end of the string.
161      *
162      * @param s the string to print.
163      */
164     public synchronized void reportb(String s) {
165         if (active) {
166             if (console) {
167                 System.err.print(s);
168             }
169             if (logOn) {
170                 appendLog(s);
171             }
172             callListeners(s);
173         }
174     }
175 
176     /**
177      * Print a message to the current output stream if the priority is high
178      * enough. Note: 0 is the highest priority. Larger numbers are of lower
179      * priority.
180      *
181      * @param s the string to print.
182      * @param priority the priority of this message
183      */
184     public synchronized void report(String s, int priority) {
185         if (active) {
186             if (priority > level) {
187                 return;
188             }
189             if (console) {
190                 System.err.println(s);
191             }
192             if (logOn) {
193                 appendLog(s + "\n");
194             }
195             callListeners(s);
196         }
197     }
198 
199     /**
200      * Logs a message to a file.
201      *
202      * @param s the message to log.
203      * @param file the name of the file to log to.
204      */
205     public synchronized static void log(String s, String file) {
206         try {
207             RandomAccessFile f = new RandomAccessFile(file, "rw");
208             f.seek(f.length());
209             f.writeBytes(s);
210             f.close();
211         } catch (IOException ie) {
212             ie.printStackTrace();
213         }
214     }
215 
216     /**
217      * Logs a message to a file.
218      *
219      * @param s the message to log.
220      */
221     public synchronized void appendLog(String s) {
222         if (logfile == null) {
223             return;
224         }
225         if (active) {
226             if (lf == null) {
227                 try {
228                     lf = new RandomAccessFile(logfile, "rw");
229                     lf.seek(lf.length());
230                 } catch (IOException ie) {
231                     System.err.println("APPENDLOG ERROR #1");
232                     //ie.printStackTrace();
233                     return;
234                 }
235             }
236             if (lf != null) {
237                 try {
238                     lf.writeBytes(s);
239                 } catch (IOException ie) {
240                     System.err.println("APPENDLOG ERROR #2");
241                 }
242             }
243         }
244     }
245 
246     /**
247      * Read a line of text from stdin.
248      *
249      * @return a line of text from stdin
250      */
251     public static String readLine() {
252         StringBuffer s = new StringBuffer();
253         try {
254             char c = 0;
255             while (c != '\n') {
256                 c = (char) System.in.read();
257                 if (c != '\n') {
258                     s.append(c);
259                 }
260             }
261         } catch (IOException e) {
262             e.printStackTrace();
263         }
264         return s.toString();
265     }
266 
267     public static double round(double f, int precision) {
268         if (Double.isNaN(f)) {
269             return f;
270         }
271         double n = 1;
272         for (int i = 0; i < precision; i++) {
273             n *= 10;
274         }
275         return java.lang.Math.round(f * n) / n;
276     }
277 
278     public static String pad(String s, int size) {
279         if (s.length() < size) {
280             return pad(s + " ", size);
281         }
282         return s;
283     }
284 
285     public void finalize() {
286         try {
287             lf.close();
288             lf = null;
289         } catch (IOException ie) {
290             System.err.println("FINALIZE ERROR");
291             //ie.printStackTrace();
292         }
293     }
294 
295     /**
296      * Load the contents of a text file into a String
297      *
298      * @param fname the file name
299      * @return the contents of the file
300      */
301     public static String loadFile(String fname) {
302         StringBuffer sb = new StringBuffer();
303         try {
304             File file = new File(fname);
305             InputStream is = new BufferedInputStream(new FileInputStream(file));
306             int b = -1;
307             while ((b = is.read()) != -1) {
308                 sb.append((char) b);
309             }
310             is.close();
311         } catch (IOException e) {
312             return null;
313         }
314         return sb.toString();
315     }
316 
317     /**
318      * Attempts to execute the command.
319      *
320      * @param cmnd the command to execute
321      * @return null if unsuccessful, or the output of the command
322      */
323     public static String loadExec(String cmnd) {
324         StringBuffer sb = new StringBuffer();
325         try {
326             Runtime rt = java.lang.Runtime.getRuntime();
327             Process p = rt.exec(cmnd);
328             InputStream is = new BufferedInputStream(p.getInputStream());
329             p.waitFor();
330             int b = -1;
331             while ((b = is.read()) != -1) {
332                 sb.append((char) b);
333             }
334         } catch (InterruptedException ie) {
335             return null;
336         } catch (IOException e) {
337             return null;
338         }
339         return sb.toString();
340     }
341 
342     /**
343      * Prompt the user for a string.
344      *
345      * @param the text prompt
346      * @return what the user typed.
347      */
348     public String prompt(String s) {
349         reportb(s);
350         String r = readString();
351         if (r != null && r.length() > 0) {
352             return r;
353         } else {
354             return prompt(s);
355         }
356     }
357 
358     /**
359      * Return the next line typed by the user.
360      *
361      * @return what the user typed.
362      */
363     public static String readString() {
364         String aString;
365         System.out.flush(); // Make sure all output is flushed
366         try {
367             aString = reader.readLine();
368         } catch (Exception e) {
369             return null;
370         }
371         return aString;
372     }
373 
374     /**
375      * wait for the user to type enter.
376      */
377     public static void pause() {
378         try {
379             reader.readLine();
380         } catch (Exception e) {
381         }
382         //Keyboard.in.pause();
383     }
384 
385     public String format(String s) {
386         return s.toLowerCase().trim();
387     }
388 
389     public static boolean fileExists(String s) {
390         File f = new File(s);
391         return f.exists();
392     }
393 
394 }
395