13 Replies
      Latest reply: Mar 13, 2017 9:20 AM by absinth RSS
      barryli Level 1 Level 1 (0 points)

        I'm testing code on OS 10.12.  My NSLog statements work as expected in XCode however in my release builds they do not write to the Console.  My understanding is that NSLog was supposed to be shimmed to os_log.

         

        In order to get around the issue I built a small class which tests the OS version and uses NSLog if < 10.12, otherwise os_log.  Once again the messages show up in XCode but do not appear in the console (on 10.12).

         

        I've tested every level of os_log support and even a custom log.  All work in Xcode, none with the console when running 'live'.  I've tested that the os_log levels are enabled and they seem to be.

         

        The documentation is extremely limited.  There is sample code for the iPhone, but I can't find anything for a Mac.  The iPhone code seems to be doing pretty much what I'm doing.

         

        I must be missing something basic.   Any help would be appreciated.

        • Re: NSLog and os_log only work in Xcode
          KMT Level 8 Level 8 (9,290 points)

          Seems Xcode 8 has gutted purposeful logging for the dev (along with layouts) - might need to find something else to focus on, such as screenshots and localizations, until the real Xcode returns from orbit.

           

          All the pandas are sad over this one...

          • Re: NSLog and os_log only work in Xcode
            eskimo Apple Staff Apple Staff (7,430 points)

            You’re talking about Mac programs running on the Mac, right?  And you’re testing on 10.12?

            If so, I tried this out and it works for me.  Here’s what I did:

            1. In Xcode 8, I created a simple tool project with the code shown below.

            2. I launched Console app.

            3. I selected my Mac in the Devices list in the sidebar.

            4. I entered “qqq” into the search field.

            5. In Terminal, I ran my tool.

            I see both the NSLog and os_log entries show up in Console.

            Please try this out for yourself and let me know if you see the same results.

            Share and Enjoy

            Quinn “The Eskimo!”
            Apple Developer Relations, Developer Technical Support, Core OS/Hardware
            let myEmail = "eskimo" + "1" + "@apple.com"

            @import Foundation;
            
            #include <os/log.h>
            
            int main(int argc, char **argv) {
                #pragma unused(argc)
                #pragma unused(argv)
                int                retVal;
            
                @autoreleasepool {
                    NSLog(@"QQQ from NSLog");
                    os_log(OS_LOG_DEFAULT, "QQQ from os_log");
            
                    retVal = EXIT_SUCCESS;
                }
            
                return retVal;
            }
            
              • Re: NSLog and os_log only work in Xcode
                barryli Level 1 Level 1 (0 points)

                Thanks for the reply.  I've created a project identical to yours and ran it.  The messages do appear in the Console.

                 

                I ran my code again and the messages did not appear.

                 

                I finally figured out that the messages (from both apps) only appear in the Console if it is running when the messages are generated.  The messages do not persist.  I've even attempted creating a custom log and that again only appears if Console is running.  I've tried using os_log_error and that doesn't do any better.

                 

                Surely there must be some methof of getting the messages to persist.

                 

                Thanks!

                  • Re: NSLog and os_log only work in Xcode
                    eskimo Apple Staff Apple Staff (7,430 points)

                    Surely there must be some methof of getting the messages to persist.

                    There’s been a whole bunch of change in this space.  Frankly, I’m not fully up to speed on it myself, but a good place to start would be the WWDC 2016 Session 721 Unified Logging and Activity Tracing, which points you to the log tool, which has a snazzy man page.

                    Share and Enjoy

                    Quinn “The Eskimo!”
                    Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                    let myEmail = "eskimo" + "1" + "@apple.com"

                      • Re: NSLog and os_log only work in Xcode
                        barryli Level 1 Level 1 (0 points)

                        After far too many hours of playing aroung and watching WWDC videos, I came to the same conclusion.  You pretty much have to use the log tool!

                         

                        However, the log tool will show thousands of AppKit type messages which will totally confuse a user.  To handles this I've prefixed all my log messages with:

                         

                             (Appname-App)

                         

                        and I'm be recommending the following log command:

                         

                             log show --predicate 'process == "AppName" and eventMessage beginswith "(AppName-App)"'

                          • Re: NSLog and os_log only work in Xcode
                            eskimo Apple Staff Apple Staff (7,430 points)

                            If you just want to filter out messages, you can do that with a saved search in the new Console app.  I suggested this to another developer and they reported that it worked for them.

                            Share and Enjoy

                            Quinn “The Eskimo!”
                            Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                            let myEmail = "eskimo" + "1" + "@apple.com"

                              • Re: NSLog and os_log only work in Xcode
                                barryli Level 1 Level 1 (0 points)

                                Thanks.  It seems as if 'log' has access to the data always.  I can't get Console to display my application messages unless the Console was open when the messages were generated.  The saved searches do not appear to help this issue.  Now it is entirely possible that I'm missing something :-)

                        • Re: NSLog and os_log only work in Xcode
                          dwarfland Level 1 Level 1 (0 points)

                          Hi.

                           

                          is there an official API for getting the log output from a process? Think, wtiting a custom debugger, or a custom "Terminal" application.

                           

                          We have an IDE that supports debugging Cocoa (and other) apps, and i find that NSLog output isn't accessible via either StdOut or StdErr (both of which our dbegger captures for the debuggee) under Sierra, however, when i run my debuggee app in Terminal.app, it can see NSLogs just fine.

                           

                          One alternative i considered/tried was running "log" as a secondary task and capture its output, but it seems to be way behind and inefficient, certaily not suitable for gertting the log messages "live" and showing them inline while debuggging, within the ocntext of other app output.

                           

                          In the docs for the new logging features, i see only the os_log client APIs (ie for creating logs) documented, but nothing about accessing them (aside from Consiole.app and the `log` executable)

                           

                          What are Xcode and Terminal.app doing to capture NSLog output?

                           

                          thanx,

                          marc

                        • Re: NSLog and os_log only work in Xcode
                          Will Loew-Blosser Level 1 Level 1 (0 points)

                          The sample app for the new logging "Paper Company (Swift)" seems to work just fine.. has just what you would expect from the os_log statements in the code. Likewise the console works as advertised.

                          BUT my app would not work like the sample until a Build setting was added. I have NOT noticed this setting any where in the documentation but it made me happy with logging again.

                          The setting is found in the Targets section of the top level xCode window. On the Build Settings page add in "User-Defined" a new section = ASSETCATALOG_COMPRESSION. Under it add two lines: Debug = lossless and Release = respect-asset-catalog.

                          Look at the sample app for example.

                          Why this works?? I sure don't know !!!! Any ideas? Any place where this is documented or explained?

                          • Re: NSLog and os_log only work in Xcode
                            absinth Level 1 Level 1 (0 points)

                            i can reproduce the logging problems on 10.12

                             

                            1.) NSLog() in production apps only 'works' if Console.app is running. else those are ignored

                            2.) asl_add_log_file() to log to a file is completely broken and no-op if compiling on 10.12. of course you won't even get a warning if compiling for older systems.

                             

                            ASL has been a disaster since day one and OSL made things even worse. logging is one of the worst aspects of macOS.