Why this happens
NSLog by itself only prints the timestamp, process name, and the format string you provide. It does not include the calling method name or source line number, which makes it harder to trace log output back to the exact location in your code. Additionally, leaving NSLog calls in shipping code adds overhead and can expose internal details.
The solution is to create custom macros that inject __PRETTY_FUNCTION__ (the full method signature) and __LINE__ (the current line number) into every log statement, and guard them with a DEBUG flag so they compile away in release configurations.
How to fix it
1. Define a debug-only macro with method and line info
Add the following to a common header (e.g., DebugLog.h) or your prefix header:
#ifdef DEBUG
# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
# define DLog(...)
#endif
__PRETTY_FUNCTION__expands to the pretty-printed function/method name (e.g.,-[ViewController viewDidLoad]).__LINE__expands to the current source line number.##__VA_ARGS__handles variadic arguments cleanly even when no extra arguments are passed.- In release builds (
DEBUGnot defined),DLogexpands to nothing, so the calls incur zero runtime cost.
Usage:
- (void)viewDidLoad {
[super viewDidLoad];
DLog(@"View loaded with frame: %@", NSStringFromCGRect(self.view.frame));
}
Output example:
-[ViewController viewDidLoad] [Line 23] View loaded with frame: {{0, 0}, {375, 667}}
2. Provide an always-on macro for critical logs
Some messages (e.g., assertion failures, security events) should appear even in release builds. Define a second macro without the DEBUG guard:
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
Use ALog sparingly for logs that must survive in production.
3. (Optional) Show an on-device alert for immediate visibility
During development you may want a visual alert instead of (or in addition to) console output. This macro displays a UIAlertController (modern replacement for the deprecated UIAlertView):
#ifdef DEBUG
# define ULog(fmt, ...) \
do { \
NSString *msg = [NSString stringWithFormat:(fmt), ##__VA_ARGS__]; \
NSString *title = [NSString stringWithFormat:@"%s\n[Line %d]", __PRETTY_FUNCTION__, __LINE__]; \
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert]; \
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; \
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alert animated:YES completion:nil]; \
} while (0)
#else
# define ULog(...)
#endif
Note:
UIAlertViewis deprecated since iOS 9. The snippet above usesUIAlertControllerfor current iOS versions.
4. Ensure the DEBUG flag is set in your project
- Open the target’s Build Settings.
- Search for Preprocessor Macros (
GCC_PREPROCESSOR_DEFINITIONS). - Add
DEBUG=1to the Debug configuration only (leave Release empty).
Alternatively, add -DDEBUG=1 to Other C Flags (OTHER_CFLAGS) for the Debug configuration.
How to prevent it
- Always use
DLog(or your project’s equivalent) instead of rawNSLogfor debug output. - Keep
ALogusage to a minimum; treat it like a production log statement. - Run a static analysis or grep for
NSLogbefore archiving to catch any stray calls. - Consider adopting
os_log/OSLogfor structured, performant logging in new code.
FAQ
Does this work with Swift code?
No. These macros are Objective-C / C preprocessor constructs. In Swift, use #function, #file, #line with print or os_log, or adopt a logging library like swift-log.
Why use __PRETTY_FUNCTION__ instead of __FUNCTION__?
__PRETTY_FUNCTION__ includes the class name and full method signature (e.g., -[MyClass myMethod:]), while __FUNCTION__ only gives the bare method name. The richer context makes logs much easier to search.
Can I strip all logging automatically without macros?
Xcode’s Strip Debug Symbols During Copy (COPY_PHASE_STRIP) and Deployment Postprocessing (DEPLOYMENT_POSTPROCESSING) remove symbols but do not remove NSLog calls themselves. Macros (or #if DEBUG blocks) are the standard way to eliminate the calls at compile time.
Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.
