协能can协议
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LOG.h 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. #ifndef _H_LOG_
  2. #define _H_LOG_
  3. /*
  4. * iLOG3 - log function library written in c
  5. * author : calvin
  6. * email : calvinwilliams.c@gmail.com
  7. * LastVersion : v1.0.11
  8. *
  9. * Licensed under the LGPL v2.1, see the file LICENSE in base directory.
  10. */
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #if ( defined _WIN32 )
  15. #ifndef _WINDLL_FUNC
  16. #define _WINDLL_FUNC _declspec(dllexport)
  17. #endif
  18. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  19. #ifndef _WINDLL_FUNC
  20. #define _WINDLL_FUNC
  21. #endif
  22. #endif
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <time.h>
  28. #include <stdarg.h>
  29. #include <sys/stat.h>
  30. #include <sys/types.h>
  31. #include <errno.h>
  32. #if ( defined _WIN32 )
  33. #include <windows.h>
  34. #include <share.h>
  35. #include <io.h>
  36. #include <fcntl.h>
  37. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  38. #include <fcntl.h>
  39. #include <unistd.h>
  40. #include <sys/time.h>
  41. #include <syslog.h>
  42. #include <pthread.h>
  43. #endif
  44. #if ( defined __STDC_VERSION__ ) && ( __STDC_VERSION__ >= 199901 )
  45. struct tm *localtime_r(const time_t *timep, struct tm *result);
  46. #endif
  47. /* ��������ֵ�� */ /* function returns value macros */
  48. #define LOG_RETURN_ERROR_ALLOC -11 /* �����ڴ�ʧ�� */ /* alloc memory failure */
  49. #define LOG_RETURN_ERROR_INTERNAL -12 /* �ڲ����� */ /* internal error */
  50. #define LOG_RETURN_ERROR_ALLOC_MAX -13 /* �ڴ�ʹ�ó��� */ /* memory usage transfinite */
  51. #define LOG_RETURN_ERROR_PARAMETER -14 /* �������� */ /* parameter invalid */
  52. #define LOG_RETURN_ERROR_NOTSUPPORT -17 /* �ݲ�֧�� */ /* not support */
  53. #define LOG_RETURN_ERROR_CREATEFILE -21 /* �����ļ�ʧ�� */ /* failed to create file */
  54. #define LOG_RETURN_ERROR_OPENFILE -22 /* ���ļ�ʧ�� */ /* failed to open file */
  55. #define LOG_RETURN_ERROR_WRITEFILE -23 /* д�ļ�ʧ�� */ /* failed to write file */
  56. /********************************************************/
  57. /* ������������ The basic features are as follows */
  58. /********************************************************/
  59. /* ��־������� */ /* log output type macros */
  60. #define LOG_OUTPUT_NOSET -1 /* ������ */ /* no set */
  61. #define LOG_OUTPUT_STDOUT 1 /* ��׼��� */ /* stdout */
  62. #define LOG_OUTPUT_STDERR 2 /* ��׼������� */ /* stderr */
  63. #define LOG_OUTPUT_SYSLOG 3 /* UNIX&Linux��syslog �� Windows��WINDOWS EVENT */
  64. #define LOG_OUTPUT_FILE 11 /* �ļ� */ /* file */
  65. #define LOG_OUTPUT_CALLBACK 21 /* ��ʹ���Զ�����־����ص������� */ /* using custom log output callback function */
  66. /* ��־�ȼ��� */ /* log level macros */
  67. #define LOG_LEVEL_DEBUG 0 /* ���Եȼ� */ /* debug level */
  68. #define LOG_LEVEL_INFO 1 /* ��ͨ��Ϣ�ȼ� */ /* info level */
  69. #define LOG_LEVEL_WARN 2 /* ����ȼ� */ /* warn level */
  70. #define LOG_LEVEL_ERROR 3 /* ����ȼ� */ /* error level */
  71. #define LOG_LEVEL_FATAL 4 /* ���ش���ȼ� */ /* error level */
  72. #define LOG_LEVEL_NOLOG 5 /* ����Ҫ�����־ */ /* no log */
  73. #define LOG_LEVEL_DEFAULT LOG_LEVEL_NOLOG
  74. /* ����־�����Ϻ� */ /* Line logging combination style macros */
  75. #define LOG_STYLE_DATE 1 /* ����"YYYY-MM-DD" */ /* date "YYYY-MM-DD" */
  76. #define LOG_STYLE_DATETIME 2 /* ����ʱ��"YYYY-MM-DD hh:mm:ss" */ /* date time "YYYY-MM-DD hh:mm:ss" */
  77. #define LOG_STYLE_DATETIMEMS 4 /* ����ʱ�����"YYYY-MM-DD hh:mm:ss.6ms"������ʱ����껥�⣬�����Զ�ѡ����Ϣ�����ģ� */ /* date time "YYYY-MM-DD hh:mm:ss.6ms" */
  78. #define LOG_STYLE_LOGLEVEL 8 /* ��־�ȼ� */ /* log level */
  79. #define LOG_STYLE_PID 16 /* ����id */ /* pid */
  80. #define LOG_STYLE_TID 32 /* �߳�id */ /* tid */
  81. #define LOG_STYLE_SOURCE 64 /* "Դ�����ļ���:Դ�����к�" */ /* source file name and row number */
  82. #define LOG_STYLE_FORMAT 128 /* Ӧ����־�� */ /* application text */
  83. #define LOG_STYLE_NEWLINE 256 /* ���� */ /* new line */
  84. #define LOG_STYLE_CUSTLABEL1 512 /* �Զ����ǩ1 */ /* custom label1 */
  85. #define LOG_STYLE_CUSTLABEL2 1024 /* �Զ����ǩ2 */ /* custom label2 */
  86. #define LOG_STYLE_CUSTLABEL3 2048 /* �Զ����ǩ3 */ /* custom label3 */
  87. #define LOG_STYLE_CALLBACK 4096 /* ��ʹ���Զ�������־���ص������� */ /* using custom log style callback function */
  88. #define LOG_STYLES_DEFAULT 0
  89. /* ������ */ /* other macros */
  90. #ifndef MAXLEN_FILENAME
  91. #define MAXLEN_FILENAME 256
  92. #endif
  93. typedef struct tagLOG LOG ;
  94. typedef struct tagLOGBUF LOGBUF ;
  95. /* ������־������� */ /* log handle functions */
  96. _WINDLL_FUNC LOG *CreateLogHandle();
  97. _WINDLL_FUNC void DestroyLogHandle( LOG *g );
  98. /* �Զ���򿪡�������ر���־�������� */ /* custom open , write , close log callback functions */
  99. typedef int funcOpenLog( LOG *g , char *log_pathfilename , void **open_handle );
  100. typedef int funcWriteLog( LOG *g , void **open_handle , int log_level , char *buf , long len , long *writelen );
  101. typedef int funcChangeTest( LOG *g , void **test_handle );
  102. typedef int funcCloseLog( LOG *g , void **open_handle );
  103. #define LOG_NO_OUTPUTFUNC NULL , NULL , NULL , NULL , NULL , NULL
  104. /* �Զ�������־��������� */ /* custom log style callback functions */
  105. typedef int funcLogStyle( LOG *g , LOGBUF *logbuf , const char *c_filename , long c_fileline , int log_level , const char *format , va_list valist );
  106. #define LOG_NO_STYLEFUNC NULL
  107. #define func_log_style funcLogStyle
  108. /* ����������ú��� */ /* set log handle environment */
  109. _WINDLL_FUNC int SetLogOutput( LOG *g , int output , char *log_pathfilename , funcOpenLog *pfuncOpenLogFirst , funcOpenLog *pfuncOpenLog , funcWriteLog *pfuncWriteLog , funcChangeTest *pfuncChangeTest , funcCloseLog *pfuncCloseLog , funcCloseLog *pfuncCloseLogFinally );
  110. _WINDLL_FUNC int SetLogOutput2( LOG *g , int output , funcOpenLog *pfuncOpenLogFirst , funcOpenLog *pfuncOpenLog , funcWriteLog *pfuncWriteLog , funcChangeTest *pfuncChangeTest , funcCloseLog *pfuncCloseLog , funcCloseLog *pfuncCloseLogFinally , char *log_pathfilename_format , ... );
  111. _WINDLL_FUNC int SetLogLevel( LOG *g , int log_level );
  112. _WINDLL_FUNC int SetLogStyles( LOG *g , long log_styles , funcLogStyle *pfuncLogStyle );
  113. /* д��־���� */ /* output log */
  114. _WINDLL_FUNC int WriteLog( LOG *g , char *c_filename , long c_fileline , int log_level , char *format , ... );
  115. _WINDLL_FUNC int DebugLog( LOG *g , const char *c_filename , long c_fileline , const char *format , ... );
  116. _WINDLL_FUNC int InfoLog( LOG *g , const char *c_filename , long c_fileline , const char *format , ... );
  117. _WINDLL_FUNC int WarnLog( LOG *g , const char *c_filename , long c_fileline , const char *format , ... );
  118. _WINDLL_FUNC int ErrorLog( LOG *g , const char *c_filename , long c_fileline , const char *format , ... );
  119. _WINDLL_FUNC int FatalLog( LOG *g , const char *c_filename , long c_fileline , const char *format , ... );
  120. /* дʮ�����ƿ���־���� */ /* output hex log */
  121. _WINDLL_FUNC int WriteHexLog( LOG *g , char *c_filename , long c_fileline , int log_level , char *buffer , long buflen , char *format , ... );
  122. _WINDLL_FUNC int DebugHexLog( LOG *g , char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... );
  123. _WINDLL_FUNC int InfoHexLog( LOG *g , char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... );
  124. _WINDLL_FUNC int WarnHexLog( LOG *g , char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... );
  125. _WINDLL_FUNC int ErrorHexLog( LOG *g , char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... );
  126. _WINDLL_FUNC int FatalHexLog( LOG *g , char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... );
  127. #if ( defined __STDC_VERSION__ ) && ( __STDC_VERSION__ >= 199901 )
  128. /* д��־�����Ŀɱ������ */ /* output log macros */
  129. #define WRITELOG( _g_ , _log_level_ , ... ) WriteLog( _g_ , __FILE__ , __LINE__ , _log_level_ , __VA_ARGS__ );
  130. #define DEBUGLOG( _g_ , ... ) DebugLog( _g_ , __FILE__ , __LINE__ , __VA_ARGS__ );
  131. #define INFOLOG( _g_ , ... ) InfoLog( _g_ , __FILE__ , __LINE__ , __VA_ARGS__ );
  132. #define WARNLOG( _g_ , ... ) WarnLog( _g_ , __FILE__ , __LINE__ , __VA_ARGS__ );
  133. #define ERRORLOG( _g_ , ... ) ErrorLog( _g_ , __FILE__ , __LINE__ , __VA_ARGS__ );
  134. #define FATALLOG( _g_ , ... ) FatalLog( _g_ , __FILE__ , __LINE__ , __VA_ARGS__ );
  135. /* дʮ�����ƿ���־�Ŀɱ������ */ /* output hex log macros */
  136. #define WRITEHEXLOG( _g_ , _log_level_ , _buf_ , _buf_size_ , ... ) WriteHexLog( _g_ , __FILE__ , __LINE__ , _log_level_ , _buf_ , _buf_size_ , __VA_ARGS__ );
  137. #define DEBUGHEXLOG( _g_ , _buf_ , _buf_size_ , ... ) DebugHexLog( _g_ , __FILE__ , __LINE__ , _buf_ , _buf_size_ , __VA_ARGS__ );
  138. #define INFOHEXLOG( _g_ , _buf_ , _buf_size_ , ... ) InfoHexLog( _g_ , __FILE__ , __LINE__ , _buf_ , _buf_size_ , __VA_ARGS__ );
  139. #define WARNHEXLOG( _g_ , _buf_ , _buf_size_ , ... ) WarnHexLog( _g_ , __FILE__ , __LINE__ , _buf_ , _buf_size_ , __VA_ARGS__ );
  140. #define ERRORHEXLOG( _g_ , _buf_ , _buf_size_ , ... ) ErrorHexLog( _g_ , __FILE__ , __LINE__ , _buf_ , _buf_size_ , __VA_ARGS__ );
  141. #define FATALHEXLOG( _g_ , _buf_ , _buf_size_ , ... ) FatalHexLog( _g_ , __FILE__ , __LINE__ , _buf_ , _buf_size_ , __VA_ARGS__ );
  142. #endif
  143. /* ����滻�� */
  144. #define create_log_handle CreateLogHandle
  145. #define destroy_log_handle DestroyLogHandle
  146. #define func_open_log funcOpenLog
  147. #define func_write_log funcWriteLog
  148. #define func_change_test funcChangeTest
  149. #define func_close_log funcCloseLog
  150. #define set_log_output SetLogOutput
  151. #define set_log_output2 SetLogOutput2
  152. #define set_log_level SetLogLevel
  153. #define set_log_styles SetLogStyles
  154. #define write_log WriteLog
  155. #define debug_log DebugLog
  156. #define info_log InfoLog
  157. #define warn_log WarnLog
  158. #define error_log ErrorLog
  159. #define fatal_log FatalLog
  160. #define write_hex_log WriteHexLog
  161. #define debug_hex_log DebugHexLog
  162. #define info_hex_log InfoHexLog
  163. #define warn_hex_log WarnHexLog
  164. #define error_hex_log ErrorHexLog
  165. #define fatal_hex_log FatalHexLog
  166. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  167. _WINDLL_FUNC LOG *CreateLogHandleG();
  168. _WINDLL_FUNC void DestroyLogHandleG();
  169. _WINDLL_FUNC int SetLogOutputG( int output , char *log_pathfilename , funcOpenLog *pfuncOpenLogFirst , funcOpenLog *pfuncOpenLog , funcWriteLog *pfuncWriteLog , funcChangeTest *pfuncChangeTest , funcCloseLog *pfuncCloseLog , funcCloseLog *pfuncCloseLogFinally );
  170. _WINDLL_FUNC int SetLogLevelG( int log_level );
  171. _WINDLL_FUNC int SetLogStylesG( long log_styles , funcLogStyle *pfuncLogStyles );
  172. #if ( defined __STDC_VERSION__ ) && ( __STDC_VERSION__ >= 199901 )
  173. #define SetLogOutput2G(_output_,_pfuncOpenLogFirst_,_pfuncOpenLog_,_pfuncWriteLog_,_pfuncChangeTest_,_pfuncCloseLog_,_pfuncCloseLogFinally_,_log_pathfilename_format_,...) \
  174. SetLogOutput2(_output_,_pfuncOpenLogFirst_,_pfuncOpenLog_,_pfuncWriteLog_,_pfuncChangeTest_,_pfuncCloseLog_,_pfuncCloseLogFinally_,_log_pathfilename_format_,__VA_ARGS__)
  175. #endif
  176. /* д��־�����������̱߳��ش洢��ȱʡ��־����ĺ������ϰ汾�� */ /* output log for TLS */
  177. _WINDLL_FUNC int WriteLogG( char *c_filename , long c_fileline , int log_level , char *format , ... );
  178. _WINDLL_FUNC int DebugLogG( char *c_filename , long c_fileline , char *format , ... );
  179. _WINDLL_FUNC int InfoLogG( char *c_filename , long c_fileline , char *format , ... );
  180. _WINDLL_FUNC int WarnLogG( char *c_filename , long c_fileline , char *format , ... );
  181. _WINDLL_FUNC int ErrorLogG( char *c_filename , long c_fileline , char *format , ... );
  182. _WINDLL_FUNC int FatalLogG( char *c_filename , long c_fileline , char *format , ... );
  183. /* дʮ�����ƿ���־�����������̱߳��ش洢��ȱʡ��־����ĺ������ϰ汾�� */ /* output hex log for TLS */
  184. _WINDLL_FUNC int WriteHexLogG( char *c_filename , long c_fileline , int log_level , char *buffer , long buflen , char *format , ... );
  185. _WINDLL_FUNC int DebugHexLogG( char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... );
  186. _WINDLL_FUNC int InfoHexLogG( char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... );
  187. _WINDLL_FUNC int WarnHexLogG( char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... );
  188. _WINDLL_FUNC int ErrorHexLogG( char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... );
  189. _WINDLL_FUNC int FatalHexLogG( char *c_filename , long c_fileline , char *buffer , long buflen , char *format , ... );
  190. #if ( defined __STDC_VERSION__ ) && ( __STDC_VERSION__ >= 199901 )
  191. /* д��־�����Ŀɱ�����꣨�����̱߳��ش洢��ȱʡ��־����ĺ������ϰ汾�� */ /* output log macros for TLS */
  192. #define WRITELOGG( _log_level_ , ... ) WriteLogG( __FILE__ , __LINE__ , _log_level_ , __VA_ARGS__ );
  193. #define DEBUGLOGG( ... ) DebugLogG( __FILE__ , __LINE__ , __VA_ARGS__ );
  194. #define INFOLOGG( ... ) InfoLogG( __FILE__ , __LINE__ , __VA_ARGS__ );
  195. #define WARNLOGG( ... ) WarnLogG( __FILE__ , __LINE__ , __VA_ARGS__ );
  196. #define ERRORLOGG( ... ) ErrorLogG( __FILE__ , __LINE__ , __VA_ARGS__ );
  197. #define FATALLOGG( ... ) FatalLogG( __FILE__ , __LINE__ , __VA_ARGS__ );
  198. /* дʮ�����ƿ���־�Ŀɱ�����꣨�����̱߳��ش洢��ȱʡ��־����ĺ������ϰ汾�� */ /* output hex log macros for TLS */
  199. #define WRITEHEXLOGG( _log_level_ , _buf_ , _buf_size_ , ... ) WriteHexLogG( __FILE__ , __LINE__ , _log_level_ , _buf_ , _buf_size_ , __VA_ARGS__ );
  200. #define DEBUGHEXLOGG( _buf_ , _buf_size_ , ... ) DebugHexLogG( __FILE__ , __LINE__ , _buf_ , _buf_size_ , __VA_ARGS__ );
  201. #define INFOHEXLOGG( _buf_ , _buf_size_ , ... ) InfoHexLogG( __FILE__ , __LINE__ , _buf_ , _buf_size_ , __VA_ARGS__ );
  202. #define WARNHEXLOGG( _buf_ , _buf_size_ , ... ) WarnHexLogG( __FILE__ , __LINE__ , _buf_ , _buf_size_ , __VA_ARGS__ );
  203. #define ERRORHEXLOGG( _buf_ , _buf_size_ , ... ) ErrorHexLogG( __FILE__ , __LINE__ , _buf_ , _buf_size_ , __VA_ARGS__ );
  204. #define FATALHEXLOGG( _buf_ , _buf_size_ , ... ) FatalHexLogG( __FILE__ , __LINE__ , _buf_ , _buf_size_ , __VA_ARGS__ );
  205. #endif
  206. /* ����滻�� */
  207. #define create_log_handle_g CreateLogHandleG
  208. #define destroy_log_handle_g DestroyLogHandleG
  209. #define set_log_output_g SetLogOutputG
  210. #define set_log_level_g SetLogLevelG
  211. #define set_log_styles_g SetLogStylesG
  212. #define set_log_output2_g SetLogOutput2G
  213. #define write_log_g WriteLogG
  214. #define debug_log_g DebugLogG
  215. #define info_log_g InfoLogG
  216. #define warn_log_g WarnLogG
  217. #define error_log_g ErrorLogG
  218. #define fatal_log_g FatalLogG
  219. #define write_hex_log_g WriteHexLogG
  220. #define debug_hex_log_g DebugHexLogG
  221. #define info_hex_log_g InfoHexLogG
  222. #define warn_hex_log_g WarnHexLogG
  223. #define error_hex_log_g ErrorHexLogG
  224. #define fatal_hex_log_g FatalHexLogG
  225. #endif
  226. /********************************************************/
  227. /* ����Ϊ�߼����� The following is a advanced features */
  228. /********************************************************/
  229. /* �Զ�������־�ȼ��ص��������� */ /* custom check log level callback function */
  230. typedef int funcFilterLog( LOG *g , void **open_handle , int log_level , char *buf , long len );
  231. #define func_filter_log funcFilterLog
  232. /* �Զ����ǩ���� */ /* custom tag amount */
  233. #define LOG_MAXCNT_CUST_LABEL 3
  234. /* �Զ����ǩ��󳤶� */ /* custom tag maximum length */
  235. #define LOG_MAXLEN_CUST_LABEL 32
  236. /* ��־ѡ�� */ /* log options */
  237. #define LOG_OPTION_OPEN_AND_CLOSE 1 /* ÿ�ζ�����־��д��־���ر���־ */ /* open , write , close log every time */
  238. #define LOG_OPTION_CHANGE_TEST 2 /* ����ļ��䶯 */ /* detect log changed and reopen it */
  239. #define LOG_OPTION_OPEN_ONCE 4 /* ��־��һ�β��ر� */ /* open log once */
  240. #define LOG_OPTION_SET_OUTPUT_BY_FILENAME 8 /* �Զ������ļ�������������� */ /* reset log output type automatically */
  241. /* "#stdout" -> LOG_OUTPUT_STDOUT */
  242. /* "#stderr" -> LOG_OUTPUT_STDERR */
  243. /* "#syslog" -> LOG_OUTPUT_SYSLOG */
  244. #define LOG_OPTION_FILENAME_APPEND_DOT_LOG 16 /* ��־����ļ������Զ�����".log" */ /* append ".log" */
  245. #define LOG_OPTION_OPEN_DEFAULT LOG_OPTION_CHANGE_TEST
  246. /* ��־ת��ģʽ */ /* log rotate mode */
  247. #define LOG_ROTATEMODE_NONE 0 /* ��ת�� */ /* no rotate */
  248. #define LOG_ROTATEMODE_SIZE 1 /* ����־�ļ���Сת�����ͺ���SetLogRotateSize���ʹ�ã�ת���ļ�����ʽ"ԭ��־�ļ���.���" */ /* according to log file size turn log */
  249. #define LOG_ROTATEMODE_PER_DAY 2 /* ��ÿ��ת����ת���ļ�����ʽ"ԭ��־�ļ���.����������������" */ /* according to daily turn log */
  250. #define LOG_ROTATEMODE_PER_HOUR 3 /* ��Сʱת����ת���ļ�����ʽ"ԭ��־�ļ���.����������������_Сʱ" */ /* according to hours turn log */
  251. /* ��־ת��ȱʡֵ */ /* log rotate default macros */
  252. #define LOG_ROTATE_SIZE_FILE_COUNT_DEFAULT 99999999
  253. #define LOG_ROTATE_SIZE_PRESSURE_FACTOR_DEFAULT 0
  254. #define LOG_FSYNC_PERIOD 10000
  255. /* �Զ�����־ת��ǰ��ص��������� */ /* custom turn log file callback function */
  256. typedef int funcBeforeRotateFile( LOG *g , char *rotate_log_pathfilename );
  257. typedef int funcAfterRotateFile( LOG *g , char *rotate_log_pathfilename );
  258. #define func_before_rotate_file funcBeforeRotateFile
  259. #define func_after_rotate_file funcAfterRotateFile
  260. /* ��������Сȱʡֵ */ /* default buffer size */
  261. #define LOG_BUFSIZE_DEFAULT (1024) /* ȱʡ����־��������С */
  262. #define LOG_BUFSIZE_MAX (16*1024) /* �������־��������С */
  263. #define LOG_HEXLOG_BUFSIZE_DEFAULT (4*1024) /* ȱʡʮ�����ƿ���־��������С */
  264. #define LOG_HEXLOG_BUFSIZE_MAX (100*1024) /* ���ʮ�����ƿ���־��������С */
  265. /* �߼�����������ú��� */ /* senior handle environment setting function */
  266. _WINDLL_FUNC int SetLogOptions( LOG *g , int log_options );
  267. _WINDLL_FUNC int SetLogFileChangeTest( LOG *g , long interval );
  268. _WINDLL_FUNC int SetLogFsyncPeriod( LOG *g , long period );
  269. _WINDLL_FUNC int SetLogCustLabel( LOG *g , int index , char *cust_label );
  270. _WINDLL_FUNC int SetLogRotateMode( LOG *g , int rotate_mode );
  271. _WINDLL_FUNC int SetLogRotateSize( LOG *g , long log_rotate_size );
  272. _WINDLL_FUNC int SetLogRotatePressureFactor( LOG *g , long pressure_factor );
  273. _WINDLL_FUNC int SetLogRotateFileCount( LOG *g , long rotate_file_count );
  274. _WINDLL_FUNC int SetBeforeRotateFileFunc( LOG *g , funcAfterRotateFile *pfuncAfterRotateFile );
  275. _WINDLL_FUNC int SetAfterRotateFileFunc( LOG *g , funcAfterRotateFile *pfuncAfterRotateFile );
  276. _WINDLL_FUNC int SetFilterLogFunc( LOG *g , funcFilterLog *pfuncFilterLog );
  277. _WINDLL_FUNC int SetLogBufferSize( LOG *g , long log_bufsize , long max_log_bufsize );
  278. _WINDLL_FUNC int SetHexLogBufferSize( LOG *g , long hexlog_bufsize , long max_log_hexbufsize );
  279. _WINDLL_FUNC int SetLogOutputFuncDirectly( LOG *g , funcOpenLog *pfuncOpenLogFirst , funcOpenLog *pfuncOpenLog , funcWriteLog *pfuncWriteLog , funcChangeTest *pfuncChangeTest , funcCloseLog *pfuncCloseLog , funcCloseLog *pfuncCloseLogFinally );
  280. _WINDLL_FUNC int SetLogStyleFuncDirectly( LOG *g , funcLogStyle *pfuncLogStyle );
  281. /* ����滻�� */
  282. #define set_log_options SetLogOptions
  283. #define set_log_file_change_test SetLogFileChangeTest
  284. #define set_log_fsync_period SetLogFsyncPeriod
  285. #define set_log_cust_label SetLogCustLabel
  286. #define set_log_rotate_mode SetLogRotateMode
  287. #define set_log_rotate_size SetLogRotateSize
  288. #define set_log_rotate_pressure_factor SetLogRotatePressureFactor
  289. #define set_log_rotate_file_count SetLogRotateFileCount
  290. #define set_before_rotate_file_func SetBeforeRotateFileFunc
  291. #define set_after_rotate_file_func SetAfterRotateFileFunc
  292. #define set_filter_log_func SetFilterLogFunc
  293. #define set_log_buffer_size SetLogBufferSize
  294. #define set_hex_log_buffer_size SetHexLogBufferSize
  295. #define set_log_output_func_directly SetLogOutputFuncDirectly
  296. #define set_log_stlye_func_directly SetLogStyleFuncDirectly
  297. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  298. _WINDLL_FUNC int SetLogOptionsG( int log_options );
  299. _WINDLL_FUNC int SetLogFileChangeTestG( long interval );
  300. _WINDLL_FUNC int SetLogFsyncPeriodG( long period );
  301. _WINDLL_FUNC int SetLogCustLabelG( int index , char *cust_label );
  302. _WINDLL_FUNC int SetLogRotateModeG( int rotate_mode );
  303. _WINDLL_FUNC int SetLogRotateSizeG( long log_rotate_size );
  304. _WINDLL_FUNC int SetLogRotatePressureFactorG( long pressure_factor );
  305. _WINDLL_FUNC int SetLogRotateFileCountG( long rotate_file_count );
  306. _WINDLL_FUNC int SetBeforeRotateFileFuncG( funcAfterRotateFile *pfuncAfterRotateFile );
  307. _WINDLL_FUNC int SetAfterRotateFileFuncG( funcAfterRotateFile *pfuncAfterRotateFile );
  308. _WINDLL_FUNC int SetFilterLogFuncG( funcFilterLog *pfuncFilterLog );
  309. _WINDLL_FUNC int SetLogBufferSizeG( long log_bufsize , long max_log_bufsize );
  310. _WINDLL_FUNC int SetHexLogBufferSizeG( long hexlog_bufsize , long max_log_hexbufsize );
  311. _WINDLL_FUNC int SetLogOutputFuncDirectlyG( funcOpenLog *pfuncOpenLogFirst , funcOpenLog *pfuncOpenLog , funcWriteLog *pfuncWriteLog , funcChangeTest *pfuncChangeTest , funcCloseLog *pfuncCloseLog , funcCloseLog *pfuncCloseLogFinally );
  312. _WINDLL_FUNC int SetLogStyleFuncDirectlyG( funcLogStyle *pfuncLogStyle );
  313. #endif
  314. /* ����滻�� */
  315. #define set_log_options_g SetLogOptionsG
  316. #define set_log_file_change_test_g SetLogFileChangeTestG
  317. #define set_log_fsync_period_g SetLogFsyncPeriodG
  318. #define set_log_cust_label_g SetLogCustLabelG
  319. #define set_log_rotate_mode_g SetLogRotateModeG
  320. #define set_log_rotate_size_g SetLogRotateSizeG
  321. #define set_log_rotate_pressure_factor_g SetLogRotatePressureFactorG
  322. #define set_log_rotate_file_count_g SetLogRotateFileCountG
  323. #define set_before_rotate_file_func_g SetBeforeRotateFileFuncG
  324. #define set_after_rotate_file_func_g SetAfterRotateFileFuncG
  325. #define set_filter_log_func_g SetFilterLogFuncG
  326. #define set_log_buffer_size_g SetLogBufferSizeG
  327. #define set_hex_log_buffer_size_g SetHexLogBufferSizeG
  328. #define set_log_output_func_directly_g SetLogOutputFuncDirectlyG
  329. #define set_log_stlye_func_directly_g SetLogStyleFuncDirectlyG
  330. #define TEST_LOGLEVEL_NOTENOUGH(_log_level_,_g_) ( (_log_level_) < (_g_)->log_level )
  331. #define TEST_ATTRIBUTE(_param_,_attr_) ( ( _param_ & _attr_ ) == _attr_ )
  332. #define TEST_NOT_ATTRIBUTE(_param_,_attr_) ( ( _param_ & _attr_ ) != _attr_ )
  333. #define TEST_STDSTREAM(_fd_) ( (_fd_) != FD_NULL && ( (_fd_) == STDOUT_HANDLE || (_fd_) == STDERR_HANDLE ) )
  334. #define TEST_FILEFD(_fd_) ( (_fd_) != FD_NULL && (_fd_) != STDOUT_HANDLE && (_fd_) != STDERR_HANDLE )
  335. #if ( defined _WIN32 ) || ( defined __linux__ ) || ( defined _AIX ) || ( defined __hpux )
  336. /* �õ������̱߳��ش洢��ȱʡ��־����ĺ����汾 */ /* log handle get/set function for TLS */
  337. _WINDLL_FUNC LOG *GetGlobalLOG();
  338. _WINDLL_FUNC void SetGlobalLOG( LOG *g );
  339. #define get_global_log GetGlobalLOG
  340. #define set_global_log SetGlobalLOG
  341. #endif
  342. /********************************************************/
  343. /* ����Ϊ���ӿ��� the following development for the hook*/
  344. /********************************************************/
  345. /* ƽ̨�� */ /* platfrom macros */
  346. #if ( defined _WIN32 )
  347. #define LOG_NEWLINE "\r\n"
  348. #define LOG_NEWLINE_LEN 2
  349. #define SNPRINTF _snprintf
  350. #define VSNPRINTF _vsnprintf
  351. #define STDOUT_HANDLE 1
  352. #define STDERR_HANDLE 2
  353. #define FD_NULL -1
  354. #define WRITE _write
  355. #define STAT _stat
  356. #define ACCESS _access
  357. #define ACCESS_OK 00
  358. #define RENAME rename
  359. #define UNLINK unlink
  360. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  361. #define LOG_NEWLINE "\n"
  362. #define LOG_NEWLINE_LEN 1
  363. #define SNPRINTF snprintf
  364. #define VSNPRINTF vsnprintf
  365. #define STDOUT_HANDLE STDOUT_FILENO
  366. #define STDERR_HANDLE STDERR_FILENO
  367. #define FD_NULL -1
  368. #define WRITE write
  369. #define STAT stat
  370. #define ACCESS access
  371. #define ACCESS_OK F_OK
  372. #define RENAME rename
  373. #define UNLINK unlink
  374. #endif
  375. /* ����� */ /* code macros */
  376. #if ( defined _WIN32 )
  377. #define SYSTEMTIME2TIMEVAL_USEC(_syst_,_tv_) \
  378. (_tv_).tv_usec = (_syst_).wMilliseconds * 1000 ;
  379. #define SYSTEMTIME2TM(_syst_,_stime_) \
  380. (_stime_).tm_year = (_syst_).wYear - 1900 ; \
  381. (_stime_).tm_mon = (_syst_).wMonth - 1 ; \
  382. (_stime_).tm_mday = (_syst_).wDay ; \
  383. (_stime_).tm_hour = (_syst_).wHour ; \
  384. (_stime_).tm_min = (_syst_).wMinute ; \
  385. (_stime_).tm_sec = (_syst_).wSecond ;
  386. #define LOCALTIME(_tt_,_stime_) \
  387. { \
  388. SYSTEMTIME stNow ; \
  389. GetLocalTime( & stNow ); \
  390. SYSTEMTIME2TM( stNow , (_stime_) ); \
  391. }
  392. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  393. #define LOCALTIME(_tt_,_stime_) \
  394. localtime_r(&(_tt_),&(_stime_));
  395. #endif
  396. #ifndef STRCMP
  397. #define STRCMP(_a_,_C_,_b_) ( strcmp(_a_,_b_) _C_ 0 )
  398. #define STRNCMP(_a_,_C_,_b_,_n_) ( strncmp(_a_,_b_,_n_) _C_ 0 )
  399. #endif
  400. #ifndef STRICMP
  401. #if ( defined _WIN32 )
  402. #define STRICMP(_a_,_C_,_b_) ( stricmp(_a_,_b_) _C_ 0 )
  403. #define STRNICMP(_a_,_C_,_b_,_n_) ( strnicmp(_a_,_b_,_n_) _C_ 0 )
  404. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  405. #define STRICMP(_a_,_C_,_b_) ( strcasecmp(_a_,_b_) _C_ 0 )
  406. #define STRNICMP(_a_,_C_,_b_,_n_) ( strncasecmp(_a_,_b_,_n_) _C_ 0 )
  407. #endif
  408. #endif
  409. #define OFFSET_BUFPTR(_logbuf_,_offset_len_) \
  410. if( _offset_len_ > 0 && _offset_len_ <= (_logbuf_)->buf_remain_len && (_logbuf_)->bufptr[0] ) \
  411. { \
  412. (_logbuf_)->bufptr += _offset_len_ ; \
  413. (_logbuf_)->buf_remain_len -= _offset_len_ ; \
  414. } \
  415. #define OFFSET_BUFPTR_IN_LOOP(_logbuf_,_offset_len_) \
  416. if( _offset_len_ > 0 && _offset_len_ <= (_logbuf_)->buf_remain_len && (_logbuf_)->bufptr[0] ) \
  417. { \
  418. (_logbuf_)->bufptr += _offset_len_ ; \
  419. (_logbuf_)->buf_remain_len -= _offset_len_ ; \
  420. } \
  421. else \
  422. { \
  423. break; \
  424. } \
  425. _WINDLL_FUNC int SetOpenFlag( LOG *g , char open_flag );
  426. _WINDLL_FUNC char IsLogOpened( LOG *g );
  427. _WINDLL_FUNC int GetLogLevel( LOG *g );
  428. _WINDLL_FUNC LOGBUF *GetLogBuffer( LOG *g );
  429. _WINDLL_FUNC LOGBUF *GetHexLogBuffer( LOG *g );
  430. _WINDLL_FUNC long FormatLogBuffer( LOG *g , LOGBUF *logbuf , const char *format , ... );
  431. _WINDLL_FUNC long FormatLogBufferV( LOG *g , LOGBUF *logbuf , const char *format , va_list valist );
  432. _WINDLL_FUNC long MemcatLogBuffer( LOG *g , LOGBUF *logbuf , const char *append , long len );
  433. #define set_open_flag SetOpenFlag
  434. #define is_log_opened IsLogOpened
  435. #define get_log_level GetLogLevel
  436. #define get_log_buffer GetLogBuffer
  437. #define get_hex_log_buffer GetHexLogBuffer
  438. #define format_log_buffer FormatLogBuffer
  439. #define format_log_buffer_v FormatLogBufferV
  440. #define memcat_log_buffer MemcatLogBuffer
  441. /* ��־�������ṹ */ /* log buffer structure */
  442. struct tagLOGBUF
  443. {
  444. long max_buf_size ;
  445. long buf_size ;
  446. char *bufbase ;
  447. char *bufptr ;
  448. long buf_remain_len ;
  449. } ;
  450. /* ��־����ṹ */ /* log handle structure */
  451. struct tagLOG
  452. {
  453. /* ���� */ /* basic */
  454. int output ;
  455. char log_pathfilename[ MAXLEN_FILENAME + 1 ];
  456. funcOpenLog *pfuncOpenLogFirst ;
  457. funcOpenLog *pfuncOpenLog ;
  458. funcWriteLog *pfuncWriteLog ;
  459. funcChangeTest *pfuncChangeTest ;
  460. funcCloseLog *pfuncCloseLog ;
  461. funcCloseLog *pfuncCloseLogFinally ;
  462. void *open_handle ;
  463. void *test_handle ;
  464. int fd ;
  465. #if ( defined _WIN32 )
  466. HANDLE hFile ;
  467. #endif
  468. char open_flag ;
  469. /* ��־�ȼ� */ /* log level */
  470. int log_level ;
  471. funcFilterLog *pfuncFilterLog ;
  472. /* ����־��� */ /* log style */
  473. long log_styles ;
  474. funcLogStyle *pfuncLogStyle ;
  475. /* �����и�ʽ��񣬹�����ʽ�������飬����д��־ʱ�����������־������ */
  476. funcLogStyle *pfuncLogStyles[ 30 + 1 ] ;
  477. int style_func_count ;
  478. /* ��־ѡ�� */ /* log options */
  479. int log_options ;
  480. long file_change_test_interval ;
  481. long file_change_test_no ;
  482. struct STAT file_change_stat ;
  483. long fsync_period ;
  484. long fsync_elapse ;
  485. /* �Զ����ǩ */ /* custom labels */
  486. char cust_label[LOG_MAXCNT_CUST_LABEL][ LOG_MAXLEN_CUST_LABEL + 1 ] ;
  487. /* ��־ת�� */ /* log rotate */
  488. int rotate_mode ;
  489. long log_rotate_size ;
  490. long rotate_file_no ;
  491. long rotate_file_count ;
  492. long pressure_factor ;
  493. long skip_count ;
  494. funcAfterRotateFile *pfuncAfterRotateFile ;
  495. funcBeforeRotateFile *pfuncBeforeRotateFile ;
  496. /* ����־������ */ /* log buffer */
  497. LOGBUF logbuf ;
  498. /* ʮ��������־������ */ /* hex log buffer */
  499. LOGBUF hexlogbuf ;
  500. /* һ������ */ /* level 1 cache */
  501. struct timeval cache1_tv ;
  502. struct tm cache1_stime ;
  503. /* �������� */ /* level 2 cache */
  504. struct timeval cache2_logstyle_tv ;
  505. struct tm cache2_logstyle_stime ;
  506. char cache2_logstyle_date_buf[ 10 + 1 ] ;
  507. long cache2_logstyle_date_buf_len ;
  508. char cache2_logstyle_datetime_buf[ 19 + 1 ] ;
  509. long cache2_logstyle_datetime_buf_len ;
  510. unsigned long cache2_logstyle_pid ;
  511. char cache2_logstyle_pid_buf[ 20 + 1 ] ;
  512. long cache2_logstyle_pid_buf_len ;
  513. unsigned long cache2_logstyle_tid ;
  514. char cache2_logstyle_tid_buf[ 20 + 1 ] ;
  515. long cache2_logstyle_tid_buf_len ;
  516. struct timeval cache2_rotate_tv ;
  517. struct tm cache2_rotate_stime ;
  518. /* ת���ļ��� */ /* rotate file lock */
  519. #if ( defined _WIN32 )
  520. HANDLE rotate_lock ;
  521. #elif ( defined __unix ) || ( defined _AIX ) || ( defined __linux__ ) || ( defined __hpux )
  522. int rotate_lock ;
  523. struct flock lock ;
  524. #endif
  525. } ;
  526. /********************************************************/
  527. /* ����Ϊ�����ӿ� the following for the assist interface*/
  528. /********************************************************/
  529. /* ���ø������� */ /* config assist functions */
  530. _WINDLL_FUNC int ConvertLogOutput_atoi( char *output_desc , int *p_log_output );
  531. _WINDLL_FUNC int ConvertLogLevel_atoi( char *log_level_desc , int *p_log_level );
  532. _WINDLL_FUNC int ConvertLogLevel_itoa( int log_level , char **log_level_desc );
  533. _WINDLL_FUNC int ConvertLogStyle_atol( char *line_style_desc , long *p_line_style );
  534. _WINDLL_FUNC int ConvertLogOption_atol( char *log_option_desc , long *p_log_option );
  535. _WINDLL_FUNC int ConvertLogRotateMode_atoi( char *rotate_mode_desc , int *p_rotate_mode );
  536. _WINDLL_FUNC int ConvertBufferSize_atol( char *bufsize_desc , long *p_bufsize );
  537. #define convert_log_output_atoi ConvertLogOutput_atoi
  538. #define convert_log_level_atoi ConvertLogLevel_atoi
  539. #define convert_log_level_itoa ConvertLogLevel_itoa
  540. #define convert_log_style_atol ConvertLogStyle_atol
  541. #define cnovert_log_option_atol ConvertLogOption_atol
  542. #define convert_log_rotate_mode_atoi ConvertLogRotateMode_atoi
  543. #define convert_buffer_size_atol ConvertBufferSize_atol
  544. #ifdef __cplusplus
  545. }
  546. #endif
  547. #endif