16
|
1 |
/*
|
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
package org.eclipse.core.resources;
|
|
18 |
|
|
19 |
import java.io.File;
|
|
20 |
import java.io.IOException;
|
|
21 |
import java.util.logging.FileHandler;
|
|
22 |
import java.util.logging.Handler;
|
|
23 |
import java.util.logging.Level;
|
|
24 |
import java.util.logging.LogRecord;
|
|
25 |
import java.util.logging.Logger;
|
|
26 |
|
|
27 |
public class DiagnosticLog {
|
|
28 |
private String name;
|
|
29 |
private String id;
|
|
30 |
private Logger logger;
|
|
31 |
private File file;
|
|
32 |
|
|
33 |
private class LogHandler extends Handler {
|
|
34 |
|
|
35 |
FileHandler fileHandler;
|
|
36 |
File file;
|
|
37 |
|
|
38 |
public LogHandler(File file) {
|
|
39 |
this.file = file;
|
|
40 |
}
|
|
41 |
|
|
42 |
public void close() throws SecurityException {
|
|
43 |
if (fileHandler != null)
|
|
44 |
fileHandler.close();
|
|
45 |
}
|
|
46 |
|
|
47 |
public void flush() {
|
|
48 |
if (fileHandler != null)
|
|
49 |
fileHandler.flush();
|
|
50 |
}
|
|
51 |
|
|
52 |
public void publish(LogRecord record) {
|
|
53 |
if (fileHandler == null)
|
|
54 |
{
|
|
55 |
try {
|
|
56 |
fileHandler= new FileHandler(file.getAbsolutePath());
|
|
57 |
} catch (SecurityException e) {
|
|
58 |
e.printStackTrace();
|
|
59 |
} catch (IOException e) {
|
|
60 |
e.printStackTrace();
|
|
61 |
}
|
|
62 |
}
|
|
63 |
if (fileHandler != null)
|
|
64 |
fileHandler.publish(record);
|
|
65 |
}
|
|
66 |
|
|
67 |
}
|
|
68 |
|
|
69 |
public DiagnosticLog(String name, String id, String logFilePath) {
|
|
70 |
this.name = name;
|
|
71 |
this.id = id;
|
|
72 |
this.logger = Logger.getLogger(id);
|
|
73 |
logger.setLevel(Level.OFF);
|
|
74 |
logger.setUseParentHandlers(false);
|
|
75 |
this.file = new File(logFilePath);
|
|
76 |
logger.addHandler(new LogHandler(file));
|
|
77 |
}
|
|
78 |
|
|
79 |
public String getName() {
|
|
80 |
return name;
|
|
81 |
}
|
|
82 |
|
|
83 |
public String getId() {
|
|
84 |
return id;
|
|
85 |
}
|
|
86 |
|
|
87 |
public File getFile() {
|
|
88 |
return file;
|
|
89 |
}
|
|
90 |
|
|
91 |
public Logger getLogger() {
|
|
92 |
return logger;
|
|
93 |
}
|
|
94 |
|
|
95 |
} |