使用NSLog
进行常规日志记录非常枯燥,因为它需要将所有结构转换为NSString
,并为每个参数使用正确的指定符
NSLog(@"Frame = %@", NSStringFromCGRect(view.frame));
NSLog(@"π = %f", 3.14f);
ATLog
宏对所有类型使用一个通用的指定符%@
,并自动将所有参数转换为NSString
对象
ATLog(@"Frame = %@", view.frame); // No need for NSStringFromCGRect
ATLog(@"π = %@", 3.14f); // One '%@' specifier for all argument types
ATConcatLog
宏使用参数拼接而不是使用指定符替换(如在动态类型语言中)
ATConcatLog(@"π=", 3.14f, @"; Σ=", 13); // π=3.140000; Σ=13
自动转换的类型的完整列表请查看测试。以下是一些示例
ATConcatLog(@"\n RootViewController: ", self.window.rootViewController,
@"\n Frame: ", self.window.frame,
@"\n Center: ", self.window.center,
@"\n Transform: ", self.window.transform,
@"\n Alignment rect insects: ", self.window.alignmentRectInsets,
@"\n Is keyWindow: ", self.window.keyWindow);
RootViewController: (nil)
Frame: {{0, 0}, {320, 568}}
Center: {160, 284}
Transform: [1, 0, 0, 1, 0, 0]
Alignment rect insects: {0, 0, 0, 0}
Is keyWindow: YES
ATLog(@"%@", self.window.clearsContextBeforeDrawing); // YES
ATLog(@"%@", (bool)true); // true
ATLog(@"%@", NULL); // (NULL)
另一个易于使用的宏。每个参数都将在单独的一行中记录
ATLogEach(@"Window properties:",
self.window.frame,
self.window.center,
self.window.keyWindow);
0) Window properties:
1) {{0, 0}, {320, 568}}
2) {160, 284}
3) YES
默认情况下,ATLog
将输出重定向到宏ATPrettyLog
,它产出的输出比NSLog
更优美、更有信息量
NSLog(@"NSLog produces not very useful output.");
ATPrettyLog(@"ATPrettyLog prints method name and line number.");
ATLog(@"ATLog uses ATPrettyLog by default.")
2013-08-22 23:14:16.258 Example[66227:c07] NSLog produces not very useful output.
23:14:16.261 -[AppDelegate logExample] [Line 20] ATPrettyLog prints method name and line number.
23:14:16.261 -[AppDelegate logExample] [Line 21] ATLog uses ATPrettyLog by default.
您可以通过在导入ATLog.h
文件之前定义一个ATLog_OUTPUT
宏来更改此默认行为。以下是为使用NSLog
或CocoaLumberjack而不是ATPrettyLog的示例
#define ATLog_OUTPUT(fmt, args...) NSLog(fmt,args)
#import "ATLog.h"
ATLog(@"I use NSLog now.");
#define ATLog_OUTPUT(fmt, args...) DDLogVerbose(fmt,args)
#import "ATLog.h"
ATLog(@"I use CocoaLumberjack now.");
Yan Rabovik (@rabovik 在twitter)
MIT许可证。