iOS 使用 printf 调试真实设备

发布于 2024-12-27 19:59:50 字数 137 浏览 2 评论 0原文

在 Xcode Organizer 中,控制台 - 我可以读取 NSLog 输出,但不能读取 printf()。是否可以在真实设备上读取 printf() 结果,就像在模拟器中一样?

In Xcode Organizer, Console - I can read the NSLog output, but not printf(). Is this possible to read printf() result on the real device, the same like in simulator?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薔薇婲 2025-01-03 19:59:50

最简单的解决方案是在项目中全局重载 printf 函数,并将其替换为 NSLog 输出

int printf(const char * __restrict format, ...)
{ 
    va_list args;
    va_start(args,format);    
    NSLogv([NSString stringWithUTF8String:format], args) ;    
    va_end(args);
    return 1;
}

Easiest solution would be to overload printf function globally in your project and replace it with NSLog output

int printf(const char * __restrict format, ...)
{ 
    va_list args;
    va_start(args,format);    
    NSLogv([NSString stringWithUTF8String:format], args) ;    
    va_end(args);
    return 1;
}
厌味 2025-01-03 19:59:50

正如 Nick Lockwood 在上面的评论之一中所说, printf 打印到 stdout,但 NSLog 打印到 stderr。您可以使用 fprintf 打印到 stderr(Xcode 控制台),而不是使用 printf,如下所示:

fprintf(stderr, "This prints to the Xcode debug console");

As Nick Lockwood said in one of the comments above, printf prints to stdout but NSLog prints to stderr. You can use fprintf to print to stderr (the Xcode console) instead of using printf, like this:

fprintf(stderr, "This prints to the Xcode debug console");
萧瑟寒风 2025-01-03 19:59:50

您可以运行以下命令以仅打印到设备的控制台:

syslog(LOG_WARNING, "log string");

您还需要 #include显式声明 syslog 和 LOG_WARNING

You can run the following command to print only to device's console:

syslog(LOG_WARNING, "log string");

You will also need to #include <sys/syslog.h> for syslog and LOG_WARNING to be explicitly declared

锦欢 2025-01-03 19:59:50

Skippy,printf()是c的输出语句而不是Objective C,所以在真实设备中printf()也不起作用。

Skippy,printf() is the output statement for c not for Objective C,SO in real device also printf() is not work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文