Thread overview
February 03

I wrote a small utility in Linux. I want to build it for Windows. He swears at some parts of the code like this:

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:

...
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);
}
...
February 04
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.
February 03

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?

February 04
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
February 04

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?

February 05
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);
```
February 04

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!

February 08

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.