// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.osutil;
/**
* This class provides an interface to Windows NT event logging.
*
* @version $Revision$, $Date$
*/
public class NTEventLogger {
private byte[] handle; // Must be non-null. Should be non-empty.
private boolean open; // true if this log is open for writing.
// The Windows NT event ID. The actual event ID is 1001, but we also
// need to set bit 29 to indicate that we are not Microsoft.
// See _Windows NT Event Logging_ (O'Reilly), page 128, for an explanation.
public static final int EVENT_ID = 0x20000000 | 1001;
private NTEventLogger() {
}
// Load native library. This is the same library used by OsSubsystem.
// loadLibrary() is idempotent, so don't worry about calling it multiple
// times.
static {
boolean mNativeLibrariesLoaded = false;
String os = System.getProperty( "os.name" );
if( ( os.equals( "Linux" ) ) ) {
// Check for 64-bit library availability
// prior to 32-bit library availability.
mNativeLibrariesLoaded =
OSUtil.tryLoad( "/usr/lib64/osutil/libosutil.so" );
if( mNativeLibrariesLoaded ) {
System.out.println( "64-bit osutil library loaded" );
} else {
// REMINDER: May be trying to run a 32-bit app
// on 64-bit platform.
mNativeLibrariesLoaded =
OSUtil.tryLoad( "/usr/lib/osutil/libosutil.so" );
if( mNativeLibrariesLoaded ) {
System.out.println( "32-bit osutil library loaded");
} else {
System.out.println( "FAILED loading osutil library!");
System.exit( -1 );
}
}
} else {
try {
System.loadLibrary( "osutil" );
System.out.println( "osutil library loaded" );
mNativeLibrariesLoaded = true;
} catch( Throwable t ) {
// This is bad news, the program is doomed at this point
t.printStackTrace();
}
}
}
/**
* Creates an application or system NT Event log handle.
*
* @param eventSourceName The name of the source application.
* This should be a subkey of
* HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\EventLog
.
* If the given source does not exist, NT will automatically return
* a handle to the application log without complaining.
* The reason to use a real source, rather than just using the default
* application log, is that the registry key of a real source
* tells NT where to find the message resources for formatting
* the strings that are sent in reportEvent
.
* If the default application log is used, NT will insert a lame
* message: "The description for Event ID ( ... ) in Source
* ( ... ) could not be found. It contains the following insertion
* string(s):".
* @throws Exception If an error occurs in the native NT code. The
* exception message will contain more information.
*/
public NTEventLogger(String eventSourceName) throws Exception {
open = true;
handle = initNTLog(eventSourceName);
}
/**
* Writes a string to the log. This is the simple way to write to the
* NT event log.
*
This method must not be called after the log is * closed. * * @param type The type of the event that is logged. Pre-defined types * are: *
Name | Description |
---|---|
EVENTLOG_SUCCESS | Success |
EVENTLOG_ERROR_TYPE | Error |
EVENTLOG_WARNING_TYPE | Warning |
EVENTLOG_INFORMATION_TYPE | Informational |
EVENTLOG_AUDIT_SUCCESS | Success Audit |
EVENTLOG_AUDIT_FAILURE | Failure Audit |
This method must not be called after the log is * closed. * * @param type The type of the event that is logged. Pre-defined types * are: *
Name | Description |
---|---|
EVENTLOG_SUCCESS | Success |
EVENTLOG_ERROR_TYPE | Error |
EVENTLOG_WARNING_TYPE | Warning |
EVENTLOG_INFORMATION_TYPE | Informational |
EVENTLOG_AUDIT_SUCCESS | Success Audit |
EVENTLOG_AUDIT_FAILURE | Failure Audit |