Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS debugging real device with 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?

like image 748
theWalker Avatar asked Jan 19 '12 10:01

theWalker


3 Answers

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;
}
like image 174
Sohaib Avatar answered Nov 17 '22 03:11

Sohaib


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");
like image 27
Swindler Avatar answered Nov 17 '22 02:11

Swindler


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

like image 4
Elviss Strazdins Avatar answered Nov 17 '22 02:11

Elviss Strazdins