-3

I have a builded application that is running on device. I open device's console view in XCode's Organizer window. I assume (for the sake of this question) that NSLog(@"Some string") gets called.

Is there any way, may be an option in device, or application's settings, that would disable this log from appearing in console?

Edit: I'm not interested in replacing NSLog by other solution that can achieve this effect. The purpose of this question is to fully understand NSLog's functionality.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Max Yankov
  • 12,551
  • 12
  • 67
  • 135
  • 2
    Search google for Objective-C DLog. – Wain Jan 20 '14 at 12:06
  • @Wain this would be useful if the purpose of my question was to achieve this effect — hide NSLogs from console. It's not. – Max Yankov Jan 20 '14 at 12:10
  • That's what it does. NSLog itself doesn't offer any options so you need some wrapper around it so you can disable logs on demand or for your release build. – Wain Jan 20 '14 at 12:13
  • I don't want to disable logs. I want to understand how they could, theoretically, be disabled, so I would understand why don't they show up when they should. – Max Yankov Jan 20 '14 at 12:17
  • There is no setting (as far as I know) that disables NSLog. But the logs ultimately are printed to stderr, so closing or redirecting stderr would have such an effect, compare trojanfoe's answer below. – Martin R Jan 20 '14 at 12:37

4 Answers4

1

(Thanks to @MartinR for encouraging me to pull my finger out on this answer and to correctly identify that you cannot just close stdout/stderr, as the next open() will re-use those file descriptors, but to redirect stdout/stderr to the infamous /dev/null).

logControl.h:

#pragma once

extern void stopLogging();
extern void startLogging();

logControl.c:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

static int loggingStopped = 0;
static int oldStdout = -1;
static int oldStderr = -1;

void stopLogging() {
    if (!loggingStopped) {
        oldStdout = dup(1);
        oldStderr = dup(2);

        int devNull = open("/dev/null", O_WRONLY);
        dup2(devNull, 1);
        dup2(devNull, 2);
        close(devNull);

        loggingStopped = 1;
    }
}

void startLogging() {
    if (loggingStopped && oldStdout >= 0 && oldStderr >= 0) {
        dup2(oldStdout, 1);
        close(oldStdout);
        oldStdout = -1;

        dup2(oldStderr, 2);
        close(oldStderr);
        oldStderr = -1;

        loggingStopped = 0;
    }
}

This works at runtime, not compile time, which I believe is what you are asking. Simply call stopLogging() or startLogging() as required.

NOTE: There is no error-checking to speak of, so that could be improved perhaps.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • Better use dup2() to "redirect" stderr to /dev/null. Otherwise the next file opened will reuse these file descriptors. See http://stackoverflow.com/a/12611692/1187415 for an example. – Martin R Jan 20 '14 at 12:31
  • @MartinR Yeah, I think you're right. You'd have to save the old stdout and stderr as well, in case you wanted to re-open it. Doesn't matter - this answer isn't well-liked anyway ;-) – trojanfoe Jan 20 '14 at 12:32
  • The problem might be that it is unclear (and misunderstood by some people) what answer the OP expects. To me yours seems to come closest to a possible answer. – Martin R Jan 20 '14 at 12:34
  • @MartinR OK, I'll make some effort. – trojanfoe Jan 20 '14 at 12:35
0

Add this line given below in your .pch file in Xcode.

   #define NSLog(...)

It will disable all NSLogs.

for more alternatives see the link

Community
  • 1
  • 1
Ankush
  • 2,405
  • 3
  • 22
  • 45
0
#if TARGET_IPHONE_SIMULATOR

//Simulator

#else

// Device
#define NSLog
#endif

Add this in your .pch file this will disable NSLog only for device alone not for simulator.

CoolMonster
  • 2,258
  • 25
  • 50
0

Add below code to .pch file

#ifdef DEBUG
#    define NSLog(...) NSLog(__VA_ARGS__)
#else
#   define NSLog(...) /* */
#endif

And in Build Settings, search "Preprocessor Macros" and remove "DEBUG=1" written in it.

and Thats it, you will not see any logs in your console now.

Funny
  • 566
  • 5
  • 16