In Xcode, is there a way to disable the timestamps that appear in the debugger console when calling NSLog?

前端 未结 8 2294
别跟我提以往
别跟我提以往 2020-12-02 14:30

Xcode\'s debugger console makes it easy to see any debugging messages my app sends out using NSLog(), but it always sticks a timestamp prefix on them:



        
相关标签:
8条回答
  • 2020-12-02 15:19

    ARC Version:

    void NFLog(NSString *format, ...)
    {
        va_list args;
        va_start(args, format);
        NSString *formattedString = [NSString stringWithFormat:format, args];
        formattedString = [formattedString stringByAppendingString:@"\n"];
        va_end(args);
        [[NSFileHandle fileHandleWithStandardOutput] writeData: [formattedString dataUsingEncoding: NSUTF8StringEncoding]];
    }
    

    update:

    What I am doing is add this code to xxx-Prefix.pch then you can use it anywhere:

    #define newLine         do { [(NSFileHandle*)[NSFileHandle fileHandleWithStandardOutput] writeData:[@"\n" dataUsingEncoding: NSUTF8StringEncoding]]; } while(0);
    #define NFLog(args,...) do { [(NSFileHandle*)[NSFileHandle fileHandleWithStandardOutput] writeData:[[NSString stringWithFormat:args, ##__VA_ARGS__] dataUsingEncoding: NSUTF8StringEncoding]]; } while(0); newLine
    

    and if you want NSLog back:

    #define NFLog(args,...) NSLog(args,##__VA_ARGS__)
    
    0 讨论(0)
  • 2020-12-02 15:21

    A way to keep using NSLog in conjunction with bbum's answer is to use a preprocessor macro to redefine NSLog to your own function

    #define USECUSTOMLOGS 1
    #if USECUSTOMLOGS
    #define NSLog MyLog
    #endif
    

    This will replace NSLog with MyLog on compile time. Basically you can keep using NSLog everywhere and it will still use your custom format for the console window. You can also change it back to use NSLog at anytime by changing the 1 to a 0.

    0 讨论(0)
提交回复
热议问题