Created
April 7, 2012 10:20
-
-
Save wannabegeek/2327214 to your computer and use it in GitHub Desktop.
Controlling Debug Logging Levels and Logging to File in Cocoa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TFLog.h | |
// | |
// Created by Tom Fewster on 08/06/2010. | |
// | |
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR | |
# import <UIKit/UIKit.h> | |
#else | |
# import <Cocoa/Cocoa.h> | |
#endif | |
#ifdef DEBUG | |
# define DebugLog(format, ...) NSLog(@"<Debug>: " format @" [" __FILE__ @":%i]", ##__VA_ARGS__, __LINE__) | |
# ifdef TRACE_LOG | |
# define DebugTrace(format, ...) NSLog(@"<Trace>: " format @" [" __FILE__ @":%i]", ##__VA_ARGS__, __LINE__) | |
# else | |
# define DebugTrace(format, ...) | |
# endif | |
# define InfoLog(format, ...) NSLog(@"<Info> " format @" [" __FILE__ @":%i]", ##__VA_ARGS__, __LINE__) | |
# define WarningLog(format, ...) NSLog(@"<Warning> " format @" [" __FILE__ @":%i]", ##__VA_ARGS__, __LINE__) | |
# define ErrorLog(format, ...) NSLog(@"<Error> " format @" [" __FILE__ @":%i]", ##__VA_ARGS__, __LINE__) | |
#else | |
# define DebugLog(format, ...) | |
# define DebugTrace(format, ...) | |
# define InfoLog(format, ...) NSLog(@"<Info>: " format, ##__VA_ARGS__) | |
# define WarningLog(format, ...) NSLog(@"<Warning>: " format, ##__VA_ARGS__) | |
# define ErrorLog(format, ...) NSLog(@"<Error>: " format, ##__VA_ARGS__) | |
#endif | |
void initialiseLogger(void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TFLog.m | |
// | |
// Created by Tom Fewster on 06/04/2012. | |
// | |
#import "TFLog.h" | |
#include <assert.h> | |
#include <stdbool.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <sys/sysctl.h> | |
void initialiseLogger(void) { | |
#ifdef DEBUG | |
int junk; | |
int mib[4]; | |
struct kinfo_proc info; | |
size_t size; | |
// Initialize the flags so that, if sysctl fails for some bizarre | |
// reason, we get a predictable result. | |
info.kp_proc.p_flag = 0; | |
// Initialize mib, which tells sysctl the info we want, in this case | |
// we're looking for information about a specific process ID. | |
mib[0] = CTL_KERN; | |
mib[1] = KERN_PROC; | |
mib[2] = KERN_PROC_PID; | |
mib[3] = getpid(); | |
// Call sysctl. | |
size = sizeof(info); | |
junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0); | |
assert(junk == 0); | |
// We're being debugged if the P_TRACED flag is set. | |
// so, if we are being debugged the out put will go to Xcode's console | |
// other wise redirect it to a file in our documents directory | |
if (!((info.kp_proc.p_flag & P_TRACED) != 0)) { | |
// find our applications documents directory | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *documentsDirectory = [paths objectAtIndex:0]; | |
// create a unique files name keyed off the date | |
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; | |
[dateFormatter setDateFormat:@"yyyyMMddHHmmss"]; | |
NSString *fileName = [NSString stringWithFormat:@"%@.log", [dateFormatter stringFromDate:[NSDate date]]]; | |
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName]; | |
// redirect stderr to our new file | |
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr); | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment