隐藏来自预编译库的NSLog输出 [英] Hiding NSLog output coming from precompiled library

查看:75
本文介绍了隐藏来自预编译库的NSLog输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用预编译的库,我没有源代码,它正在打印很多调试日志。是否可以隐藏特定库的输出?

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?

推荐答案

否。 NSLog 将其输出直接写入标准错误,而不检查是否应该。调用该函数后,将写入输出。

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.

如果你不能得到一个版本的库如果没有日志记录,您可以将标准错误重定向到 / dev / null ,这将导致 NSLog 输出被丢弃系统。请注意,这需要您使用低级文件描述符,并且您将丢弃所有日志记录的输出,而不仅仅是该库。您可以通过仅在调用库函数时重定向来最小化丢失的输出,但随后库调用的任何函数或方法也将忽略其日志。您还可以随时重定向它,除非您正在记录,这意味着所有其他库将丢弃其日志。由于无法确保不丢弃有用的日志(例如异常消息),因此我不建议对应用程序的调试版本进行任何重定向。

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.

这是重定向和非重定向都可行(请注意,这些也可以作为objective-c方法):

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
}

第一个函数将标准错误重定向到 / dev / null 并返回旧文件描述符的副本。第二个将它重定向到复制的描述符并关闭额外的副本。警告:您应该检查每个被调用函数的返回值,因为它们映射到系统调用。

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.

这篇关于隐藏来自预编译库的NSLog输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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