00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef HAMCAST_LOGGING_H
00024 #define HAMCAST_LOGGING_H
00025
00026 #ifdef __cplusplus
00027 #include <sstream>
00028 extern "C" {
00029 #endif
00030
00031 #define HC_LOG_TRACE_LVL 0x0100
00032 #define HC_LOG_DEBUG_LVL 0x0200
00033 #define HC_LOG_INFO_LVL 0x0300
00034 #define HC_LOG_WARN_LVL 0x0400
00035 #define HC_LOG_ERROR_LVL 0x0500
00036 #define HC_LOG_FATAL_LVL 0x0600
00037
00038 typedef void (*hc_log_fun_t)(int, const char*, const char*);
00039
00043 hc_log_fun_t hc_get_log_fun();
00044
00048 void hc_set_log_fun(hc_log_fun_t function_ptr);
00049
00054 void hc_log(int log_lvl, const char* function_name, const char* log_msg);
00055
00060 void hc_set_default_log_fun(int log_lvl);
00061
00062 #ifdef __cplusplus
00063 }
00064 #endif
00065
00066 #ifdef __GNUC__
00067 # define HC_FUN __PRETTY_FUNCTION__
00068 #else
00069 # define HC_FUN __FUNCTION__
00070 #endif
00071
00072 #ifdef __cplusplus
00073
00074 #define HC_DO_LOG(message, loglvl) \
00075 { \
00076 std::ostringstream scoped_oss; \
00077 scoped_oss << message; \
00078 std::string scoped_osss = scoped_oss.str(); \
00079 hc_log( loglvl , HC_FUN , scoped_osss.c_str()); \
00080 } ((void) 0)
00081
00082 namespace {
00083 template<int m_lvl>
00084 struct HC_trace_helper
00085 {
00086 const char* m_fun;
00087 HC_trace_helper(const char* fun, const std::string& initmsg) : m_fun(fun)
00088 {
00089 std::string msg = "ENTER";
00090 if (!initmsg.empty())
00091 {
00092 msg += ": ";
00093 msg += initmsg;
00094 }
00095 hc_log(m_lvl, m_fun, msg.c_str());
00096 }
00097 ~HC_trace_helper() { hc_log(m_lvl, m_fun, "LEAVE"); }
00098 };
00099 }
00100
00101 #define HC_LOG_TRACE(message) \
00102 ::std::ostringstream hc_trace_helper_##__LINE__ ; \
00103 hc_trace_helper_##__LINE__ << message ; \
00104 ::HC_trace_helper< HC_LOG_TRACE_LVL > hc_fun_HC_trace_helper_##__LINE__ \
00105 ( HC_FUN , hc_trace_helper_##__LINE__ .str() )
00106
00107 #else
00108 # define HC_DO_LOG(message, loglvl) hc_log( loglvl , HC_FUN , message)
00109 # define HC_LOG_TRACE(message) HC_DO_LOG(message, HC_LOG_TRACE_LVL)
00110 #endif
00111
00112 #define HC_LOG_DEBUG(message) HC_DO_LOG(message, HC_LOG_DEBUG_LVL)
00113 #define HC_LOG_INFO(message) HC_DO_LOG(message, HC_LOG_INFO_LVL)
00114 #define HC_LOG_WARN(message) HC_DO_LOG(message, HC_LOG_WARN_LVL)
00115 #define HC_LOG_ERROR(message) HC_DO_LOG(message, HC_LOG_ERROR_LVL)
00116 #define HC_LOG_FATAL(message) HC_DO_LOG(message, HC_LOG_FATAL_LVL)
00117
00118 #endif // HAMCAST_LOGGING_H