在调试模式下启用和禁用NSLog

时间:2022-09-06 21:15:20

I want to enable NSLog when I am in debug and disable it otherwise. A very simple thing is:

我希望在调试时启用NSLog,否则禁用它。一个非常简单的事情是:

#ifdef DEBUG
NSLog(@"My log");
#endif

But all this #ifdef and #endif is borring... :( So I try other thing: (.pch is good place to put it)

但是所有这些#ifdef和#endif都是borring…所以我试着做另一件事。pch是放置它的好地方)

#ifdef DEBUG
#   define NSLog(text) NSLog(text);
#else 
#   define NSLog(text) 
#endif

This work very fine (isn't recursive). But the problem is that NSLog have infinite arguments.

这个工作非常好(不是递归的)。但问题是NSLog有无穷个参数。

void NSLog(NSString *format, ...)

How I solve this to work in preprocessor mode?

如何在预处理器模式下解决这个问题?

-- Edit --

——编辑

This code make your NSLog better:

此代码使您的NSLog更好:

#ifdef DEBUG
    #define NSLog(FORMAT, ...) fprintf(stderr,"%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
    #define NSLog(...)
#endif

5 个解决方案

#1


102  

This should do the trick:

这应该可以做到:

 #ifdef DEBUG
 #   define NSLog(...) NSLog(__VA_ARGS__)
 #else 
 #   define NSLog(...) (void)0
 #endif

#2


19  

This is a bit shorter and also disables NSLog when using a device. If you're writing a game, NSLogs sent often can reduce your FPS from 60 to 20.

这更短一点,并且在使用设备时禁用NSLog。如果你正在编写一个游戏,NSLogs经常会将你的FPS从60减少到20。

#if !defined(DEBUG) || !(TARGET_IPHONE_SIMULATOR)
    #define NSLog(...)
#endif

#3


8  

All the above answers are correct. I can suggest you to do it in a following way also. Suppose i have a if statement with no brackets

以上答案都是正确的。我也可以建议你用以下的方法做这件事。假设我有一个没有括号的if语句

if(x==5)
NSLog("x is 5");

What will happen if it will replace NSLog with no statement. So we can simply replace it with an empty loop.

如果它将替换NSLog而没有声明,将会发生什么。我们可以用一个空循环替换它。

#ifdef DEBUG
#define NSLog(...) NSLog(__VA_ARGS__)
#else
#define NSLog(...) do {} while (0)
#endif

This statement will run an empty loop once. This will safely remove your NSLog from all of your live code.

此语句将运行一次空循环。这将安全地从所有活动代码中删除NSLog。

#4


1  

#ifndef Debug
    #define Debug 1
#endif

#if Debug
#   define DebugLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#   define DebugLog(...)
#endif

Set Debug to 1 for enabling the log and 0 for disabling it.

将Debug设置为1用于启用日志,0用于禁用日志。

#5


1  

Here is a nice trick... add to any .m

这是一个很好的技巧……添加任何打烊

#define EXTRANSLog if([[NSUserDefaults standardUserDefaults] boolForKey:@"SomeFancyKey"] == YES) NSLog 

Replace any

替换任何

NSLog(@"????");

with

EXTRANSLog(@"????");

In this example, I created a NSUser key and set the BOOL to YES,, use some form of switch etc to change the key to NO or remove altogether if you don't want to view the EXTRANSLog's via console debugger.

在本例中,我创建了一个NSUser键并将BOOL设置为YES,如果您不想通过控制台调试器查看EXTRANSLog的键,可以使用某种形式的switch etc将键更改为NO或完全删除。

I use this when troubleshooting and don't want all the excessive logs to appear. Only when SomeFancyKey == YES.

在进行故障排除时,我使用此方法,不希望出现所有过量的日志。只有当某事= YES。

This is the same as

这是一样的。

#define NSLog if(1) NSLog

where 1 is YES show NSLog, and 0 is NO.

其中1是NSLog, 0是NO。

#1


102  

This should do the trick:

这应该可以做到:

 #ifdef DEBUG
 #   define NSLog(...) NSLog(__VA_ARGS__)
 #else 
 #   define NSLog(...) (void)0
 #endif

#2


19  

This is a bit shorter and also disables NSLog when using a device. If you're writing a game, NSLogs sent often can reduce your FPS from 60 to 20.

这更短一点,并且在使用设备时禁用NSLog。如果你正在编写一个游戏,NSLogs经常会将你的FPS从60减少到20。

#if !defined(DEBUG) || !(TARGET_IPHONE_SIMULATOR)
    #define NSLog(...)
#endif

#3


8  

All the above answers are correct. I can suggest you to do it in a following way also. Suppose i have a if statement with no brackets

以上答案都是正确的。我也可以建议你用以下的方法做这件事。假设我有一个没有括号的if语句

if(x==5)
NSLog("x is 5");

What will happen if it will replace NSLog with no statement. So we can simply replace it with an empty loop.

如果它将替换NSLog而没有声明,将会发生什么。我们可以用一个空循环替换它。

#ifdef DEBUG
#define NSLog(...) NSLog(__VA_ARGS__)
#else
#define NSLog(...) do {} while (0)
#endif

This statement will run an empty loop once. This will safely remove your NSLog from all of your live code.

此语句将运行一次空循环。这将安全地从所有活动代码中删除NSLog。

#4


1  

#ifndef Debug
    #define Debug 1
#endif

#if Debug
#   define DebugLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#   define DebugLog(...)
#endif

Set Debug to 1 for enabling the log and 0 for disabling it.

将Debug设置为1用于启用日志,0用于禁用日志。

#5


1  

Here is a nice trick... add to any .m

这是一个很好的技巧……添加任何打烊

#define EXTRANSLog if([[NSUserDefaults standardUserDefaults] boolForKey:@"SomeFancyKey"] == YES) NSLog 

Replace any

替换任何

NSLog(@"????");

with

EXTRANSLog(@"????");

In this example, I created a NSUser key and set the BOOL to YES,, use some form of switch etc to change the key to NO or remove altogether if you don't want to view the EXTRANSLog's via console debugger.

在本例中,我创建了一个NSUser键并将BOOL设置为YES,如果您不想通过控制台调试器查看EXTRANSLog的键,可以使用某种形式的switch etc将键更改为NO或完全删除。

I use this when troubleshooting and don't want all the excessive logs to appear. Only when SomeFancyKey == YES.

在进行故障排除时,我使用此方法,不希望出现所有过量的日志。只有当某事= YES。

This is the same as

这是一样的。

#define NSLog if(1) NSLog

where 1 is YES show NSLog, and 0 is NO.

其中1是NSLog, 0是NO。