How can I use `syslog` in Swift

The feature seems to be syslog()unavailable in Swift 2.

How to send a message in syslogon OS X?

+4
source share
1 answer

The problem is

void syslog(int priority, const char *message, ...);

accepts a list of variable arguments and is not imported into Swift. You can use

void vsyslog(int priority, const char *message, va_list args);

and define a wrapper function in Swift:

func syslog(priority : Int32, _ message : String, _ args : CVarArgType...) {
    withVaList(args) { vsyslog(priority, message, $0) }
}

syslog(LOG_ERR, "i=%ld, x=%f", 123, 34.56)

Note that string arguments must be passed as C strings:

syslog(LOG_ERR, "status=%s", "OK".cStringUsingEncoding(NSUTF8StringEncoding))

Alternatively, use NSLog()that also writes to the system log:

NSLog("i=%ld, x=%f, status=%@", 123, 34.56, "OK")

, OS X syslog() " " . asl_XXX , #include <asl.h> . , asl_log() Swift, asl_vlog().

+2

Source: https://habr.com/ru/post/1612105/


All Articles