Skip to content

Instantly share code, notes, and snippets.

@ashikahmad
Last active December 11, 2015 20:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashikahmad/4658632 to your computer and use it in GitHub Desktop.
Save ashikahmad/4658632 to your computer and use it in GitHub Desktop.
A quick replacement to NSLog in your Obj C project

#Better NSLog A quick replacement to NSLog in your Obj C project

This will need no extra effort at all and changes the way you see your logs in debug area.

###How it looks in Debug Area:

Before:

2013-01-29 15:46:24.076 Looptivity[76673:c07] ----2013-01-28 19:02:47 +0000
2013-01-29 15:46:24.076 Looptivity[76673:c07] USER YES
2013-01-29 15:46:24.107 Looptivity[76673:c07] Application become active
2013-01-29 15:46:24.109 Looptivity[76673:c07] Network reachable!

After:

[         MomiLoopAppDelegate.m : 154 ]: ----2013-01-28 19:02:47 +0000
[         MomiLoopAppDelegate.m : 160 ]: USER YES
[         MomiLoopAppDelegate.m : 409 ]: Application become active
[                    MVRemote.m : 102 ]: Network reachable!

###Pros:

  • Automatically NSLogs are vanished from product build.
  • Get rid of long date-time stamp from log.
  • Trace logs with a bit more meaningful "Filename : Line-num" format with your log outputs.

###Cons:

  • In your Debug-area All Output / Target Output should be selected. As it logs with printf function, it will not be visible, when Debugger output is selected. Though, I don't think it is a problem at all, do you?

###How to Use:

  • Just Append this lines in your project's .pch file, nothing more!

###Code:

#ifdef __OBJC__
#define __FILE_NAME_ONLY__ [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String]
#else
#define __FILE_NAME_ONLY__ ""
#endif

#ifndef OPTIMIZE
# define NSLog(...) printf("[%-30s:%4d]: %s\n", __FILE_NAME_ONLY__, __LINE__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String])
#else
#define NSLog(...) do {} while (0)
#endif

###Note: This fix is collected and mixed up from some of blogs and Stack Overflow answers I cannot remember by now. But thanks to all of them to make my life easier!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment