如何控制工程中的LOG信息,并写入文件

本文介绍了TFLog,一种专为iOS和Mac开发者设计的日志记录与调试工具,它提供了丰富的日志级别控制,并且在不同环境下能够自动选择合适的日志输出方式。通过定义不同的日志函数,开发者可以轻松地在开发过程中记录关键信息,提高调试效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
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 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值