/* * UTSDebug.h * * Created on: 03.08.2012 * Author: Viacheslav Isaev */ #ifndef UTSDEBUG_H_ #define UTSDEBUG_H_ #ifndef UTS_LOG_OUT #define UTS_LOG_OUT 0 #endif #include #include #include "errlog.h" #include #if UTS_LOG_OUT == 1 #include #endif #define UTS_LVL_NO_DEUG 0 #define UTS_LVL_CRITICAL 1 #define UTS_LVL_ERROR 2 #define UTS_LVL_WARNING 3 #define UTS_LVL_INFO 4 #define UTS_LVL_DEBUG 5 #define UTS_LVL_TRACE 6 #define UTS_LVL_STACK 7 #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ extern const char* strDebugLevels[]; extern const errlogSevEnum iocDebugLevels[]; #ifdef __cplusplus } #endif /* __cplusplus */ #ifndef UTS_LOG_LEVEL #define UTS_LOG_LEVEL 0 #endif #ifndef UTS_MODULE_NAME #define UTS_MODULE_NAME "MODULE" #endif #define UTS_LOG_EXPAND(LEVEL, args...) UTS_LOG(LEVEL, __FILE__, __LINE__, "", args) #define UTS_LOG(LEVEL, file, line, type, args...) \ do{ \ if (LEVEL <= UTS_LOG_LEVEL)\ UTS_LOG_OUT_FORMATTING(LEVEL, file, line, args);\ }while(0) #if UTS_LOG_OUT == 0 #define UTS_LOG_OUT_TO(LEVEL, args...) \ errlogSevPrintf(iocDebugLevels[LEVEL], "%s", args) #else #define UTS_LOG_OUT_TO(LEVEL, args...) \ std::cout << args << std::endl #endif #define UTS_LOG_OUT_FORMATTING(LEVEL, file, line, args...) \ do{\ const size_t msg_size=255;\ char msg[msg_size];\ char format[msg_size];\ snprintf(&msg[0], msg_size, args); \ snprintf(&format[0], msg_size, "%s [%s] %s:%d %s\n", UTS_MODULE_NAME, strDebugLevels[LEVEL], file, line, msg ); \ UTS_LOG_OUT_TO(LEVEL, format);\ }while(0) #define UTS_CRT(args...) UTS_LOG_EXPAND(UTS_LVL_CRITICAL, args) #define UTS_ERR(args...) UTS_LOG_EXPAND(UTS_LVL_ERROR, args) #define UTS_WRN(args...) UTS_LOG_EXPAND(UTS_LVL_WARNING, args) #define UTS_INF(args...) UTS_LOG_EXPAND(UTS_LVL_INFO, args) #define UTS_DBG(args...) UTS_LOG_EXPAND(UTS_LVL_DEBUG, args) #define UTS_TRC(args...) UTS_LOG_EXPAND(UTS_LVL_TRACE, args) #define UTS_STK(args...) uts::StackLoger loger(args, __FILE__, __LINE__) /// Universal Tracing System (UTS) namespace uts { /** Helper class which allows to track when current stack scope is finished. * */ class StackLoger { const std::string _msg; const std::string _file; int _line; public: StackLoger(const std::string msg, const std::string file, int line); ~StackLoger(); }; /** Call back type for StackMeeter. * */ typedef boost::function StackMeeterCallback; /** Helper class which allows count time in current stack scope. * */ class StackMeeter { clock_t _start; StackMeeterCallback _callback; public: StackMeeter(const StackMeeterCallback &callback): _callback(callback) { _start = clock(); } ~StackMeeter() { clock_t fin = clock() - _start; _callback(fin); } }; } #endif /* UTSDEBUG_H_ */