简体   繁体   中英

Regular expression for turning NSLogs on and off?

I've got a large Xcode project which has a few dozen NSLogs, some commented out.

The find/replace in Xcode has a regular expression option, so how could I make it comment out all active NSLogs, in a way which another find/replace can turn them back on again. So, the initial code is:

//NSLog(@"one");
NSLog(@"two");

becomes, after regex find/replace:

//NSLog(@"one");
//**NSLog(@"two");

which which turned back on becomes:

//NSLog(@"one");
NSLog(@"two");

There's a much better solution. Just put this in your prefix.pch file:

//disable logging when not in debug
#ifndef DEBUG
#undef NSLog
#define NSLog(args, ...)
#endif

That will disable NSLogs when your app is not in debug mode. That way you don't have to actually remove them from the code, but they'll have no performance impact.

In case you're wondering how it works, it redefines the NSLog() function as blank when the DEBUG macro is not defined. By default DEBUG is defined only for debug projects. You can change it to be toggled on and off in a different way if you prefer by just replacing the first line with #if SOME_OTHER_CONDITION.

A better solution for this would be to use a macro such as DLog which you could control using a debug (or your own compiler flag). Check this link or this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM