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

August 28th, 2009 by aimslife Leave a reply »

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!"

Advertisement

Leave a Reply