How make debugging levels and enable/disable in C language?

Debugging is always pay important role in software development life cycle. You can make your life easy with the help of macros. With the help of macros you can enable and disable debugging and can define debugging level.

/**
* LOGGING LEVEL
* ***************
* Macro                        Reason
* ****************************************************************************************************************
* __DEBUG_LEVEL_LOG__          Complete logging including debug level, info level, warning level and error level
* __INFO_LEVEL_LOG__           Logging including info level, warning level and error level
* __WARNING_LEVEL_LOG__        Logging including warning level and error level
* __ERROR_LEVEL_LOG__          Logging including error level
*
* __DEBUG_LEVEL_LOG_ONLY__     Logging of debug level only
* __INFO_LEVEL_LOG_ONLY__      Logging of info level only
* __WARNING_LEVEL_LOG_ONLY__   Logging of warning level only
* __ERROR_LEVEL_LOG_ONLY__     Logging of error level only
*
**/

#define __DEBUG_LEVEL_LOG__
#define __PRINT_LEVEL_LOG__
#define __LINUX_USER_SPACE__ // __LINUX_KERNEL_SPACE__

#ifdef __DEBUG_LEVEL_LOG__
        #define __INFO_LEVEL_LOG__
        #define __WARNING_LEVEL_LOG__
        #define __ERROR_LEVEL_LOG__
#endif

#ifdef __INFO_LEVEL_LOG__
        #define __WARNING_LEVEL_LOG__
        #define __ERROR_LEVEL_LOG__
#endif

#ifdef __WARNING_LEVEL_LOG__
        #define __ERROR_LEVEL_LOG__
#endif

#ifdef __DEBUG_LEVEL_LOG_ONLY__
    #define __DEBUG_LEVEL_LOG__
#endif

#ifdef __INFO_LEVEL_LOG_ONLY__
    #define __INFO_LEVEL_LOG__
#endif

#ifdef __WARNING_LEVEL_LOG_ONLY__
    #define __WARNING_LEVEL_LOG__
#endif

#ifdef __ERROR_LEVEL_LOG_ONLY__
    #define __ERROR_LEVEL_LOG__
#endif

#ifdef __PRINT_LEVEL_LOG__
  #ifdef __LINUX_KERNEL_SPACE__
    #define print(format, …) printk(format, ## __VA_ARGS__);
  #endif
  #ifdef __LINUX_USER_SPACE__
    #define print(format, …) printf(format, ## __VA_ARGS__);
  #else
    #define print(format, …)
  #endif
#else
    #define print(format, …)
#endif

#ifdef __DEBUG_LEVEL_LOG__
    #define debug(format, …) { \
        print("DEBUG   : [%s() : %s : %d] : ", __FUNCTION__, __FILE__, __LINE__); \
        print(format, ## __VA_ARGS__); \
        print("\n"); \
    }
#else
    #define debug(format, …)
#endif

Please create "info, warning and error" logging statement according to Debugging Logging macro. First three macros will help us to enable/disable logging and defines logging level.

Enjoy it! and say "AHO!"

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s