PrintLog.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // "$Id$"
  3. //
  4. // Copyright (c)1992-2007, ZheJiang Dahua Technology Stock CO.LTD.
  5. // All Rights Reserved.
  6. //
  7. // Description:
  8. // Revisions: Year-Month-Day SVN-Author Modification
  9. //
  10. #ifndef __INFRA3_PRINT_LOG_H__
  11. #define __INFRA3_PRINT_LOG_H__
  12. #include <stdio.h>
  13. #include "Defs.h"
  14. #include "Function.h"
  15. #include "Thread.h"
  16. //// log格式"[Time]|Category|[Version]|Level|File|[Function]|Line|[Tid]|Content"
  17. /// \defgroup PrintLog PrintLog
  18. /// 打印信息记录等等。
  19. /// 打印信息分trace, debug, info, warn, error, fatal共6个级别,记录的形式可以是
  20. /// 直接打印到标准输出,也可以记录到文件或者发送到远程日志服务器,目前只支持
  21. /// 第一种方式。每行打印信息前自动加上了时间和打印类型的字符串,比如"12:54:12|info"
  22. /// @{
  23. namespace Dahua{
  24. namespace Infra{
  25. ///日志等级, 通过setPrintLogLevel进行设置
  26. enum LogLevel
  27. {
  28. logLevelUnknown= 0, ///< 未知打印等级类型
  29. logLevelFatal, ///< fatal等级,当设置为此等级时,有一种打印输出(fatal)都有输出
  30. logLevelError, ///< error等级,当设置为此等级时,有两种打印输出(fatal,error)都有输出
  31. logLevelWarn, ///< warn等级,当设置为此等级时,有三种打印输出(fatal,error,warn)都有输出
  32. logLevelInfo, ///< info等级,当设置为此等级时,有四种打印输出(fatal,error,warn,info)都有输出
  33. logLevelTrace, ///< Trace等级,当设置为此等级时,有五种打印输出(fatal,error,warn,info,trace)都有输出
  34. logLevelDebug, ///< Debug等级,当设置为此等级时,以上六种打印(fatal,error,warn,info,trace,debug)都有输出
  35. };
  36. //打印选项
  37. struct PrintOptions
  38. {
  39. char time; //是否打印时间,0打印,1不打印,默认打印
  40. char color; //是否打印颜色,0打印,1不打印,默认打印
  41. char libName; //是否打印库名,0打印,1不打印,默认打印
  42. char level; //是否打印等级,0打印,1不打印,默认打印
  43. char resv[28]; //保留
  44. };
  45. /// 打印输出回调函数类型
  46. /// 参数为要打印的字符串
  47. typedef TFunction1<void, char const*> LogPrinterProc;
  48. /// 定义打印回调函数类型
  49. typedef TFunction1<bool, char const*> PrintProc;
  50. /// 设置打印的输出回调函数
  51. /// \param [in] printer 输出回调函数, 为空时设置打印输出到标准输出设备
  52. /// \return 成功返回0, 失败返回-1
  53. int INFRA_API setLogPrinter(LogPrinterProc printer);
  54. /// 设置打印的级别,高于该级别的打印不能输出
  55. ///\param [in] level-日志等级,参见 LogLevel 定义
  56. void INFRA_API setPrintLogLevel(int level);
  57. /// 打印调用库名,记录调试信息,级别nLevel,不直接调用
  58. /// \return 返回打印的字节数
  59. int INFRA_API logLibName(int nLevel, const char* libName, const char* fmt, ...)
  60. __attribute__((format(printf, 3, 4)));
  61. /// 记录调试信息,级别6,不直接调用,用于实现debgf宏
  62. /// \return 返回打印的字节数
  63. int INFRA_API logDebug(const char* fmt, ...) __attribute__((format(printf, 1, 2)));
  64. /// 记录跟踪信息,级别5,不直接调用,用于实现tracef宏
  65. /// \return 返回打印的字节数
  66. int INFRA_API logTrace(const char* fmt, ...) __attribute__((format(printf, 1, 2)));
  67. /// 记录调试信息,级别4,不直接调用,用于实现infof宏
  68. /// \return 返回打印的字节数
  69. int INFRA_API logInfo(const char* fmt, ...) __attribute__((format(printf, 1, 2)));
  70. /// 记录警告信息,级别3,不直接调用,用于实现warnf宏
  71. /// \return 返回打印的字节数
  72. int INFRA_API logWarn(const char* fmt, ...) __attribute__((format(printf, 1, 2)));
  73. /// 记录错误信息,级别2,不直接调用,用于实现errorf宏
  74. /// \return 返回打印的字节数
  75. int INFRA_API logError(const char* fmt, ...) __attribute__((format(printf, 1, 2)));
  76. /// 记录致命错误信息,级别1,不直接调用,用于实现fatalf宏
  77. /// \return 返回打印的字节数
  78. int INFRA_API logFatal(const char* fmt, ...) __attribute__((format(printf, 1, 2)));
  79. /// 以16进制和字符对应的方式打印内存数据
  80. /// \param pdat 数据缓冲指针
  81. /// \param length 数据缓冲长度
  82. void INFRA_API dumpHex(uint8_t* pdat, size_t length = 512);
  83. /// 注册打印处理函数
  84. bool INFRA_API attachPrint(PrintProc proc);
  85. /// 注销打印处理函数
  86. bool INFRA_API detachPrint(PrintProc proc);
  87. /// 设置打印选项
  88. /// \printOptions 打印选项,参见PrintOptions结构体
  89. void INFRA_API setPrintOptions(PrintOptions printOptions);
  90. ///设置日志动态过滤条件(-h命令查看帮助),
  91. ///参数说明:logfilter command usage:
  92. ///logfilter -loglevel [levelnum](0-no printlevel, 1-fatal, 2-error, 3-warn, 4-info, 5-trace, 6-debug): set log level;
  93. ///logfilter -logmodule [modulename] [on/off] #turn module's log on/off
  94. ///logfilter -logmoduleLevel[modulename][levelnum] #set module's loglevel
  95. ///logfilter -logtime [on/off] #turn log's time on/off
  96. ///logfilter -logthread [on/off] #turn log's thread on/off
  97. ///logfilter -logversion [on/off] #turn module svn version on/off
  98. ///logfilter -logpath [logpath/""] #logger module's log to logpath or cancel logger(by set path to "")
  99. ///logfiter -logsize [logsize] #log file max size limited to logsize Kbytes;
  100. ///\ param [in] argc --arg参数的个数
  101. ///\ param [in] argv---arg参数内容
  102. void INFRA_API setLogFilter(int argc, char * argv[]);
  103. struct ModulePrintLevel{
  104. char module[256];
  105. Infra::LogLevel level;
  106. };
  107. ///获取所有模块的日志级别
  108. ///\ param [in/out] moduleLlist --日志列表指针
  109. ///\ param [out] len --日志列表有效长度
  110. void INFRA_API getLogModules(ModulePrintLevel moduleLlist[256], int &len);
  111. /// 日志过滤处理函数
  112. ///\ param [in] level--日志等级
  113. ///\ param [in] module--该日志产生的模块名
  114. ///\ param [in] file--该日志产生的文件名
  115. ///\ param [in] func--该日志产生的函数名
  116. ///\ param [in] line--该日志产生的行号
  117. ///\ param [in] svnversion ---该模块的版本号
  118. ///\ param [in] fmt---日志格式控制符,后面跟用户的日志信息
  119. int INFRA_API logFilter(int level,
  120. const char* module,
  121. const char *file,
  122. const char *func,
  123. int line,
  124. const char *svnversion,
  125. const char *fmt, ...)__attribute__((format(printf, 7, 8)));
  126. /// 日志模块清理接口,用于清理日志模块所打开的资源,
  127. /// 应该最后模块卸载或者程序退出才使用。
  128. void INFRA_API logCleanup();
  129. } // namespace Infra
  130. } // namespace Dahua
  131. #define __FUNCTION_NO_NS__ (strrchr(__FUNCTION__, ':') != NULL ? strrchr(__FUNCTION__, ':') + 1 : __FUNCTION__)
  132. //////////////////////////////////////////////////////////////////////////
  133. // print message, controlled by messsage level
  134. #undef debugf
  135. #undef tracef
  136. #undef infof
  137. #undef warnf
  138. #undef errorf
  139. #undef fatalf
  140. #undef tracepoint
  141. #ifndef LOG_LIB_NAME
  142. #define LOG_LIB_NAME "Unknown"
  143. #endif
  144. #ifndef LOG_LIB_VERSION
  145. #define LOG_LIB_VERSION "Unknown"
  146. #endif
  147. #if !defined(_MSC_VER) || (_MSC_VER > 1300)
  148. #define debugf(format, ...) \
  149. logFilter(Dahua::Infra::logLevelDebug, LOG_LIB_NAME,__FILE__, __FUNCTION_NO_NS__, __LINE__, LOG_LIB_VERSION, format , ## __VA_ARGS__)
  150. #define tracef(format, ...)\
  151. logFilter(Dahua::Infra::logLevelTrace, LOG_LIB_NAME,__FILE__, __FUNCTION_NO_NS__, __LINE__, LOG_LIB_VERSION, format , ## __VA_ARGS__)
  152. #define infof(format, ...) \
  153. logFilter(Dahua::Infra::logLevelInfo, LOG_LIB_NAME,__FILE__, __FUNCTION_NO_NS__, __LINE__, LOG_LIB_VERSION, format , ## __VA_ARGS__)
  154. #define warnf(format, ...) \
  155. logFilter(Dahua::Infra::logLevelWarn, LOG_LIB_NAME,__FILE__, __FUNCTION_NO_NS__, __LINE__, LOG_LIB_VERSION, format , ## __VA_ARGS__)
  156. #define errorf(format, ...) \
  157. logFilter(Dahua::Infra::logLevelError, LOG_LIB_NAME,__FILE__, __FUNCTION_NO_NS__, __LINE__, LOG_LIB_VERSION, format , ## __VA_ARGS__)
  158. #define fatalf(format, ...) \
  159. logFilter(Dahua::Infra::logLevelFatal, LOG_LIB_NAME,__FILE__, __FUNCTION_NO_NS__, __LINE__, LOG_LIB_VERSION, format , ## __VA_ARGS__)
  160. #define tracepoint() debugf("tracepoint:\n")
  161. /// 打印宏(带当前线程号和this指针地址)
  162. #define debugf_log_this(format, ...) debugf("[%s:%d] this:%p tid:%d, " format, __FILE__, __LINE__, this,Dahua::Infra::CThread::getCurrentThreadID(), ## __VA_ARGS__)
  163. #define tracef_log_this(format, ...) tracef("[%s:%d] this:%p tid:%d, " format, __FILE__, __LINE__, this, Dahua::Infra::CThread::getCurrentThreadID(), ## __VA_ARGS__)
  164. #define infof_log_this(format, ...) infof("[%s:%d] this:%p tid:%d, " format, __FILE__, __LINE__, this,Dahua::Infra::CThread::getCurrentThreadID(), ## __VA_ARGS__)
  165. #define warnf_log_this(format, ...) warnf("[%s:%d] this:%p tid:%d, " format, __FILE__, __LINE__, this, Dahua::Infra::CThread::getCurrentThreadID(), ## __VA_ARGS__)
  166. #define errorf_log_this(format, ...) errorf("[%s:%d] this:%p tid:%d, " format, __FILE__, __LINE__, this, Dahua::Infra::CThread::getCurrentThreadID(), ## __VA_ARGS__)
  167. #define fatalf_log_this(format, ...) fatalf("[%s:%d] this:%p tid:%d, " format, __FILE__, __LINE__, this, Dahua::Infra::CThread::getCurrentThreadID(), ## __VA_ARGS__)
  168. /// 打印宏(带当前线程号)
  169. #define debugf_log(format, ...) debugf("[%s:%d] tid:%d, " format, __FILE__, __LINE__, Dahua::Infra::CThread::getCurrentThreadID(), ## __VA_ARGS__)
  170. #define tracef_log(format, ...) tracef("[%s:%d] tid:%d, " format, __FILE__, __LINE__, Dahua::Infra::CThread::getCurrentThreadID(), ## __VA_ARGS__)
  171. #define infof_log(format, ...) infof("[%s:%d] tid:%d, " format, __FILE__, __LINE__, Dahua::Infra::CThread::getCurrentThreadID(), ## __VA_ARGS__)
  172. #define warnf_log(format, ...) warnf("[%s:%d] tid:%d, " format, __FILE__, __LINE__, Dahua::Infra::CThread::getCurrentThreadID(), ## __VA_ARGS__)
  173. #define errorf_log(format, ...) errorf("[%s:%d] tid:%d, " format, __FILE__, __LINE__, Dahua::Infra::CThread::getCurrentThreadID(), ## __VA_ARGS__)
  174. #define fatalf_log(format, ...) fatalf("[%s:%d] tid:%d, " format, __FILE__, __LINE__, Dahua::Infra::CThread::getCurrentThreadID(), ## __VA_ARGS__)
  175. #else
  176. ////// 对于windows平台vc6(_MSC_VER <1300)日志打印宏
  177. #define debugf logDebug
  178. #define tracef logTrace
  179. #define infof logInfo
  180. #define warnf logWarn
  181. #define errorf logError
  182. #define fatalf logFatal
  183. #define tracepoint logDebug
  184. /// 打印宏(带当前线程号和this指针地址)
  185. #define debugf_log_this debugf
  186. #define tracef_log_this tracef
  187. #define infof_log_this infof
  188. #define warnf_log_this warnf
  189. #define errorf_log_this errorf
  190. #define fatalf_log_this fatalf
  191. /// 打印宏(带当前线程号)
  192. #define debugf_log debugf
  193. #define tracef_log tracef
  194. #define infof_log infof
  195. #define warnf_log warnf
  196. #define errorf_log errorf
  197. #define fatalf_log fatalf
  198. #endif
  199. #endif //__INFRA_PRINT_LOG_H__