I am using a precompiled library I don't have the source of and it's printing a lot of debug logs. Is it possible to hide the output from just a specific library?
-
I tried to define the `NSLog` as a macros with additional `__FILE__` argument, unfortunately this appeared to have to effect on the library `NSLog`'s. – A-Live Sep 26 '12 at 21:06
-
Nope. Unless the folks who wrote the code added some sort of runtime test to bypass logging then there's no way to turn it off in a compiled module. (And they SHOULD use their own macro for logging, vs NSLog directly, so that they can turn them on and off with a compile flag.) – Hot Licks Sep 27 '12 at 00:05
1 Answers
No. NSLog
writes its output directly to standard error without checking to see if it should. Once the function has been called, the output will be written.
Perhaps you are using a debug version of the library. Check with those who created it to see if there is, or they would be willing to create, a log-free version.
If you cannot get a version of the library without logging, you could redirect your standard error to /dev/null
, which will cause NSLog
output to be discarded by the system. Note that this requires you to mess with low level file descriptors, and you will discard output from all logging, not just that library. You could minimize the lost output by only redirecting when you call the library functions, but then any function or method that the library calls will also have its logs ignored. You could also redirect it at all times except for when you are logging, which means all other libraries will have their logs discarded. Since there is no way to ensure that helpful logs are not discarded (like exception messages), I would not recommend any redirection for a debug build of your application.
Here is redirecting and unredirecting would work (note that these would work as objective-c methods too):
int discardLogging() {
static int discardedOutputFD = -1;
if(discardedOutputFD == -1) discardedOutputFD = open("/dev/null", O_WRONLY);
int copy = dup(2); // Duplicate the old file descriptor, so it can be restored
dup2(discardedOutputFD, 2); // Redirect
return copy;
}
void restartLogging(int copy) {
dup2(copy, 2); // Redirect back
close(copy); // Not needed anymore
}
The first function redirects standard error to /dev/null
and returns a copy of the old file descriptor. The second redirects it to the copied descriptor and closes the extra copy. Warning: You should check the return values of every function called, since they map to system calls.

- 39,734
- 6
- 101
- 123
-
2Problem is the library in question runs in a background thread, otherwise your discard/restart solution would be perfect. We've asked the developers to remove the debugging output, and they say they will eventually but it's getting in our way now. – moinudin Sep 26 '12 at 23:54