2 * ******************************************************************************
\r
3 * Copyright (C) 2007, International Business Machines Corporation and others.
\r
4 * All Rights Reserved.
\r
5 * ******************************************************************************
\r
7 package com.ibm.icu.dev.tool.tzu;
\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
16 import javax.swing.JOptionPane;
\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
23 public class Logger {
\r
26 * A default name to use for creating a log file.
\r
28 public static final String DEFAULT_FILENAME = "icutzu.log";
\r
31 * The single instance of the logger.
\r
33 private static Logger logger = null;
\r
38 public static final int NORMAL = 0;
\r
43 public static final int QUIET = -1;
\r
48 public static final int VERBOSE = 1;
\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
55 * The file to use for logging output.
\r
57 * The verbosity for output to the screen. Should be one of the following:
\r
63 * @return The instance of the logger.
\r
64 * @throws FileNotFoundException
\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
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
81 * The file to use for logging output.
\r
83 * The verbosity for output to the screen. Should be one of the following:
\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
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
105 * The parent to use when displaying a dialog.
\r
107 private Component dialogParent = null;
\r
110 * The means of output to the log file.
\r
112 private PrintStream fileStream = null;
\r
115 * The status bar to display status messages.
\r
117 private ResultComponent statusComponent = null;
\r
120 * The verbosity of the logger.
\r
122 private int verbosity = NORMAL;
\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
129 * The filename to use for logging output.
\r
131 * The verbosity for output to the screen.
\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
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
147 this.fileStream.println();
\r
148 this.fileStream.println("##### " + new Date() + " #####");
\r
149 this.fileStream.println();
\r
153 * Prints an error message to the screen and to the log.
\r
156 * The message to print.
\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
167 * Prints an error message to the screen and to the log, and terminates the line.
\r
170 * The message to print.
\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
181 * Returns the current allowed verbosity.
\r
183 * @return The current allowed verbosity for output to the screen. Should be one of the
\r
191 public int getVerbosity() {
\r
196 * Sets the allowed verbosity.
\r
199 * The desired allowed verbosity for output to the screen. Should be one of the
\r
207 public void setVerbosity(int verbosity) {
\r
208 this.verbosity = verbosity;
\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
216 * The message to print.
\r
218 public void loglnToBoth(String message) {
\r
219 loglnToScreen(message);
\r
220 loglnToFile(message);
\r
224 * Logs a message to the file used by this logger, and terminates the line.
\r
227 * The message to print.
\r
229 public void loglnToFile(String message) {
\r
230 if (fileStream != null)
\r
231 fileStream.println(message);
\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
239 * The message to print.
\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
250 * Prints a message to the screen and to the log.
\r
253 * The message to print.
\r
255 public void logStackTraceToBoth(Exception ex) {
\r
256 logStackTraceToScreen(ex);
\r
257 logStackTraceToFile(ex);
\r
261 * Logs the stack trace to the file.
\r
266 public void logStackTraceToFile(Exception ex) {
\r
267 if (fileStream != null)
\r
268 ex.printStackTrace(fileStream);
\r
272 * Logs the stack trace to the file.
\r
277 public void logStackTraceToScreen(Exception ex) {
\r
278 if (verbosity > NORMAL)
\r
279 ex.printStackTrace(System.out);
\r
283 * Prints a message to the screen and to the log, and terminates the line.
\r
286 * The message to print.
\r
288 public void printlnToBoth(String message) {
\r
289 printlnToScreen(message);
\r
290 loglnToFile(message);
\r
294 * Prints a message to the screen, and terminates the line.
\r
297 * The message to print.
\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
308 * If dialogParent is not null, brings up an informative dialog about something the user should
\r
312 * The message to the user.
\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