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

前端 未结 8 2293
别跟我提以往
别跟我提以往 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 14:58

    Define a macro

    #if __has_feature(objc_arc)
      #define MDLog(format, ...) CFShow((__bridge CFStringRef)[NSString stringWithFormat:format, ## __VA_ARGS__]);
    #else
      #define MDLog(format, ...) CFShow([NSString stringWithFormat:format, ## __VA_ARGS__]);
    #endif
    

    And use this macro in you code like

    NSLog(@"some log message");
    MDLog(@"some log message");
    

    Here is the output of console

    NSLog->2014-01-28 10:43:17.873 TestApp[452:60b] some log message
    MDLog -> some log message


    P.S.

    If anyone wants Custom Logs which gives you more info like method name / line number etc. can download the open source MLog.h on GitHub.

    0 讨论(0)
  • 2020-12-02 14:59

    In the top left corner of the console window there is a pulldown menu that says All Output / Debugger Output / Target Output.

    Select Target Output. It worked on my older versions of Xcode, but to be honest with you it doesn't with my current 4.3 version.

    I hope this helps you.

    JR

    0 讨论(0)
  • 2020-12-02 15:05

    Clean Log Macro

    Here is a macro I've made for this purpose. It works exactly like NSLog but with just your text, no extra info


    Macro (paste to your .h)

    #define CLog(__string, ...) fprintf(stderr, "\n%s", [([NSString stringWithFormat:__string, ##__VA_ARGS__]) UTF8String])
    

    Example use:

    CLog(@"I am %i days and %i years old", 3, 7);
    

    Logs:

    I am 3 days and 7 years old

    0 讨论(0)
  • 2020-12-02 15:11

    NSLog() is what is doing that, not the debugger console.

    The easiest way to avoid it is to not use NSLog at all. You could use fprintf(), but that is a pain in that it doesn't support %@ format types.

    I generally write a function for this:

    void MyLog(NSString *format, ...) {
        va_list args;
        va_start(args, format);
        NSString *formattedString = [[NSString alloc] initWithFormat: format
                                                      arguments: args];
        va_end(args);
        [[NSFileHandle fileHandleWithStandardOutput]
            writeData: [formattedString dataUsingEncoding: NSNEXTSTEPStringEncoding]];
    
    }
    

    Obviously, modify it to add a newline or use a shorter prefix, etc...

    (Fixed the stray ctrl-b)

    0 讨论(0)
  • 2020-12-02 15:13

    This is way more easier than the suggested solutions. Using carelesslyChoosy's solution and adding a little bit more to make it log entries on release builds only you get the macro below. Just add this macro to your header or .pch file. This macro will show log entries when the DEBUG flag is enabled, on release builds you won't see log entries.

    #ifdef DEBUG
    #define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
    #define Log(x, ...) NSLog(@"%s %d: " x, __FUNCTION__, __LINE__, ##__VA_ARGS__)
    #else
    #define Log(x, ...)
    #endif
    
    0 讨论(0)
  • 2020-12-02 15:19

    put in this one line code in your .pch file and you are done

    #define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
    

    notice the ## part

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