Date
Sep. 8th, 2024
 
2024年 8月 6日

Post: iOS : Debug 与 NSLog [Objective-C]

iOS : Debug 与 NSLog [Objective-C]

Published 12:02 Feb 16, 2015.

Created by @ezra. Categorized in #Programming, and tagged as #iOS.

Source format: Markdown

Table of Content

偶然看到别人代码里的 NSLog,忽然想到或许可以写一片简短的博客,能帮到一些朋友也说不定。

大家都知道日志输出在开发测试过程中的重要性,但发布应用后这些无用的打印都在白白的浪费着资源,那么,让日志随着运行环境的不同自动开关就成了一个不错的选择。

#ifdef DEBUG
#define NSLog(fmt, ...) NSLog((@"\n* FileName:%s\n" "* FunctionName:%s\n" "* FunctionPretty:%s\n" "* LineNumber:%d\n* " fmt), [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __FUNCTION__, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define NSLog(...)
#endif

通过这段宏定义,我们为每一个 NSLog 输出都添加了文件名、方法名、行号等信息,并在非 DEBUG 环境自动关闭输出。现在,我们来写段代码测试一下,写法与从前没有任何不同。

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"哈哈哈");
}
@end

看看输出结果:

2016-02-16 16:05:16.881 PHP Manual for Mac[18894:284676] 
* FileName:ViewController.m
* FunctionName:-[ViewController viewDidLoad]
* FunctionPretty:-[ViewController viewDidLoad]
* LineNumber:64
* 哈哈哈
Pinned Message
HOTODOGO
The Founder and CEO of Infeca Technology.
Developer, Designer, Blogger.
Big fan of Apple, Love of colour.
Feel free to contact me.
反曲点科技创始人和首席执行官。
程序猿、设计师、奇怪的博主。
苹果死忠、热爱色彩斑斓的世界。
如有意向请随时 与我联系