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?

2
  • You can just give my answer a try ;-) Commented Jul 18, 2011 at 12:19
  • Has nothing specific to do with iPad ios or iPhone. Only Objective-C and maybe Xcode. Commented Jun 9, 2013 at 13:54

15 Answers 15

80

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(...) 
Sign up to request clarification or add additional context in comments.

4 Comments

+100 This is the best answer. It should be accepted as the correct answer for this question.
As long as nothing else is after the log statement on the same line — rare situation but it's perfectly valid and possible!
This generates warnings.
It generates warning every where the NSlog is being used.
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.

4 Comments

+1 for dealing with the commented NSLogs; great catch that many would miss
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.
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...
Look at the Saqib Saud answer below. It's a way better way to do it.
12
#define NSLog if(1) NSLog

if you dont want log set 1 as 0.

Comments

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

1 Comment

You're right. The best answer is below: #define NSLog(...) do { } while (0) for the release builds
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.

2 Comments

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.
The same way. What makes you think it's any different?
4
#ifdef RELEASE
   #define NSLog(...) do { } while (0)
#endif

is the best way i found so far.

Comments

3

#define NSLog(...)

add this line into your .pch file

if you want log than comment it

Comments

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 ;-)

1 Comment

'Beauty' and 'regular expression' don't belong in the same sentence ;)
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.

Comments

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

Comments

1

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

Comments

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 //

Comments

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

Comments

0

try this also:

    #define NSLog(@"YOUR MESSAGE HERE!") do { } while (0)

1 Comment

How is this different from the same answer that @seanSLee gave three years ago?
-1

Add the following line to your .pch file

#define NSLog

for enable comment it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.