1

For debugging purposes I've added this code to my .pch. Although I'm pretty satisfied with the output, I would like to improve the way I print the filename (without the full path) when I use __FILE__ in the DLog definition.

I'm using [[NSString stringWithUTF8String:__FILE__] lastPathComponent]

Maybe you could you suggest a cleaner/shorter way to do it?

#ifdef DEBUG
    #define DLog(...) NSLog(@"%@ %d %s %s", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, __PRETTY_FUNCTION__, __FUNCTION__)
    #define ALog(...) [[NSAssertionHandler currentHandler] handleFailureInFunction:[NSString stringWithCString:__PRETTY_FUNCTION__ encoding:NSUTF8StringEncoding] file:[NSString stringWithCString:__FILE__ encoding:NSUTF8StringEncoding] lineNumber:__LINE__ description:__VA_ARGS__]
#else
    #define DLog(...) do { } while (0)
    #ifndef NS_BLOCK_ASSERTIONS
        #define NS_BLOCK_ASSERTIONS
    #endif
    #define ALog(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])
#endif

UPDATE If possible I would like also get rid of the datetime and app name (i.e. 2012-05-30 16:23:29.795 AppName[11746:12203]. Any suggestion?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
microspino
  • 7,693
  • 3
  • 48
  • 49
  • the closest one I can think of is `printf` itself. – Eimantas May 30 '12 at 14:35
  • have you tried using printf function? – Adam May 30 '12 at 14:35
  • I have my own logging code which is shared by my C++ static library and my Objective-C++ application and I resorted to holding a `static const char *m_classname` in every class and a macro that passed it to the log function. The method name is supplied using `__FUNCTION__`. Works well. – trojanfoe May 30 '12 at 14:41

1 Answers1

1

I think [[NSString stringWithUTF8String:__FILE__] lastPathComponent] could become @(__FILE__).lastPathComponent, given the literal syntax that got introduced after you asked this question.

hlfcoding
  • 2,532
  • 1
  • 21
  • 25