自己改写了网络上面的nslog宏定义功能,感觉很好用,推荐给大家:
首先,在-Prefix.pch文件中加入如下代码:
#ifdef DEBUG
# define mLOG(fmt, ...) do { \
NSString* file = [[NSString alloc] initWithFormat:@"%s", __FILE__]; \
NSLog((@"< FILE:%@; LINE:%d; FUNC:%s > --: " fmt), [file lastPathComponent], __LINE__, __func__, ##__VA_ARGS__); \
} while(0)
#else
# define mLOG(...)
#endif
如果你没有使用ARC,则为下面代码:
#ifdef DEBUG
# define mLOG(fmt, ...) do { \
NSString* file = [[NSString alloc] initWithFormat:@"%s", __FILE__]; \
NSLog((@"< FILE:%@; LINE:%d; FUNC:%s > --: " fmt), [file lastPathComponent], __LINE__, __func__, ##__VA_ARGS__); \
[file release]; \
} while(0)
#else
# define mLOG(...)
#endif
编译一下,就可以在项目任何位置使用mLOG这个代码了如下:
NSArray *arrtest = [[NSArray alloc] initWithObjects:@"one", @"tow", @"three", @"four", nil];
mLOG(@"%@", arrtest);
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
mLOG(@"We all good friends.");
输出如下:
2014-05-15 12:20:14.063 ATMChaoRenJieKe1971[15222:60b] < FILE:NewsViewController.m; LINE:88; FUNC:-[NewsViewController viewDidLoad] > --: (
one,
tow,
three,
four
)
2014-05-15 12:20:14.067 ATMChaoRenJieKe1971[15222:60b] < FILE:NewsViewController.m; LINE:93; FUNC:-[NewsViewController viewWillAppear:] > --: We all good friends.
可以看出,方便使用还简洁,