Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSLog on devices in iOS 10 / Xcode 8 seems to truncate? Why?

Why the console output shows incomplete in Xcode 8 / iOS 10?

enter image description here

like image 989
iPeta Avatar asked Sep 20 '16 02:09

iPeta


People also ask

Where does NSLog write to?

NSLog outputs messages to the Apple System Log facility or to the Console app (usually prefixed with the time and the process id). Many of the system frameworks use NSLog for logging exceptions and errors, but there is no requirement to restrict its usage to those purposes.

What is Os_activity_mode?

OS_ACTIVITY_MODE=disable. info, debug, disable. Sets the logging mode of the launched process to the specified level.


2 Answers

A temporary solution, just redefine all NSLOG to printf in a global header file.

#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]); 
like image 150
xfdai Avatar answered Sep 22 '22 13:09

xfdai


In iOS 10 & Xcode 8, Apple switched from the good old ASL (Apple System Log) to a new logging system called Unified logging. NSLog calls are in fact delegating to new os_log API's. (source: https://developer.apple.com/reference/os/logging):

Important

Unified logging is available in iOS 10.0 and later, macOS 10.12 and later, tvOS 10.0 and later, and watchOS 3.0 and later, and supersedes ASL (Apple System Logger) and the Syslog APIs. Historically, log messages were written to specific locations on disk, such as /etc/system.log. The unified logging system stores messages in memory and in a data store, rather than writing to text-based log files.

And

Important

Log message lines greater than the system’s maximum message length are truncated when stored by the logging system. Complete messages are visible when using the log command-line tool to view a live stream of activity. Bear in mind, however, that streaming log data is an expensive activity.

The "system’s maximum message length" limitation is revealed in the SDK's header to be 1024 characters for formatted variables, as noted by @Hot_Leaks (source: <os/log.h>):

/*!    * @function os_log    *     * ...    *    * There is a physical cap of 1024 bytes per log line for dynamic content,    * such as %s and %@, that can be written to the persistence store.    * All content exceeding the limit will be truncated before it is    * written to disk.    *  * ...   *  */   #define os_log(log, format, ...)    os_log_with_type(log, OS_LOG_TYPE_DEFAULT, format, ##__VA_ARGS__) 

Since the buffer size limitation seems to be hard-coded into libsystem_trace.dylib, I don't see a way around it but to print a string literal instead of a formatted variable (%@), or split the formatted string variables to < 1024 strings.

printf will work during debugging, since the debugger (Xcode) shows the process's out / error streams, but it will not be sent to the device log itself. This means that xfdai's solution will not help you when using other log applications such as macOS's Console App, or with issue's emerging on non-debugged applications (such as AppStore application running on customer's device).


Extending xfdai's answer to deployed applications

In deployed applications / non-debug builds, there's no way to see either NSLogs or printfs.

The only way to have messages printed directly to the device log (which can be accessed using Xcode -> Window -> Devices, mac's Console App or 3rd party utilities such as deviceconsole) is calling os_log API's (which is the successor of ASL used since iOS 10).

Here's a global header file I'm using to redefine NSLog as a call to _os_log_internal on iOS 10:

#ifndef PrefixHeader_pch #define PrefixHeader_pch  #ifdef __OBJC__ #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> #endif  #import <os/object.h> #import <os/activity.h>  /*  *  System Versioning Preprocessor Macros  */  #define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame) #define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending) #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) #define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)  // os_log is only supported when compiling with Xcode 8. // Check if iOS version > 10 and the _os_log_internal symbol exists, // load it dynamically and call it. // Definitions extracted from #import <os/log.h>  #if OS_OBJECT_SWIFT3 OS_OBJECT_DECL_SWIFT(os_log); #elif OS_OBJECT_USE_OBJC OS_OBJECT_DECL(os_log); #else typedef struct os_log_s *os_log_t; #endif /* OS_OBJECT_USE_OBJC */  extern struct os_log_s _os_log_default;  extern __attribute__((weak)) void _os_log_internal(void *dso, os_log_t log, int type, const char *message, ...);  // In iOS 10 NSLog only shows in device log when debugging from Xcode: #define NSLog(FORMAT, ...) \ if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {\     void(*ptr_os_log_internal)(void *, __strong os_log_t, int, const char *, ...) = _os_log_internal;\     if (ptr_os_log_internal != NULL) {\         _Pragma("clang diagnostic push")\         _Pragma("clang diagnostic error \"-Wformat\"")\         _os_log_internal(&__dso_handle, OS_OBJECT_GLOBAL_OBJECT(os_log_t, _os_log_default), 0x00, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);\         _Pragma("clang diagnostic pop")\     } else {\         NSLog(FORMAT, ##__VA_ARGS__);\     }\ } else {\     NSLog(FORMAT, ##__VA_ARGS__);\ }  #endif /* PrefixHeader_pch */ 
like image 43
Elist Avatar answered Sep 19 '22 13:09

Elist