1 | /* |
2 | * $Id: EventLogHandler.java,v 1.1 2006/03/07 17:52:10 hastings Exp $ |
3 | * |
4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
5 | * |
6 | * All Rights Reserved |
7 | * |
8 | * This material may be reproduced by or for the U. S. Government |
9 | * pursuant to the copyright license under the clause at |
10 | * DFARS 252.227-7014 (OCT 2001). |
11 | */ |
12 | package com.moesol.bindings.platform_sdk.debugging_and_error_handling; |
13 | |
14 | import java.util.logging.ErrorManager; |
15 | import java.util.logging.Formatter; |
16 | import java.util.logging.Handler; |
17 | import java.util.logging.Level; |
18 | import java.util.logging.LogRecord; |
19 | import java.util.logging.SimpleFormatter; |
20 | |
21 | import com.moesol.bindings.platform_sdk.component_services.COMException; |
22 | import com.moesol.bindings.platform_sdk.component_services.HRESULT; |
23 | import com.moesol.bindings.platform_sdk.windows_api.PlatformSDK; |
24 | |
25 | /** |
26 | * Copy java logging records to Windows Event Log. |
27 | * |
28 | * @author Hastings |
29 | */ |
30 | public class EventLogHandler extends Handler { |
31 | |
32 | // |
33 | // The types of events that can be logged. |
34 | // |
35 | public static final int EVENTLOG_SUCCESS = 0x0000; |
36 | public static final int EVENTLOG_ERROR_TYPE = 0x0001; |
37 | public static final int EVENTLOG_WARNING_TYPE = 0x0002; |
38 | public static final int EVENTLOG_INFORMATION_TYPE = 0x0004; |
39 | public static final int EVENTLOG_AUDIT_SUCCESS = 0x0008; |
40 | public static final int EVENTLOG_AUDIT_FAILURE = 0x0010; |
41 | |
42 | public EventLogHandler() { |
43 | super(); |
44 | |
45 | if (getFormatter() == null) { |
46 | setFormatter(new SimpleFormatter()); |
47 | } |
48 | } |
49 | public void publish(LogRecord record) { |
50 | if (!isLoggable(record)) { |
51 | return; |
52 | } |
53 | |
54 | Formatter fmt = getFormatter(); |
55 | String msg = fmt.format(record); |
56 | int nt_eventlog_level = computeNtEventLogLevel(record.getLevel()); |
57 | if (!ReportEvent(nt_eventlog_level, msg)) { |
58 | reportError( |
59 | "ReportEvent failed", |
60 | new COMException(HRESULT.HRESULT_FROM_NT(PlatformSDK.GetLastError())), |
61 | ErrorManager.WRITE_FAILURE |
62 | ); |
63 | } |
64 | } |
65 | |
66 | private int computeNtEventLogLevel(Level level) { |
67 | if (level.intValue() >= Level.SEVERE.intValue()) { |
68 | return EVENTLOG_ERROR_TYPE; |
69 | } |
70 | if (level.intValue() >= Level.WARNING.intValue()) { |
71 | return EVENTLOG_WARNING_TYPE; |
72 | } |
73 | if (level.intValue() >= Level.INFO.intValue()) { |
74 | return EVENTLOG_INFORMATION_TYPE; |
75 | } |
76 | |
77 | return EVENTLOG_SUCCESS; |
78 | } |
79 | |
80 | private boolean ReportEvent(int nt_eventlog_level, String msg) { |
81 | return jni_ReportEvent(nt_eventlog_level, msg); |
82 | } |
83 | |
84 | private static native boolean jni_ReportEvent(int nt_eventlog_level, String msg); |
85 | |
86 | public void flush() { |
87 | // done |
88 | } |
89 | |
90 | public void close() throws SecurityException { |
91 | // done |
92 | } |
93 | |
94 | } |