Objective-C NSLog variant: __FILE__ without fullpath and output without datetime

1k views Asked by At

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?

1

There are 1 answers

0
hlfcoding On

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