Cocoa Lumberjack: how to show file and line number?

后端 未结 3 1548
挽巷
挽巷 2021-02-06 07:18

I am trying to find a way for Cocoa Lumberjack to show me file and line number.

After looking through the docs and some Googling, I found no easy way to do this.

3条回答
  •  故里飘歌
    2021-02-06 07:59

    While a separate formatter class would work, it would make your logging code a bit more verbose. In my project I opted for adding some additional macros that make use of CocoaLumberjack like so:

    // Prefix.pch file
    // ...
    
    #ifdef DEBUG
    #define DLogError(fmt, ...) DDLogError((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
    #define DLogWarn(fmt, ...) DDLogWarn((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
    #define DLogInfo(fmt, ...) DDLogInfo((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
    #define DLogDebug(fmt, ...) DDLogDebug((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
    #define DLogVerbose(fmt, ...) DDLogVerbose((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
    #else
    #define DLogError(fmt, ...)
    #define DLogWarn(fmt, ...)
    #define DLogInfo(fmt, ...)
    #define DLogDebug(fmt, ...)
    #define DLogVerbose(fmt, ...)
    #endif
    

    In your client code, you could then call:

    DLogWarn(@"This is a warning");
    

提交回复
热议问题