19

I'd like to do the following in Xcode:

Find all NSLog commands without comments, and replace it with //NSLog...

In other words, I want to comment all NSLog calls. Is this possible? Is there an easy way to do this?

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
CodeGuy
  • 28,427
  • 76
  • 200
  • 317

15 Answers15

81

wait there is far more simple method. Just add the following lines when you dont need NSLOG to your constants file

#define NSLog                       //

and comment it when you need it.

EDIT: In latest Xcode, you get error on above statement. So I figured out the best way is

#define NSLog(...) 
Saqib Saud
  • 2,799
  • 2
  • 27
  • 41
17

Update:

The answer bellow is actually much better. See here.

Initial answer:

There is a little hack that you could do. Search for all NSLog and replace them with //NSLog and than do another search for ////NSLog and replace them with //NSLog.

Community
  • 1
  • 1
Mihai Fratu
  • 7,579
  • 2
  • 37
  • 63
  • +1 for dealing with the commented NSLogs; great catch that many would miss –  Jul 18 '11 at 11:58
  • wait, but what if I wanted to iterate through the instances of NSLog without comments? is that possible. that's sort of what I wanted to do. – CodeGuy Jul 18 '11 at 13:45
  • You could try searching for " NSLog" without the quotes BUT with the space in front of it... It won't work if your NSLog was at the begging of the row without any indent but I think there is a small possibility for that... – Mihai Fratu Jul 18 '11 at 14:14
  • Look at the Saqib Saud answer below. It's a way better way to do it. – Tomasz Feb 11 '13 at 03:22
12
#define NSLog if(1) NSLog

if you dont want log set 1 as 0.

Prateek Prem
  • 1,544
  • 11
  • 14
11

I have to do a separate answer because I cant comment yet, but one thing you really need to be careful about when you do a find and replace is something like this:

if(BOOL)
NSLog(@"blah blah");

[self doSomething];

If you comment out the nslog, the conditional gets associated with the method below it, correct me if I'm wrong

Marcus Schwab
  • 348
  • 3
  • 10
7

The answers you have are correct for your question. But. Your real question is how to turn of NSLogs in certain conditions. i.e. you want them to show for Debug builds, and not for Release builds. In which case try defining and using the DLog() macro as described on Cocoa Is My Girlfriend. If you're using Xcode4 it's even easier because the Debug and Release builds define and undefine DEBUG so you don't have to do that.

It's a lot easier than commenting and uncommenting lines of code.

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • This is by far the best approach. Define your own wrapper that you then redefine in one place for release builds. Easy. You can even have different test release and real release builds. Just use Xcode build settings and a unique pair of files to import based on your build setting. – uchuugaka Jun 09 '13 at 13:52
  • The same way. What makes you think it's any different? – Abizern Feb 01 '14 at 18:52
4
#ifdef RELEASE
   #define NSLog(...) do { } while (0)
#endif

is the best way i found so far.

Sean S Lee
  • 1,274
  • 9
  • 24
3

#define NSLog(...)

add this line into your .pch file

if you want log than comment it

2

I would do this

#define EnableNSLog 1

#if EnableNSLog == 0
#define NSLog   //
#elif EnableNSLog == 1
#warning Disable NSLog
#endif

This should generate a warning message to remind me to disable NSLog before final release.

Ed Liss
  • 546
  • 4
  • 16
2

You can do this in a single find and replace operation. You can just do this simple Regular Expression replace. This handles both the commented(//) and non-commented lines. This also works even if the previous commented lines has more than two forward slashes(like ///)instead of rwo. You can refer this link. Do the following steps.

  1. Select Edit > Find > Find and Replace in Workspace
  2. Style => Regular Expression
  3. Type (/)*(NSLog.*) in Find field.
  4. Do the find operation.
  5. Type //\2 in the Replace field.
  6. Do the replace operation.

Enjoy the beauty of regular expressions ;-)

Community
  • 1
  • 1
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
1

How to disable NSLog in Xcode for Production stage

Add #define NSLog in appName-Prefix.pch file in Supporting Files Folder of your project and result file code look like...

// Prefix header for all source files of the 'NSLog' target in the 'NSLog' project
//

#import <Availability.h>

#ifndef __IPHONE_4_0
#warning "This project uses features only available in iOS SDK 4.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
#endif

//Add this to disable NSLog
#define NSLog //
Arun
  • 3,406
  • 4
  • 30
  • 55
1

in the Menu bar : Edit > Find > Find and Replace in Workspace then, display options to use regular expressions. search/replace for "[^/]NSLog"

teriiehina
  • 4,741
  • 3
  • 41
  • 63
1

right click on NSLog statement in xcode and select "find in project" as text.you would be prompted to a new window where you can follow the guidance given by Mihai Fratu.

TNQ

Dinakar
  • 1,198
  • 15
  • 37
0

You can use following Preprocessor Directives, it will not go with release mode. So you don't have to commenting NSLog().

#ifdef DEBUG
    NSLog(@"YOUR MESSAGE HERE!");
#endif
Babul Mirdha
  • 3,816
  • 1
  • 22
  • 25
0

try this also:

    #define NSLog(@"YOUR MESSAGE HERE!") do { } while (0)
Suraj Sukale
  • 1,778
  • 1
  • 12
  • 19
-1

Add the following line to your .pch file

#define NSLog

for enable comment it

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36