]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/tool/tzu/Logger.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / tool / tzu / Logger.java
1 /*\r
2  * ******************************************************************************\r
3  * Copyright (C) 2007, International Business Machines Corporation and others.\r
4  * All Rights Reserved.\r
5  * ******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.tool.tzu;\r
8 \r
9 import java.awt.Component;\r
10 import java.io.File;\r
11 import java.io.FileNotFoundException;\r
12 import java.io.FileOutputStream;\r
13 import java.io.PrintStream;\r
14 import java.util.Date;\r
15 \r
16 import javax.swing.JOptionPane;\r
17 \r
18 /**\r
19  * A singleton object that handles output to the screen and to a log file. Get the current instance\r
20  * of the logger with <code>getInstance</code> and use the output functions to output to the\r
21  * screen, the log file, the status bar, and in dialog messages.\r
22  */\r
23 public class Logger {\r
24 \r
25     /**\r
26      * A default name to use for creating a log file.\r
27      */\r
28     public static final String DEFAULT_FILENAME = "icutzu.log";\r
29 \r
30     /**\r
31      * The single instance of the logger.\r
32      */\r
33     private static Logger logger = null;\r
34 \r
35     /**\r
36      * Normal mode.\r
37      */\r
38     public static final int NORMAL = 0;\r
39 \r
40     /**\r
41      * Quiet mode.\r
42      */\r
43     public static final int QUIET = -1;\r
44 \r
45     /**\r
46      * Verbose mode.\r
47      */\r
48     public static final int VERBOSE = 1;\r
49 \r
50     /**\r
51      * Gets the instance of the logger, constructing a new one with <code>filename</code> and\r
52      * <code>verbosity</code> if one is not already constructed.\r
53      * \r
54      * @param logFile\r
55      *            The file to use for logging output.\r
56      * @param verbosity\r
57      *            The verbosity for output to the screen. Should be one of the following:\r
58      *            <ul>\r
59      *            <li>QUIET</li>\r
60      *            <li>NORMAL</li>\r
61      *            <li>VERBOSE</li>\r
62      *            </ul>\r
63      * @return The instance of the logger.\r
64      * @throws FileNotFoundException\r
65      */\r
66     public static synchronized Logger getInstance(File logFile, int verbosity)\r
67             throws FileNotFoundException {\r
68         if (logger == null) {\r
69             logger = new Logger(logFile, verbosity, null, null);\r
70         }\r
71         return logger;\r
72     }\r
73 \r
74     /**\r
75      * Gets the instance of the logger, constructing a new one with <code>filename</code> and\r
76      * <code>verbosity</code> if one is not already constructed. If a statusbar is given, status\r
77      * messages will be sent to it. If a dialogParent is specified, dialog messages will be\r
78      * displayed.\r
79      * \r
80      * @param logFile\r
81      *            The file to use for logging output.\r
82      * @param verbosity\r
83      *            The verbosity for output to the screen. Should be one of the following:\r
84      *            <ul>\r
85      *            <li>QUIET</li>\r
86      *            <li>NORMAL</li>\r
87      *            <li>VERBOSE</li>\r
88      *            </ul>\r
89      * @param statusBar\r
90      *            The status bar for status-bar messages, or null if none is present.\r
91      * @param dialogParent\r
92      *            The parent for dialog messages, or null if no dialog messages are wanted.\r
93      * @return The instance of the logger.\r
94      * @throws FileNotFoundException\r
95      */\r
96     public static synchronized Logger getInstance(File logFile, int verbosity,\r
97             ResultComponent statusComponent, Component dialogParent) throws FileNotFoundException {\r
98         if (logger == null) {\r
99             logger = new Logger(logFile, verbosity, statusComponent, dialogParent);\r
100         }\r
101         return logger;\r
102     }\r
103 \r
104     /**\r
105      * The parent to use when displaying a dialog.\r
106      */\r
107     private Component dialogParent = null;\r
108 \r
109     /**\r
110      * The means of output to the log file.\r
111      */\r
112     private PrintStream fileStream = null;\r
113 \r
114     /**\r
115      * The status bar to display status messages.\r
116      */\r
117     private ResultComponent statusComponent = null;\r
118 \r
119     /**\r
120      * The verbosity of the logger.\r
121      */\r
122     private int verbosity = NORMAL;\r
123 \r
124     /**\r
125      * Constructs a logger that outputs to the filename specified and outputs to the screen with the\r
126      * specified verbosity. Used internally.\r
127      * \r
128      * @param filename\r
129      *            The filename to use for logging output.\r
130      * @param verbosity\r
131      *            The verbosity for output to the screen.\r
132      * @param statusBar\r
133      *            The status bar for status-bar messages, or null if none is present.\r
134      * @param dialogParent\r
135      *            The parent for dialog messages, or null if no dialog messages are wanted.\r
136      * @throws FileNotFoundException\r
137      */\r
138     private Logger(File logFile, int verbosity, ResultComponent statusComponent,\r
139             Component dialogParent) throws FileNotFoundException {\r
140         if (this.fileStream != null)\r
141             this.fileStream.close();\r
142         this.fileStream = new PrintStream(new FileOutputStream(logFile.toString(), true));\r
143         this.verbosity = verbosity;\r
144         this.statusComponent = statusComponent;\r
145         this.dialogParent = dialogParent;\r
146 \r
147         this.fileStream.println();\r
148         this.fileStream.println("##### " + new Date() + " #####");\r
149         this.fileStream.println();\r
150     }\r
151 \r
152     /**\r
153      * Prints an error message to the screen and to the log.\r
154      * \r
155      * @param message\r
156      *            The message to print.\r
157      */\r
158     public void error(String message) {\r
159         if (statusComponent != null)\r
160             statusComponent.addStatusMessage(message);\r
161         System.err.print(message);\r
162         if (fileStream != null)\r
163             fileStream.print(message);\r
164     }\r
165 \r
166     /**\r
167      * Prints an error message to the screen and to the log, and terminates the line.\r
168      * \r
169      * @param message\r
170      *            The message to print.\r
171      */\r
172     public void errorln(String message) {\r
173         if (statusComponent != null)\r
174             statusComponent.addStatusMessage(message);\r
175         System.err.println(message);\r
176         if (fileStream != null)\r
177             fileStream.println(message);\r
178     }\r
179 \r
180     /**\r
181      * Returns the current allowed verbosity.\r
182      * \r
183      * @return The current allowed verbosity for output to the screen. Should be one of the\r
184      *         following:\r
185      *         <ul>\r
186      *         <li>QUIET</li>\r
187      *         <li>NORMAL</li>\r
188      *         <li>VERBOSE</li>\r
189      *         </ul>\r
190      */\r
191     public int getVerbosity() {\r
192         return verbosity;\r
193     }\r
194 \r
195     /**\r
196      * Sets the allowed verbosity.\r
197      * \r
198      * @param verbosity\r
199      *            The desired allowed verbosity for output to the screen. Should be one of the\r
200      *            following:\r
201      *            <ul>\r
202      *            <li>QUIET</li>\r
203      *            <li>NORMAL</li>\r
204      *            <li>VERBOSE</li>\r
205      *            </ul>\r
206      */\r
207     public void setVerbosity(int verbosity) {\r
208         this.verbosity = verbosity;\r
209     }\r
210 \r
211     /**\r
212      * Logs a message to the screen if the logger is in a mode higher than normal mode (ie.\r
213      * VERBOSE), and always logs the message to the file.\r
214      * \r
215      * @param message\r
216      *            The message to print.\r
217      */\r
218     public void loglnToBoth(String message) {\r
219         loglnToScreen(message);\r
220         loglnToFile(message);\r
221     }\r
222 \r
223     /**\r
224      * Logs a message to the file used by this logger, and terminates the line.\r
225      * \r
226      * @param message\r
227      *            The message to print.\r
228      */\r
229     public void loglnToFile(String message) {\r
230         if (fileStream != null)\r
231             fileStream.println(message);\r
232     }\r
233 \r
234     /**\r
235      * Logs a message to the screen if the logger is in a mode higher than normal mode (ie.\r
236      * VERBOSE), and terminates the line.\r
237      * \r
238      * @param message\r
239      *            The message to print.\r
240      */\r
241     public void loglnToScreen(String message) {\r
242         if (verbosity > NORMAL) {\r
243             if (statusComponent != null)\r
244                 statusComponent.addStatusMessage(message);\r
245             System.out.println(message);\r
246         }\r
247     }\r
248 \r
249     /**\r
250      * Prints a message to the screen and to the log.\r
251      * \r
252      * @param message\r
253      *            The message to print.\r
254      */\r
255     public void logStackTraceToBoth(Exception ex) {\r
256         logStackTraceToScreen(ex);\r
257         logStackTraceToFile(ex);\r
258     }\r
259 \r
260     /**\r
261      * Logs the stack trace to the file.\r
262      * \r
263      * @param ex\r
264      *            The exception\r
265      */\r
266     public void logStackTraceToFile(Exception ex) {\r
267         if (fileStream != null)\r
268             ex.printStackTrace(fileStream);\r
269     }\r
270 \r
271     /**\r
272      * Logs the stack trace to the file.\r
273      * \r
274      * @param ex\r
275      *            The exception\r
276      */\r
277     public void logStackTraceToScreen(Exception ex) {\r
278         if (verbosity > NORMAL)\r
279             ex.printStackTrace(System.out);\r
280     }\r
281 \r
282     /**\r
283      * Prints a message to the screen and to the log, and terminates the line.\r
284      * \r
285      * @param message\r
286      *            The message to print.\r
287      */\r
288     public void printlnToBoth(String message) {\r
289         printlnToScreen(message);\r
290         loglnToFile(message);\r
291     }\r
292 \r
293     /**\r
294      * Prints a message to the screen, and terminates the line.\r
295      * \r
296      * @param message\r
297      *            The message to print.\r
298      */\r
299     public void printlnToScreen(String message) {\r
300         if (verbosity >= NORMAL) {\r
301             if (statusComponent != null)\r
302                 statusComponent.addStatusMessage(message);\r
303             System.out.println(message);\r
304         }\r
305     }\r
306 \r
307     /**\r
308      * If dialogParent is not null, brings up an informative dialog about something the user should\r
309      * be aware of.\r
310      * \r
311      * @param message\r
312      *            The message to the user.\r
313      */\r
314     public void showInformationDialog(String message) {\r
315         if (dialogParent != null)\r
316             JOptionPane.showMessageDialog(dialogParent, message, "INFORMATION MESSAGE",\r
317                     JOptionPane.INFORMATION_MESSAGE);\r
318     }\r
319 }\r