KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > browser > BrowserLog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.browser;
12
13 import java.io.*;
14 import com.ibm.icu.text.DateFormat;
15 import com.ibm.icu.text.SimpleDateFormat;
16 import java.util.Date JavaDoc;
17
18 import org.eclipse.help.internal.base.*;
19
20 /**
21  * Log for messages output by external browser processes.
22  */

23 public class BrowserLog {
24     private static BrowserLog instance;
25     private String JavaDoc logFileName;
26     private boolean newSession;
27     DateFormat formatter = new SimpleDateFormat("MMM dd, yyyy kk:mm:ss.SS"); //$NON-NLS-1$
28
String JavaDoc LN = System.getProperty("line.separator"); //$NON-NLS-1$
29
/**
30      * Constructor
31      */

32     private BrowserLog() {
33         try {
34             newSession = true;
35             logFileName = HelpBasePlugin.getDefault().getStateLocation()
36                     .append("browser.log") //$NON-NLS-1$
37
.toOSString();
38         } catch (Exception JavaDoc e) {
39             // can get here if platform is shutting down
40
}
41     }
42     /**
43      * Obtains singleton
44      */

45     private static BrowserLog getInstance() {
46         if (instance == null) {
47             instance = new BrowserLog();
48         }
49         return instance;
50     }
51     /**
52      * Appends a line to the browser.log
53      */

54     public static synchronized void log(String JavaDoc message) {
55         getInstance().append(message);
56     }
57     private void append(String JavaDoc message) {
58         if (logFileName == null) {
59             return;
60         }
61         Writer outWriter = null;
62         try {
63             outWriter = new BufferedWriter(new OutputStreamWriter(
64                     new FileOutputStream(logFileName, true), "UTF-8")); //$NON-NLS-1$
65
if (newSession) {
66                 newSession = false;
67                 outWriter.write(LN + formatter.format(new Date JavaDoc())
68                         + " NEW SESSION" + LN); //$NON-NLS-1$
69
}
70             outWriter.write(formatter.format(new Date JavaDoc()) + " " + message + LN); //$NON-NLS-1$
71
outWriter.flush();
72             outWriter.close();
73         } catch (Exception JavaDoc e) {
74             if (outWriter != null) {
75                 try {
76                     outWriter.close();
77                 } catch (IOException ioe) {
78                 }
79             }
80         }
81     }
82 }
83
Popular Tags