Custom NSLog for release and debugging, custom nslog debugging
Problem
- A large number
NSLog
But you want to cancel theseNSLog
- Is it often used during development?
NSLog(@"%s", __FUNCTION__);
Solve the problem
- New
ExtendNSLog
Class, inherited fromNSObject
- In
ExtendNSLog.h
Delete default code
- Add the following function declaration:
void ExtendNSLog(const char *file, int lineNumber, const char *functionName, NSString *format, ...);
- In
ExtendNSLog.m
Delete default code
- Add the following code:
1 void ExtendNSLog(const char *file, int lineNumber, const char *functionName, NSString *format, ...) { 2 3 va_list ap; 4 5 va_start(ap, format); 6 7 if (![format hasSuffix: @"\n"]) { 8 format = [format stringByAppendingString: @"\n"]; 9 }10 11 NSString *body = [[NSString alloc] initWithFormat:format arguments:ap];12 13 va_end(ap);14 15 NSString *fileName = [[NSString stringWithUTF8String:file] lastPathComponent];16 fprintf(stderr, "(%s) (%s:%d) %s",17 functionName,18 [fileName UTF8String],19 lineNumber,20 [body UTF8String]);21 }
- New
PrefixHeader.pch
File
- Enter the following content:
1 #ifdef __OBJC__ 2 3 #import <UIKit/UIKit.h> 4 #import <Foundation/Foundation.h> 5 6 #import "ExtendNSLog.h" 7 8 #ifdef DEBUG 9 #define NSLog(args...) ExtendNSLog(__FILE__, __LINE__, __PRETTY_FUNCTION__, args);10 #else11 #define NSLog(x...)12 #endif13 14 #endif
- Select
Project
->TARGETS
->[ProjectName]
->Build Settings
- Enter in the search box
prefix header
- In
Prefix Header
Input[ProjectName]/PrefixHeader.pch
As shown in
- Run the test, such as modifying the running mode.