Logging logs in Windows
Alexander Zhirov
azhirov1991 at gmail.com
Tue Apr 18 12:06:27 UTC 2023
On Saturday, 4 February 2023 at 14:48:55 UTC, Richard (Rikki)
Andrew Cattermole wrote:
> I.e. here are my functions for syslog and Windows Event log (I
> won't copy it all, it won't be helpful & the file log function
> is giant compared).
>
> ```d
> void syslog() {
> version (Posix) {
> import core.sys.posix.syslog : syslog;
>
> syslog(prioritySyslogForLevels[level],
> unsafeTextMessageComposite.ptr);
> }
> }
>
> void windowsEvents() {
> version (Windows) {
> import core.sys.windows.windows :
> ReportEventA, WORD, DWORD, EVENTLOG_INFORMATION_TYPE,
> EVENTLOG_WARNING_TYPE,
> EVENTLOG_ERROR_TYPE;
>
> static WORD[] WTypes = [
> EVENTLOG_INFORMATION_TYPE,
> EVENTLOG_INFORMATION_TYPE, EVENTLOG_WARNING_TYPE,
> EVENTLOG_ERROR_TYPE,
> EVENTLOG_ERROR_TYPE, EVENTLOG_ERROR_TYPE
> ];
>
> static DWORD[] dwEventID = [0, 0, 0, 0, 0,
> 0];
> const(char)*[2] messages = [
> ModuleLine2.ptr,
>
> cast(char*)unsafeTextMessageComposite.ptr +
> unsafeDateTime.length + ModuleLine.length
> ];
>
> ReportEventA(windowsEventHandle,
> WTypes[level], 0, dwEventID[level], cast(void*)null, 2, 0,
> &messages[0], cast(void*)null);
> }
> }
> ```
>
> Note: you also need to setup an event source on Windows i.e.
>
> ```d
> import core.sys.windows.windows : RegisterEventSourceA;
> windowsEventHandle = RegisterEventSourceA(null,
> cast(char*)processName.ptr);
> ```
>
> and to close it:
>
> ```d
> import core.sys.windows.windows : DeregisterEventSource;
> DeregisterEventSource(windowsEventHandle);
> ```
I tried to make a simple recording, but when compiling, he swears
at linking.
```d
import core.sys.windows.windows;
void writeToEventLog(LPCSTR pszSrcName, LPCSTR message) {
HANDLE hEventLog = RegisterEventSourceA(NULL, pszSrcName);
ReportEventA(hEventLog, EVENTLOG_ERROR_TYPE, 0, 0, NULL, 1,
0, &message, NULL);
DeregisterEventSource(hEventLog);
}
void main() {
writeToEventLog("test", "This is test message");
}
```
```ps
PS C:\sources\d-journals> dub
Starting Performing "debug" build using
C:\D\dmd2\windows\bin64\dmd.exe for x86_64.
Building d-journals ~master: building configuration
[application]
Linking d-journals
lld-link: error: undefined symbol: RegisterEventSourceA
>>> referenced by C:\sources\d-journals\source\app.d:4
>>>
>>> C:\Users\alexander\AppData\Local\dub\cache\d-journals\~master\build\application-debug-windows-x86_64-dmd_v2.102.0-dirty-92393AAC9FC80DD3A486D7D072118EACA853FFECDE2EEAA8920EDA92F0620E60\d-journals.obj:(_D3app15writeToEventLogFPxaQdZv)
lld-link: error: undefined symbol: ReportEventA
>>> referenced by C:\sources\d-journals\source\app.d:5
>>>
>>> C:\Users\alexander\AppData\Local\dub\cache\d-journals\~master\build\application-debug-windows-x86_64-dmd_v2.102.0-dirty-92393AAC9FC80DD3A486D7D072118EACA853FFECDE2EEAA8920EDA92F0620E60\d-journals.obj:(_D3app15writeToEventLogFPxaQdZv)
lld-link: error: undefined symbol: DeregisterEventSource
>>> referenced by C:\sources\d-journals\source\app.d:6
>>>
>>> C:\Users\alexander\AppData\Local\dub\cache\d-journals\~master\build\application-debug-windows-x86_64-dmd_v2.102.0-dirty-92393AAC9FC80DD3A486D7D072118EACA853FFECDE2EEAA8920EDA92F0620E60\d-journals.obj:(_D3app15writeToEventLogFPxaQdZv)
Error: linker exited with status 1
Error C:\D\dmd2\windows\bin64\dmd.exe failed with exit code 1.
```
What could I have missed?
More information about the Digitalmars-d-learn
mailing list