How to turn off all output of Xcode console in iOS development

A running project found that the console output a lot of content, how to turn it off.

1. The original printing data includes xcode printing data, third-party library data and self written data

2. In pch, after replacing NSLog with macro definition, we found that there were many less printings written by ourselves

#define NSLog(...) //NSLog(__VA_ARGS__)

3.[DYMTLInitPlatform] platform initialization successful

After searching, it is found that under edit scheme - > Options, close GPU Frame Capture and its Metal API Validation

4. Then it will be left

These are the output from the third-party library, for example: listen to the yungaode tweet

5. Use fishhook to replace some output functions (fishhook is very simple, just one. h and one. m are also very simple. If you need to contact me, you can ask for demo project)

Replace NSLog first

static void (*orig_NSLog)(NSString *format, ...);

void my_NSLog(NSString *buts, ...){


rebind_symbols((struct rebinding[1]){ {"NSLog", my_NSLog, (void *)&orig_NSLog}}, 1);

int fd = open(argv[0], O_RDONLY);
uint32_t magic_number = 0;
read(fd, &magic_number, 4);
printf("Mach-O Magic Number: %x \n", magic_number);

Less found

6. Continue to replace printf. It is found that there is no change. There is only one NSLogv to replace

int my_printf(const char * ss, ...) {
    return 0;
//    return orig_printf(ss);
static void (*orig_NSLogv)(NSString *format,va_list args);
void  my_NSLogv(NSString *format,va_list args){
        rebind_symbols((struct rebinding[1]){ {"NSLogv", my_NSLogv, (void *)&orig_NSLogv}}, 1);
        rebind_symbols((struct rebinding[1]){ {"printf", my_printf, (void *)&orig_printf}}, 1);

7.CUICatalog: Invalid asset name supplied: because the name after [UIImage imageNamed: @ ""] is empty, just modify it


8. So far, all the logs are gone




There's another way to do it. It's just a few lines of code. The steps above are all saved


#pragma mark - user method, write the output information to the dr.log file;
// Save print information to a file in the Document directory
- (void)redirectNSlogToDocumentFolder
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    NSString *fileName = [NSString stringWithFormat:@"dr.log"];// Note it's not NSData!
    NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];
    // Delete existing files first
    NSFileManager *defaultManager = [NSFileManager defaultManager];
    [defaultManager removeItemAtPath:logFilePath error:nil];
    // Import log to file
    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);
    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);


Tags: less xcode

Posted on Sat, 28 Dec 2019 11:03:28 -0500 by jeffjohnvol