3

This looked promising but doesn't seem like a duplicate question as it addresses issues in Swift.


I am finalising an existing Objective C project but NSLog disappeared when I updated to Xcode 9. So I'm looking for some setting in Xcode that lets me continue using NSLog to fine tune the project.

Basic debugging using logging for Swift and Objective-C apps appears to have changed in Xcode 9 as NSLog messages no longer appear in the Debug area. The window where DEBUG=1 is set no longer looks like Figure 2 The DEBUG preprocessor macro setting in an Xcode project.

As an example, I want to display this string message in the Debug area using Xcode9.

    NSString *outputData = @"This should show in Debug area";
    NSLog( @"text: %@", outputData );

I studied the latest documentation here or here but so far that has not helped. It may also be worth noting that previously the Debug area would open automatically when I ran the project. Since installing Xcode9 it no longer does that.

Hopefully the extra information provided in the edits below will suggest to someone where I have yet to look. Thanks.


EDIT 1

In the Console area - i.e. the bottom right part of the Debug area - I had All Output selected. So I tried using Debugger output and Target output but there was still no log.

I added these statements in the Prefix.pch file

#ifndef DEBUG
#define NSLog(...) /* suppress NSLog when in release mode 
#endif

When I ran the code the following appeared in the Issue Navigator.

    Unused variable 'outputData'

EDIT 2

There seems to be a different place to set DEBUG=1 in Xcode9 (see below).

I inserted a DEBUG macro into the .pch file using the examples suggested in several SO posts here, here, here (all quite old) and even here (making sure to change MyLog back to NSLog). And in each case I was able to report the same issue in the Issue navigator but never in the Debug console. A similar problem (with Xcode5) was solved here by copying over the files into a new project but I want to avoid this.

My app which was almost finished has so far not had to deal with any of the complex issues that unified logging seeks to address. But I watched the 2016 WWDC video on unified logging and read through its slide show files searching for an example of how to use the appropriate API to do something as basic - print NSLog live to the console area - the way I did before installing Xcode9. This may be the wrong approach. But I can’t think of a better way to proceed.


EDIT 3

It's worth noting that when I created a new Objective C project using Xcode9 and ran the above code, the string message appeared in the Console area.

In other words, using Xcode9 the new logging API does work with NSLog, but only for a new project and not for one created using an earlier version of Xcode.


Enable DEBUG preprocessor macro

The DEBUG preprocessor macro setting in an Xcode project.

Greg
  • 1,750
  • 2
  • 29
  • 56
  • i can see logs like before in XCode 9. Have you tried with a sample prj? – Ellen Sep 25 '17 at 13:37
  • 1
    Possible duplicate of [Swift: print() vs println() vs NSLog()](https://stackoverflow.com/questions/25951195/swift-print-vs-println-vs-nslog). Not exactly a duplicate. But the accepted answer covers all that you are asking and more! – mfaani Sep 25 '17 at 17:00
  • I'm not sure exactly what you mean by sample prj. I'm trying to work with an existing project that used to produce copious logs in the Debug area. These have now completely vanished even though this project continues to build and run whether I use the simulator or the device. – Greg Sep 25 '17 at 17:13
  • 1
    In the Xcode 9 console, there's a little picker to choose "All output", "Debugger output" and "Target output". Which do you have selected? Also, you say that where you set "DEBUG=1" doesn't look like that technical note, but it does for me in Xcode 9. Edit your question to include screen snapshot. (BTW, that's only relevant if you're using "#if DEBUG` pattern.) Bottom line, I cannot reproduce the behavior you describe, so I'd suggest you identify [the steps necessary for us to create blank project and reproduce the problem you describe](https://stackoverflow.com/help/mcve). – Rob Sep 25 '17 at 19:25
  • Rob, see my latest edit. – Greg Sep 25 '17 at 22:50
  • @Rob, I finally solved the problem. https://stackoverflow.com/a/46501096/2348597. Thanks – Greg Sep 30 '17 at 08:49

3 Answers3

1

Rather than writing your debug statement into code, you might try dropping in a breakpoint, right clicking on it, and selecting "Edit Breakpoint". You should see this menu:

enter image description here

From there, you'll click "Edit Breakpoint" and you're presented with this:

enter image description here

From there, you can click "Action" and you'll see this menu:

enter image description here

You can enter print messages (ex: myMethodCalled) or variable values (myIntValue = @myIntValue@), or you can type debugger commands to execute at that spot in the code (ex: po dump(myArray)).

The downside of this approach is the breakpoints aren't always "sticky" to the line of code you originally drop it in on.

Adrian
  • 16,233
  • 18
  • 112
  • 180
  • Adrian, didn’t know I could do that (+1). I’m now using it to make a ‘pop’ whenever I hit a breakpoint. This gives more flexibility than my current workaround (i.e. painting a UIView on the iPhone screen). It could prove useful doing final edits needed to move my app onto the app store (currently stuck in TestFlight). Thanks – Greg Sep 28 '17 at 05:34
1

Try this. Navigate to:

Product -> Scheme -> Edit Scheme -> Run -> Arguments -> Environment Variables

in Xcode.

Add OS_ACTIVITY_MODE and set the value to disable or leave it empty.

Make sure it is checked!

Hope this helps

Balázs Vincze
  • 3,550
  • 5
  • 29
  • 60
  • Balázs, you are close! OS_ACTIVITY_MODE was disabled, I left it empty (with OS_ACTIVITY_MODE checked) and suddenly in the Console area there is a log but no Debug log e.g. [MC] Lazy loading NSBundle MobileCoreServices.framework, [MC] Loaded MobileCoreServices.framework, [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/gs/Library/Developer/CoreSimulator/Devices/08EEE25D-4D90-4242-9DA6-932CF03AC9CA/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles – Greg Sep 28 '17 at 08:37
  • Balázs, I've solved it, due in part to the lead you gave me. +1 – Greg Sep 30 '17 at 08:44
0

A Debug log was enabled in the Console area of Xcode9 after replacing these definitions in the preCompile header

    #ifndef DEBUG
    #define NSLog(...) /* suppress NSLog when in release mode */ 
    #endif

with the following

    #ifdef __DEBUG__
    #define NSLog(...) /* suppress NSLog when in release mode */
    #endif

Environment Variables for Arguments were set according to the answer above with an additional setting for Options. Someone more familiar with the unified logging API might explain the particular options but I'm satisfied I have a working solution.

Arguments

enter image description here

Options

enter image description here

Greg
  • 1,750
  • 2
  • 29
  • 56