如何在Swift中使用`syslog` [英] How can I use `syslog` in Swift

查看:156
本文介绍了如何在Swift中使用`syslog`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来像syslog()函数在Swift 2中不可用.

Looks like syslog() function is not available in Swift 2.

如何在OS X上向syslog发送消息?

How can I send a message to syslog on OS X?

推荐答案

问题是

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

具有可变参数列表,并且不会导入到Swift中.你可以 使用

takes a variable argument list and is not imported to Swift. You can use

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

相反,在Swift中定义包装函数:

instead 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)

请注意,字符串参数必须作为C字符串传递:

Note that string arguments have to be passed as C strings:

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


或者,使用NSLog()也会写入系统日志:


Alternatively, use NSLog() which writes to the system log as well:

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


还请注意,在OS X上,syslog()只是对 .你可以打电话 asl_XXX直接起作用,您只需#include <asl.h> 在桥接头文件中.如上所述,asl_log()不是 导入到Swift中,您必须调用asl_vlog().


Note also that on OS X, syslog() is just a wrapper to the "Apple System Logger facility". You can call the asl_XXX functions directly, you only have to #include <asl.h> in the bridging header file. As above, asl_log() is not imported to Swift and you have to call asl_vlog() instead.

这篇关于如何在Swift中使用`syslog`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆