15
votes

I have some C code in a static library that I'm compiling into an iPhone app. I'd like to print some debug messages to the console; is there something like NSLog I should use? I'm guessing NSLog only works for the Objective-C parts of the program.

EDIT: fprintf(stdout, fmt...) and fprintf(stderr, fmt...) don't work either.. any idea why they don't? Should they work?

8
Plain old printf seems to work for some code I'm working on.Hot Licks
So I figured out that the output shows up in the console in Xcode, but not the console in organizer.. I guess that was what I was missingAnthony Blake
I've never quite figured out what shows up in Organizer. Seems to randomly select lines.Hot Licks
I'm sure that fprintf output used to show up in the organizer console.. maybe its something to do with Xcode 4.3Anthony Blake
I'm amazed when anything works with Xcode 4.Hot Licks

8 Answers

10
votes

you can always do the classic:

fprintf(stderr, "hi, this is a log line: %s", aStringVariable);
8
votes

You can make a wrapper for NSLog if you mix Objective-C code like this:

log.h

void debug(const char *message, ...) __attribute__((format(printf, 1, 2)));

log.m

#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);
}

and then, in your C file:

#include "log.h"
...
debug("hello world! variable: %d", num);
2
votes

While printf will show up if you're debugging from XCode, it won't show up in the Organizer Console. You can use what NSLog uses: CFLog or syslog.

2
votes
#include <asl.h>
...
asl_log(NULL, NULL, ASL_LEVEL_ERR, "Hi There!");

Note that lower priority levels such as ASL_LEVEL_INFO may not show up in console.

2
votes

Other solution:

#include <CoreFoundation/CoreFoundation.h>
extern "C" void NSLog(CFStringRef format, ...);

#define MyLog(fmt, ...) \
{ \
    NSLog(CFSTR(fmt), ##__VA_ARGS__); \
}

MyLog("val = %d, str = %s", 123, "abc");
0
votes

You should be able to see printf or fprintf statements. Try running some of the code in the library but from the terminal, something should appear if not. A much more complicated (and even silly/stupid/wrong) method that I will guarantee you that will work would be:

sprintf(message,"This is a log line %s",someString);
system("echo %s",message);

If that doesn't work then you probably have something weird in your code.

Note: This will probably only work in the simulator.

0
votes

You probably need to use the Apple System Log facility to get the output into the device console.

Check the functions in usr/asl.h.

asl_open
asl_new
asl_set
asl_log
asl_free
0
votes

While printf will show up if you're debugging from XCode, it won't show up in the Organizer Console. 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