微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么Xcode 8(iOS 10)在控制台中打印[LogMessageLogging]

为什么 Xcode 8(iOS 10)打印[LogMessageLogging]< private>在控制台中,当我调用地图视图时?

任何人都可以提出一些建议吗?

解决方法

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

原文地址:https://www.jb51.cc/iOS/328907.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐