iOS 10
Xcode 8
NSLog
debugging
truncated logs

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

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Debugging and logging are integral parts of developing applications, especially when it comes to intricate mobile operating systems like iOS. On iOS 10, issues have been encountered where the commonly used NSLog function in Objective-C appears to truncate output when viewed through Xcode 8. This can be a source of frustration when developers need complete logs for debugging purposes. Let us explore why this happens and how to address it effectively.

Understanding NSLog Truncation

NSLog is a logging function used in Objective-C that outputs messages to the console. This can be crucial for debugging and tracking application behavior during development.

Observations of Truncation

Developers who upgraded to using Xcode 8 and deployed on devices running iOS 10 observed that logs from NSLog are occasionally truncated. This truncation can make it difficult to analyze application behavior, as the full output is not always captured.

Reasons for Truncation

  1. Buffer Size Limitations: The most common cause of truncation is buffer size limitations. By default, iOS devices have a cap on the amount of log data retained in memory. When the buffer fills up, older log messages are overwritten.
  2. Output Stream Overwhelming: Another reason for truncation can be an overwhelmed output stream. NSLog outputs asynchronously, which means if a massive volume of logs is being generated rapidly, some may be cut off when viewed in Xcode's console.
  3. Device-Side Log Handling: iOS handles logs differently compared to the simulator. On devices, there may be additional constraints or event prioritization that impacts log processing.

Solutions and Workarounds

There are several approaches you can adopt to mitigate these issues:

  1. Reduce Log Volume: Simplify and reduce the volume of logs during normal operation. You can use conditional compilation to limit debug messages or adopt different log levels (e.g., error, warning, info, debug).
  2. Persistent Log Files: Instead of relying solely on NSLog, consider writing critical logs to files stored on the device. You can achieve this using standard file operations in iOS, providing you a more persistent logging solution.
  3. Use Logging Libraries: Consider using third-party libraries designed for advanced logging, like CocoaLumberjack. These libraries offer more sophisticated logging techniques, including different log levels and asynchronous log handling.
  4. System Console Utilization: For viewing logs without truncation, connect your iOS device to a Mac and use the system console to view full logs, as it may provide a more comprehensive view of the device's log history.

Example

Here's an example demonstrating how you might set up a simple file logging mechanism in Objective-C:

• (void)logMessage:(NSString *)message {


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.