任何人都可以提出一些建议吗?
解决方法
Privacy
The unified logging system considers dynamic strings and complex dynamic objects to be private,and does not collect them automatically. To ensure the privacy of users,it is recommended that log messages consist strictly of static strings and numbers. In situations where it is necessary to capture a dynamic string,you may explicitly declare the string public using the keyword public. For example,
%{public}s
.
>资料来源:https://developer.apple.com/reference/os/1891852-logging
> WWDC视频:https://developer.apple.com/videos/play/wwdc2016/721/?time=714
示例(ObjC):
os_log_t log = os_log_create("com.example.my-subsystem","test"); const char *staticString = "I am static string!"; const char *dynamicString = [[Nsstring stringWithFormat:@"I am %@!",@"dynamic string"] cStringUsingEncoding:NSUTF8StringEncoding]; os_log(log,"Message: %s",staticString); os_log(log,dynamicString); os_log(log,"Message: %{public}s",dynamicString); // Output // [test] Message: I am static string! // [test] Message: <private> // [test] Message: I am dynamic string!
示例(Swift):
目前在Swift(https://openradar.appspot.com/radar?id=6068967584038912)中记录字符串似乎已被破坏,因此我们需要手动将C函数桥接到Swift:
更新01.雷达28599032是修复程序,不适用于Xcode 10.
// File: os_log_rdar28599032.h #import <Foundation/Foundation.h> #import <os/log.h> void log_private(os_log_t,os_log_type_t,Nsstring *); void log_public(os_log_t,Nsstring *); // File: os_log_rdar28599032.m #import "os_log_rdar28599032.h" void log_private(os_log_t log,os_log_type_t type,Nsstring * message) { os_log_with_type(log,type,"%s",[message cStringUsingEncoding:NSUTF8StringEncoding]); } void log_public(os_log_t log,"%{public}s",[message cStringUsingEncoding:NSUTF8StringEncoding]); } // File: ViewController.swift import Cocoa import os.log import os.activity class ViewController: NSViewController { static let log = oslog(subsystem: "com.example.my-subsystem",category: "test") typealias SelfClass = ViewController override func viewDidLoad() { super.viewDidLoad() log_private(SelfClass.log,.fault,"I am dynamic \("string")!") log_public(SelfClass.log,"I am dynamic \("string")!") log_private(SelfClass.log,#file) log_public(SelfClass.log,#file) } } // Output // [test] <private> // [test] I am dynamic string! // [test] <private> // [test] /[REDACTED]/ViewController.swift