来自C代码的NSLog样式调试消息

Ant*_*ake 13 c iphone objective-c ios

我在静态库中有一些C代码,我正在编译成一个iPhone应用程序.我想将一些调试消息打印到控制台; 是否有像我应该使用的NSLog?我猜测NSLog只适用于程序的Objective-C部分.

编辑:fprintf(stdout,fmt ...)和fprintf(stderr,fmt ...)也不起作用..任何想法为什么他们不这样做?他们应该工作吗?

Mic*_*ann 10

你可以随时做经典:

fprintf(stderr, "hi, this is a log line: %s", aStringVariable);
Run Code Online (Sandbox Code Playgroud)

  • 是的,尝试了这两种方法,但都没有效果。我可以在同一代码中设置断点,并且Xcode调试器在这些点处中断。.因此,代码肯定在设备上运行。 (2认同)

Tom*_*mer 5

如果您混合使用Objective-C代码,则可以为NSLog做一个包装:

log.h

void debug(const char *message, ...) __attribute__((format(printf, 1, 2)));
Run Code Online (Sandbox Code Playgroud)

日志

#import <Foundation/Foundation.h>
#import "log.h"

void debug(const char *message,...)
{
    va_list args;
    va_start(args, message);
    NSLog(@"%@",[[NSString alloc] initWithFormat:[NSString stringWithUTF8String:message] arguments:args]);
    va_end(args);
}
Run Code Online (Sandbox Code Playgroud)

然后,在您的C文件中:

#include "log.h"
...
debug("hello world! variable: %d", num);
Run Code Online (Sandbox Code Playgroud)