@logtape/windows-eventlog
Version:
Windows Event Log sink for LogTape
78 lines (76 loc) • 2.62 kB
JavaScript
import { getLogger } from "@logtape/logtape";
//#region ffi.node.ts
/**
* Node.js FFI implementation for Windows Event Log API using koffi
*/
var WindowsEventLogFFI = class {
eventSource = null;
koffi = null;
sourceName;
initialized = false;
lib = null;
metaLogger = getLogger([
"logtape",
"meta",
"windows-eventlog"
]);
constructor(sourceName) {
this.sourceName = sourceName;
}
/**
* Initialize the FFI bindings and register event source
*/
async initialize() {
if (this.initialized) return;
try {
const koffiModule = await import("koffi");
this.koffi = koffiModule.default || koffiModule;
this.lib = this.koffi.load("advapi32.dll");
const RegisterEventSourceA = this.lib.func("uintptr __stdcall RegisterEventSourceA(uintptr lpUNCServerName, str lpSourceName)");
const ReportEventA = this.lib.func("bool __stdcall ReportEventA(uintptr hEventLog, uint16 wType, uint16 wCategory, uint32 dwEventID, uintptr lpUserSid, uint16 wNumStrings, uint32 dwDataSize, char** lpStrings, uint8* lpRawData)");
const DeregisterEventSource = this.lib.func("bool __stdcall DeregisterEventSource(uintptr hEventLog)");
this.RegisterEventSourceA = RegisterEventSourceA;
this.ReportEventA = ReportEventA;
this.DeregisterEventSource = DeregisterEventSource;
this.eventSource = this.RegisterEventSourceA(0, this.sourceName);
if (!this.eventSource || this.eventSource === 0) throw new Error(`Failed to register event source: ${this.sourceName}`);
this.initialized = true;
} catch (error) {
throw new Error(`Failed to initialize Windows Event Log FFI: ${error}`);
}
}
RegisterEventSourceA = null;
ReportEventA = null;
DeregisterEventSource = null;
/**
* Write an event to Windows Event Log
*/
writeEvent(eventType, eventId, message) {
if (!this.initialized || !this.eventSource || !this.ReportEventA) return;
try {
const messageWithNull = message + "\0";
const messages = [messageWithNull];
const success = this.ReportEventA(this.eventSource, eventType, 0, eventId, 0, 1, 0, messages, null);
if (!success) throw new Error("ReportEventA() returned false.");
} catch (error) {
throw error;
}
}
/**
* Clean up resources
*/
dispose() {
if (this.initialized && this.eventSource && this.DeregisterEventSource) {
try {
this.DeregisterEventSource(this.eventSource);
} catch (error) {
this.metaLogger.error("Failed to deregister event source during cleanup: {error}", { error });
}
this.eventSource = null;
this.initialized = false;
}
}
};
//#endregion
export { WindowsEventLogFFI };
//# sourceMappingURL=ffi.node.js.map