无法在 Swift 上创建 OSLog 活动



我想使用OSLog活动来构建我的日志,但我在Swift上遇到了缺少所需活动常量的问题,如OS_ACTIVITY_CURRENTOS_ACTIVITY_NONE,并且在尝试创建活动时出错:

let activity = _os_activity_create(dso, strdup(name), 
OS_ACTIVITY_CURRENT, // Error: Static member 'OS_ACTIVITY_CURRENT' cannot be used on instance of type 'OSLogOutput'
OS_ACTIVITY_FLAG_DEFAULT)

os.activity框架对活动常量进行了描述,但没有任何定义(但定义了活动标志OS_ACTIVITY_FLAG_DEFAULTOS_ACTIVITY_FLAG_DETACHEDOS_ACTIVITY_FLAG_IF_NONE_PRESENT(:

/*!
* @const OS_ACTIVITY_CURRENT
*
* @discussion
* Create activity and links to the current activity if one is present.
* If no activity is present it is treated as if it is detached.
*/

如果我们查看ObjC标头<os/activity.h>,我们可以找到以下常量:

#define OS_ACTIVITY_NONE OS_OBJECT_GLOBAL_OBJECT(os_activity_t, _os_activity_none)
#define OS_ACTIVITY_CURRENT OS_OBJECT_GLOBAL_OBJECT(os_activity_t, _os_activity_current)

看起来OSLog在Swift上受到限制,那么如何处理OSLog活动,或者在哪里可以找到常量?

经过一些研究,我发现OS_ACTIVITY_CURRENTOS_ACTIVITY_NONE可以使用C互操作性动态加载,因为它不暴露于Swift:

static let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2)
static let OS_ACTIVITY_NONE = unsafeBitCast(dlsym(RTLD_DEFAULT, "_os_activity_none"), to: os_activity_t.self)
static let OS_ACTIVITY_CURRENT = unsafeBitCast(dlsym(RTLD_DEFAULT, "_os_activity_current"), to: os_activity_t.self)

但os.log框架提供API却没有提供所需的常量,这看起来仍然很奇怪。

感谢https://nsscreencast.com/episodes/347-activity-tracing-in-swift

相关内容

  • 没有找到相关文章

最新更新