// |
02 | // TFLog.h |
03 | // |
04 | // Created by Tom Fewster on 08/06/2010. |
05 | // |
06 |
07 | #if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR |
08 | # import <UIKit/UIKit.h> |
09 | #else |
10 | # import <Cocoa/Cocoa.h> |
11 | #endif |
12 |
13 | #ifdef DEBUG |
14 | # define DebugLog(format, ...) NSLog(@"<Debug>: " format @" [" __FILE__ @":%i]", ##__VA_ARGS__, __LINE__) |
15 | # ifdef TRACE_LOG |
16 | # define DebugTrace(format, ...) NSLog(@"<Trace>: " format @" [" __FILE__ @":%i]", ##__VA_ARGS__, __LINE__) |
17 | # else |
18 | # define DebugTrace(format, ...) |
19 | # endif |
20 | # define InfoLog(format, ...) NSLog(@"<Info> " format @" [" __FILE__ @":%i]", ##__VA_ARGS__, __LINE__) |
21 | # define WarningLog(format, ...) NSLog(@"<Warning> " format @" [" __FILE__ @":%i]", ##__VA_ARGS__, __LINE__) |
22 | # define ErrorLog(format, ...) NSLog(@"<Error> " format @" [" __FILE__ @":%i]", ##__VA_ARGS__, __LINE__) |
23 | #else |
24 | # define DebugLog(format, ...) |
25 | # define DebugTrace(format, ...) |
26 | # define InfoLog(format, ...) NSLog(@"<Info>: " format, ##__VA_ARGS__) |
27 | # define WarningLog(format, ...) NSLog(@"<Warning>: " format, ##__VA_ARGS__) |
28 | # define ErrorLog(format, ...) NSLog(@"<Error>: " format, ##__VA_ARGS__) |
29 | #endif |
30 |
31 | void initialiseLogger(void); |
01 | // |
02 | // TFLog.m |
03 | // |
04 | // Created by Tom Fewster on 06/04/2012. |
05 | // |
06 |
07 | #import "TFLog.h" |
08 | #include <assert.h> |
09 | #include <stdbool.h> |
10 | #include <sys/types.h> |
11 | #include <unistd.h> |
12 | #include <sys/sysctl.h> |
13 |
14 | void initialiseLogger(void) { |
15 | #ifdef DEBUG |
16 | int junk; |
17 | int mib[4]; |
18 | struct kinfo_proc info; |
19 | size_t size; |
20 |
21 | // Initialize the flags so that, if sysctl fails for some bizarre |
22 | // reason, we get a predictable result. |
23 |
24 | info.kp_proc.p_flag = 0; |
25 |
26 | // Initialize mib, which tells sysctl the info we want, in this case |
27 | // we're looking for information about a specific process ID. |
28 |
29 | mib[0] = CTL_KERN; |
30 | mib[1] = KERN_PROC; |
31 | mib[2] = KERN_PROC_PID; |
32 | mib[3] = getpid(); |
33 |
34 | // Call sysctl. |
35 |
36 | size = sizeof(info); |
37 | junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0); |
38 | assert(junk == 0); |
39 |
40 | // We're being debugged if the P_TRACED flag is set. |
41 | // so, if we are being debugged the out put will go to Xcode's console |
42 | // other wise redirect it to a file in our documents directory |
43 | if (!((info.kp_proc.p_flag & P_TRACED) != 0)) { |
44 | // find our applications documents directory |
45 | NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
46 | NSString *documentsDirectory = [paths objectAtIndex:0]; |
47 |
48 | // create a unique files name keyed off the date |
49 | NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; |
50 | [dateFormatter setDateFormat:@"yyyyMMddHHmmss"]; |
51 | NSString *fileName = [NSString stringWithFormat:@"%@.log", [dateFormatter stringFromDate:[NSDate date]]]; |
52 | NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName]; |
53 |
54 | // redirect stderr to our new file |
55 | freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr); |
56 | } |
57 | #endif |
58 | } |