KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > browser > browsers > 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.ui.internal.browser.browsers;
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.ui.internal.browser.WebBrowserUIPlugin;
19 /**
20  * Log for messages output by external browser processes.
21  */

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

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

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

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