Snippet #59
Language: Objective-C, Author:
License: Public domain
Leopard sends all NSlog() to syslog - what a pain in the debugger! This is similar to snippet ZNLog but my implementation is invoked with a preprocessor macro and the log line has a time of day stamp and file path reduced to just the file name.
I presume this should work in all versions of Mac OS X but I've only used it with Leopard. You need no declaration to call the function unless your use is in the same file it is and appears earlier.
No copyright; do what you want with it.
// Macro to replace NSLog calls so lines go to Leopard's XCode debug console. Leopard's NSLog always copies to syslog! #define NSLOG(...) (void)ZZMyLog(__FILE__, __LINE__, __PRETTY_FUNCTION__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String]) // Place the following code about anywhere, even in main.c #import <sys/time.h> // Supports NSLOG macro to avoid sending Leopard lines to syslog. This global function can be called from anywhere. void ZZMyLog(char *fileStr, int lineInt, char *funcStr, char *formatedStr) { // fileStr: "/Users/dave/Desktop/CID/CIDHost.m" // funcStr: "-[CIDHost awakeFromNib]:" // formatedStr: post formated string from MYLOG macro // strip file name path char * modname = strrchr(fileStr, '/'); if (modname==NULL) modname = fileStr; else modname++; struct timeval theTime; struct timezone theZone; gettimeofday(&theTime, &theZone); time_t seconds = theTime.tv_sec; // seconds since 1/1/1970 seconds -= theZone.tz_minuteswest*60; // time zone adjustment if (theZone.tz_dsttime) seconds += 60*60; // add daylight savings time seconds %= (60*60*24); // seconds in today int hours = seconds/(60*60); // hours in day int minutes = seconds/60 - hours*60; // minute in hour seconds %= 60; // seconds in minute int millis = theTime.tv_usec; millis /= 1000; // milliseconds printf("%02d:%02d:%02d.%03d %s:%03i %s: %s\n", hours, minutes, (int)seconds, millis, modname, lineInt, funcStr, formatedStr); }
Compatible with:
- Mac OS X 10.5 Intel
Comments
Comment feed
