Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm looking for a review of my macros. I have these in the project pre-compile header. I tent to copy them into all my new projects as well, unless its a very simple project.

#ifdef __APPLE__
    #import <TargetConditionals.h>
#endif


// turn on or off different types of logging. ALog() is always logged.
#define LOG_D       // Turn on standard debug messages  DLog();
#define LOG_N       // Turn on network debug messages   NLog();
#define LOG_DB      // Turn on database debug messages  DBLog();
#undef  LOG_V       // Turn off verose debug messages   VLog();
#undef  LOG_UI      // Turn off popup messages          UILog();


// -- You should not have to mess with anything below this line --


// Be safe, start with all things defined
#define DEBUGLOG(...) do {} while (0)
#define ALERTLOG(...) do {} while (0)
#define ALWAYSLOG(...) do {} while (0)

// Define three basic debug types, DEBUGLOG, ALERTLOG and ALWAYSLOG
// These macros generally should not be called directly
// DEBUGLOG - show a message only when compiled for debugging
// ALERTLOG - show a popup window message only when compile for debugging
// ALWAYSLOG - show a message debugging and release
#ifdef __APPLE__
#ifdef DEBUG
#undef DEBUGLOG
#define DEBUGLOG(type, fmt, ...) NSLog((@"%@: %s [Line %d] " fmt), type, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#undef ALERTLOG
#if TARGET_OS_MAC
#define ALERTLOG(fmt, ...) DEBUGLOG(@"I", fmt, ##__VA_ARGS__)
#else
#define ALERTLOG(fmt, ...)  { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%s\n [Line %d] ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:fmt, ##__VA_ARGS__]  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; }
#endif
#endif
#undef ALWAYSLOG
#define ALWAYSLOG(type, fmt, ...) NSLog((@"%@: %s [Line %d] " fmt), type, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#endif


// Create some standard logging macros
#ifdef LOG_D
#define DLog(fmt, ...) DEBUGLOG(@"D", fmt, ##__VA_ARGS__)
#else
#define DLog(...) do {} while (0)
#endif
#ifdef LOG_N
#define NLog(fmt, ...) DEBUGLOG(@"Net", fmt, ##__VA_ARGS__)
#else
#define NLog(...) do {} while (0)
#endif
#ifdef LOG_DB
#define DBLog(fmt, ...) DEBUGLOG(@"DB", fmt, ##__VA_ARGS__)
#else
#define DBLog(...) do {} while (0)
#endif
#ifdef LOG_V
#define VLog(fmt, ...) DEBUGLOG(@"V", fmt, ##__VA_ARGS__)
#else
#define VLog(...) do {} while (0)
#endif
#ifdef LOG_UI
#define UILog(fmt, ...) ALERTLOG(fmt, ##__VA_ARGS__)
#else
#define UILog(...) do {} while (0)
#endif
#define ALog(fmt, ...) ALWAYSLOG(@"V", fmt, ##__VA_ARGS__)
share|improve this question

2 Answers 2

  1. At very least, DO NOT copy-paste your macros directly into .pch file

    A PCH file is, by definition, a project specific precompiled header. It really shouldn't be used beyond the context of the project and it really shouldn't contain anything but #includes/#imports.

    As suggested by an Apple engineer, you should make a separate file with your macros e.g. Defines.h, and just put #import Defines.h into .pch file.

  2. I would suggest re-thinking you logging strategy. If it is not absolutely necessary (e.g. you do not have access to production instance of your app), you should log only the critical errors. Abundance of logging code decreases its readability/maintainability and can even CAUSE bugs. I recommend an excellent blog post on this matter from co-founder of Stack Overflow.
share|improve this answer

One comment: don't define / undefine the flags that switch logging on or off in the file. That stops you from defining them on the compiler command line. The way you have things now, if you want to make a release build (with no debug logging at all), you have to change the source code.

Another minor point: consider using a logging framework. There are a couple of Objective-C logging frameworks available that will allow you to turn logging on or off without recompiling at all.

share|improve this answer
    
Actually the above will have all debugging off in a release build. There is a #ifdef debug which is compiler controlled around the loggin functions. The inline defines control how much and what kind of logging you want to see when you are in debug mode. –  Justin808 Apr 17 '12 at 17:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.