0

我想使用 OSLog 活动来构建我的日志,但我遇到了 Swift 上缺少所需活动常量的问题,例如OS_ACTIVITY_CURRENTand OS_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_DEFAULT, OS_ACTIVITY_FLAG_DETACHED, OS_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 活动或在哪里可以找到常量?

4

1 回答 1

0

经过一些研究,我发现OS_ACTIVITY_CURRENT并且OS_ACTIVITY_NONE可以使用 C-Interoperability 动态加载,因为它没有暴露给 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

于 2020-08-07T09:54:30.990 回答