www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Logging logs in Windows

reply Alexander Zhirov <azhirov1991 gmail.com> writes:
I wrote a small utility in Linux. I want to build it for Windows. 
He swears at some parts of the code like this:

```powershell
C:\sources\pxe-restore>dub build -b release
     Starting Performing "release" build using dmd for x86_64.
     Building pxe-restore ~master: building configuration 
[application]
source\azh\log.d(3,8): Error: module `core.sys.posix.syslog` 
import `syslog` not found
source\azh\query.d(7,8): Error: module `core.sys.posix.syslog` 
import `LOG_NOTICE` not found
source\azh\query.d(7,8): Error: module `core.sys.posix.syslog` 
import `LOG_ERR` not found
source\azh\query.d(7,8): Error: module `core.sys.posix.syslog` 
import `LOG_INFO` not found
source\azh\query.d(7,8): Error: module `core.sys.posix.syslog` 
import `LOG_WARNING` not found
source\app.d(12,8): Error: module `core.sys.posix.syslog` import 
`LOG_NOTICE` not found
source\app.d(12,8): Error: module `core.sys.posix.syslog` import 
`LOG_ERR` not found
source\app.d(12,8): Error: module `core.sys.posix.syslog` import 
`LOG_INFO` not found
source\app.d(12,8): Error: module `core.sys.posix.syslog` import 
`LOG_WARNING` not found
Error dmd failed with exit code 1.
```

A small fragment of the code used:

```d
...
import core.sys.posix.syslog : LOG_NOTICE, LOG_ERR, LOG_INFO, 
LOG_WARNING;
import core.sys.posix.syslog : syslog;
...
void log(int priority, string message)
{
     syslog(priority, (message ~ "\0").ptr);
}
...
```
Feb 03 2023
parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
Yes syslog is not available on Windows as that is a Posix API. All of 
your calls to syslog should be guarded by a version for Posix.

The Windows Event Log which is comparable, isn't 1:1 so its a bit of a 
task to replace it.
Feb 03 2023
parent reply Alexander Zhirov <azhirov1991 gmail.com> writes:
On Friday, 3 February 2023 at 16:00:55 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
 Yes syslog is not available on Windows as that is a Posix API. 
 All of your calls to syslog should be guarded by a version for 
 Posix.
Is there an analogue for Windows? And is it possible to implement it with the similarity of directives, as in C?
Feb 03 2023
parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
On 04/02/2023 6:55 AM, Alexander Zhirov wrote:
 On Friday, 3 February 2023 at 16:00:55 UTC, Richard (Rikki) Andrew 
 Cattermole wrote:
 Yes syslog is not available on Windows as that is a Posix API. All of 
 your calls to syslog should be guarded by a version for Posix.
Is there an analogue for Windows? And is it possible to implement it with the similarity of directives, as in C?
The analogue is the Windows Event Log and its not 1:1. Here is a starting point that I myself have used in the past: https://github.com/php/php-src/blob/master/win32/wsyslog.c
Feb 03 2023
parent reply Alexander Zhirov <azhirov1991 gmail.com> writes:
On Friday, 3 February 2023 at 18:02:59 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
 Here is a starting point that I myself have used in the past:
I understand that programming under Windows is a shame for a programmer, but is there really no ready-made solution for using the system log in Windows?
Feb 04 2023
next sibling parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
On 05/02/2023 2:31 AM, Alexander Zhirov wrote:
 On Friday, 3 February 2023 at 18:02:59 UTC, Richard (Rikki) Andrew 
 Cattermole wrote:
 Here is a starting point that I myself have used in the past:
I understand that programming under Windows is a shame for a programmer, but is there really no ready-made solution for using the system log in Windows?
Not at all. syslog is the system api for logging on Posix, event log is the Windows equivalent system api. They are both good in their own ways, even if they are not 1:1. I can't see anything on dub-registry or in Phobos. But its not hard to implement. 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); ```
Feb 04 2023
next sibling parent Alexander Zhirov <azhirov1991 gmail.com> writes:
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'll try to check. Thank you very much!
Feb 04 2023
prev sibling parent reply Alexander Zhirov <azhirov1991 gmail.com> writes:
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?
Apr 18 2023
parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
Sounds like a simple case of not linking against the right library:

https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-reporteventa

Library	Advapi32.lib
DLL	Advapi32.dll

Should be as simple as adding advapi32 to your libs directive.
Apr 18 2023
parent Alexander Zhirov <azhirov1991 gmail.com> writes:
On Tuesday, 18 April 2023 at 12:08:35 UTC, Richard (Rikki) Andrew 
Cattermole wrote:
 Sounds like a simple case of not linking against the right 
 library:

 https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-reporteventa

 Library	Advapi32.lib
 DLL	Advapi32.dll

 Should be as simple as adding advapi32 to your libs directive.
Yes, you were right, it helped. [I will know that there is a footnote at the bottom with a description](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-reporteventa#requirements)
Apr 18 2023
prev sibling parent Kagamin <spam here.lot> writes:
On Saturday, 4 February 2023 at 13:31:41 UTC, Alexander Zhirov 
wrote:
 I understand that programming under Windows is a shame for a 
 programmer, but is there really no ready-made solution for 
 using the system log in Windows?
It would be a logging library like log4j that would have different logging backends.
Feb 07 2023