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?
2
Answers
When you set OS_ACTIVITY_MODE to
disable
, you disable all log outputs for processes that have access to this environment variable, for example the simulators.When you set the value to either
info
ordebug
it enables the corresponding logging mode unless it is disabled in the logging preferences for certain log categories.So in order to enable only a certain log category, you have to enable OS_ACTIVITY_MODE, that is, set it either to
info
ordebug
or leave it at the default, and then disable logging for all other categories.For more information how to do this see the man page for log, type
man log
in the console.Get the current log preferences for simulators:
In the console type:
xcrun simctl spawn booted log config --status
That basically means: apply
log config -status
to all booted simulators.This may print the following to your console:
Get the status for a certain category and subsystem
This may print the below to the console:
Get Help
For help, the tools
xcrun simctl
andlog
have nifty man pages and help pages.For example:
type
log config --help
into the console, it prints:Set the log level for a specific subsystem and category
Get the status again for this sybsystem and category will print:
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 inlibdispatch_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.