为什么调用setenv("OS_ACTIVITY_DT_MODE","disable",1)不会影响日志记录? [英] Why calling setenv("OS_ACTIVITY_DT_MODE", "disable", 1) does not affect logging?

查看:71
本文介绍了为什么调用setenv("OS_ACTIVITY_DT_MODE","disable",1)不会影响日志记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过在运行方案中将 OS_ACTIVITY_MODE environmnet变量设置为 disable ,可以在Swift中隐藏由第三方库生成的冗长日志,如下图所示./p>

这会使您的应用程序的所有 NSLog 输出静音.我只想对某些电话禁用它.我试图像这样设置环境变量:

  setenv("OS_ACTIVITY_MODE","disable",1) 

像这样

  putenv(UnsafeMutablePointer< Int8>(变异:(作为NSString的"OS_ACTIVITY_MODE = disable").utf8String)) 

通过调用 print(ProcessInfo.processInfo.environment)进行验证可以更改环境,但是与在XCode运行方案中指定环境不同,日志不受影响.

为什么不起作用以及如何解决?

解决方案

因为在程序启动时环境变量通常只能读取一次.

在libdispatch 442.1.4中, OS_ACTIVITY_MODE 变量在 libdispatch_init ,在程序启动时仅被调用一次.有趣的是,我在库的更高版本中找不到此代码.也许它已经被转移到其他地方了.但这并不重要;相同的原则适用.设置环境变量的值通常不会影响已经初始化的代码,只能用于影响子进程.

关于解决方案,我没有一个简单的解决方案.就个人而言,我会尝试找到某种方法来禁用正在讨论的特定库中的日志记录或过滤掉消息:在最坏的情况下,我会在消息中放置几个​​我自己的 NSLog 调用在调用该库之前和之后由我控制,并告诉过滤器忽略两者之间的所有内容.

Overly verbose logs produced by 3rd party libraries can be hidden in Swift by setting OS_ACTIVITY_MODE environmnet variable to disable in your run scheme - as shown on the image below.

That silences all NSLog output from your app. I want to disable it only for certain calls. I tried to set the environment variable like this:

setenv("OS_ACTIVITY_MODE", "disable", 1)

And like this

putenv(UnsafeMutablePointer<Int8>(mutating: ("OS_ACTIVITY_MODE=disable" as NSString).utf8String))

The environment is changed as verified by calling print(ProcessInfo.processInfo.environment) but there unlike specifying it in XCode run scheme, the logs are not affected.

Why doesn't it work and how to fix it?

解决方案

Because environment variables are usually read only once, when the program starts.

In libdispatch 442.1.4, the OS_ACTIVITY_MODE variable is read in _voucher_init to determine a value of an internal variable; _voucher_init is in turn called in libdispatch_init, which is called only once at program startup. Interestingly, I cannot find this code in later versions of the library; perhaps it has been moved somewhere else. But it doesn’t really matter; the same principle applies. Setting the value of an environment variable will usually have no effect on already-initialised code, and can only be used to influence child processes.

As for solutions, I don’t have an easy one. Personally, I would try finding some method to disable logging in the specific library in question, or to filter the messages out: in the worst case, I’d put a couple of my own NSLog calls with messages controlled by me before and after calls to the library, and tell a filter to ignore everything between the two.

这篇关于为什么调用setenv("OS_ACTIVITY_DT_MODE","disable",1)不会影响日志记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆