Ras*_*sto 7 xcode environment-variables setenv ios swift
通过在您的运行方案中设置OS_ACTIVITY_MODE环境变量,可以在 Swift 中隐藏 3rd 方库产生的过于冗长的日志disable- 如下图所示。
这将使NSLog您的应用程序的所有输出静音。我只想为某些呼叫禁用它。我尝试像这样设置环境变量:
setenv("OS_ACTIVITY_MODE", "disable", 1)
Run Code Online (Sandbox Code Playgroud)
而像这样
putenv(UnsafeMutablePointer<Int8>(mutating: ("OS_ACTIVITY_MODE=disable" as NSString).utf8String))
Run Code Online (Sandbox Code Playgroud)
通过调用验证环境已更改,print(ProcessInfo.processInfo.environment)但与在 XCode 运行方案中指定它不同,日志不受影响。
为什么它不起作用以及如何解决它?
当您将 OS_ACTIVITY_MODE 设置为 时disable,您将禁用有权访问此环境变量的进程(例如模拟器)的所有日志输出。
当您将该值设置为 或 时,info它将debug启用相应的日志记录模式,除非在某些日志类别的日志记录首选项中禁用它。
因此,为了仅启用特定日志类别,您必须启用OS_ACTIVITY_MODE,即将其设置为info或debug或保留默认值,然后禁用所有其他类别的日志记录。
有关如何执行此操作的更多信息,请参阅日志的手册页,man log在控制台中键入。
在控制台中输入:
xcrun simctl spawn booted log config --status
这基本上意味着:适用log config -status于所有启动的模拟器。
这可能会将以下内容打印到您的控制台:
System mode = INFO STREAM_LIVE PRIVATE_DATA
Run Code Online (Sandbox Code Playgroud)
xcrun simctl spawn booted log config --status --subsystem com.mycompany.myapp --category network
Run Code Online (Sandbox Code Playgroud)
这可能会将以下内容打印到控制台:
Mode for 'com.mycompany.myapp(network)' INFO PERSIST_DEFAULT
Run Code Online (Sandbox Code Playgroud)
如需帮助,可以使用工具xcrun simctl并log提供漂亮的手册页和帮助页。
例如:log config --help在控制台中输入,它会打印:
usage: log config [options] --mode <modes>
or: log config [options] --status
or: log config [options] --reset
description:
Read or configure settings for the logging system. Configuration
can act system-wide; or on a subsystem, category, or process level.
options:
--category <name> Get/set settings for a given category
--mode <modes> Enable given modes
--process <pid> | <name> Get/set settings for a given process
--reset Reset settings to defaults
--status Show current settings
--subsystem <name> Get/set settings for a given subsystem
modes:
Modes can be specified as a comma-separated list of key:value pairs.
Valid keys and their values are:
level off | default | info | debug
persist off | default | info | debug
stream live | default
Run Code Online (Sandbox Code Playgroud)
xcrun simctl spawn booted log config --mode "level:debug" --subsystem com.mycompany.myapp --category network
Run Code Online (Sandbox Code Playgroud)
再次获取此 sybsystem 的状态,并将打印类别:
Mode for 'com.mycompany.myapp(network)' DEBUG PERSIST_DEFAULT
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
379 次 |
| 最近记录: |