ab_code.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /***************************************************************************
  2. *
  3. * 通用代码
  4. * 常用函数
  5. * 程序日志输出
  6. * 读取解析配置文件
  7. * 代码转换函数
  8. *
  9. * Author : Li Bo Feng
  10. *
  11. * Update History
  12. * DATE OWNER DESCRIPTION
  13. * ----------- ------------ -----------
  14. * 2010-01-19 Li Bo Feng Generated
  15. *
  16. ***************************************************************************/
  17. #ifndef _AB_CODE_H_
  18. #define _AB_CODE_H_
  19. #ifndef TRUE
  20. #define TRUE (1==1)
  21. #define FALSE !TRUE
  22. #endif
  23. #ifndef timelong
  24. typedef long long time_long;
  25. #endif
  26. ////////////// ABLOG definition ////////////////////////////////////////////////
  27. #define LPL_All 0x00FF
  28. #define LPL_Error 0x0008
  29. #define LPL_Metrics 0x0004
  30. #define LPL_State 0x0002
  31. #define LPL_Detail 0x0001
  32. #define LPL_Flow 0x00F0
  33. #define LPL_Flow_Public 0x0010
  34. #define LPL_Flow_Private 0x0020
  35. #define LPL_Flow_Restricted 0x0040
  36. #define LPL_Flow_Minor 0x0080
  37. #ifndef ABLOG
  38. #define ABLOG ____FunctionID____,__FILE__,__LINE__
  39. #endif
  40. #ifndef ABLOG_IsTracing
  41. #define ABLOG_IsTracing(_class) \
  42. (____LP_LEVEL____ & LPL_##_class)
  43. #endif
  44. #ifndef ABLOG_Entry
  45. #define ABLOG_Entry(_FL_, _FID_) \
  46. static char ____FunctionID____[] = #_FID_; \
  47. static int ____LP_LEVEL____; \
  48. static int ____LP_FLOW____;\
  49. ____LP_LEVEL____ = get_log_print_level(); \
  50. ____LP_FLOW____ = ____LP_LEVEL____ & LPL_Flow_##_FL_; \
  51. if (____LP_FLOW____) {log_println(ABLOG,"Entry");}
  52. #endif
  53. #ifndef ABLOG_Return_String
  54. #define ABLOG_Return_String(_RC_) \
  55. if(____LP_FLOW____){log_println(ABLOG,"Return: %s",_RC_);} return _RC_
  56. #endif
  57. #ifndef ABLOG_Return_Int
  58. #define ABLOG_Return_Int(_RC_) \
  59. if(____LP_FLOW____){log_println(ABLOG,"Return: %s",int_string(_RC_));} return _RC_
  60. #endif
  61. #ifndef ABLOG_Return_Long
  62. #define ABLOG_Return_Long(_RC_) \
  63. if(____LP_FLOW____){log_println(ABLOG,"Return: %s",format_long("%ld", _RC_));} return _RC_
  64. #endif
  65. #ifndef ABLOG_Return_Code
  66. #define ABLOG_Return_Code(_RC_) \
  67. if(____LP_FLOW____){log_println(ABLOG,"Return: %s",____RC2S____(_RC_));} return _RC_
  68. #endif
  69. #ifndef ABLOG_Return_Point
  70. #define ABLOG_Return_Point(_RC_) \
  71. if(____LP_FLOW____){log_println(ABLOG,"Return: 0x%08X", _RC_);} return _RC_
  72. #endif
  73. #ifndef ABLOG_Return
  74. #define ABLOG_Return \
  75. if(____LP_FLOW____){log_println(ABLOG,"Return");} return
  76. #endif
  77. #ifndef ABLOG_
  78. #define ABLOG_(_class, ...) \
  79. if (____LP_LEVEL____ & LPL_##_class) log_println(ABLOG, __VA_ARGS__)
  80. #endif
  81. #ifndef ABLOG_Printf
  82. #define ABLOG_Printf(_class, _args) \
  83. if (____LP_LEVEL____ & LPL_##_class) log_println _args
  84. #endif
  85. #ifndef println
  86. #define println(_args) \
  87. if (____LP_LEVEL____ & LPL_Detail) log_println _args
  88. #endif
  89. typedef char* (*RETURN_CODE_STRING)(int rc);
  90. typedef void (*function_on_ablog_reinitialized)();
  91. // 输出日志重新初始化回调函数
  92. extern function_on_ablog_reinitialized on_ablog_reinitialized;
  93. /**
  94. * 获取日志输出标记
  95. */
  96. int get_log_print_level();
  97. /**
  98. * 是否通过程序设置过ablog相关参数
  99. */
  100. int ablog_print();
  101. /**
  102. * 格式化输出日志
  103. */
  104. void log_println(const char *func, const char *file, int line, const char *format, ...);
  105. /**
  106. * @param fileout 0 输出到 stdout, 1 输出到通过 set_log_filename 设置的文件
  107. * @param func 代码标记
  108. * @param file 代码文件
  109. * @param line 代码行
  110. * @param format 格式
  111. * @param vl va_list
  112. */
  113. void log_println_vl(int fileout, const char *func, const char *file, int line, const char *format, va_list vl);
  114. ////////////// end of ABLOG definition ////////////////////////////////////////////////
  115. /**
  116. * 返回当前进程是否处于 debug 状态
  117. */
  118. int debug_current_process();
  119. /**
  120. * 释放 ab_code 占用的资源
  121. */
  122. void clear_coding();
  123. /**
  124. * 虚拟当前时间 virtual_current_time
  125. * 根据环境变量指定的 VIRTUAL_TIME = `date +"%Y-%m-%d %H:%M:%S"`确定时间偏移
  126. * 返回计算后的虚拟当前时间
  127. * 使用此函数前必须在main开始时调用 init_coding();
  128. */
  129. time_t vtime();
  130. #define MAX_FILEPATH_LENGTH 256
  131. /**
  132. * 模式替换实例化结构
  133. */
  134. struct pattern_instance
  135. {
  136. char pattern[MAX_FILEPATH_LENGTH];
  137. time_t instantiated_time; //实例化时间
  138. time_t expiration_ct; //文件实例名过期时间,以系统时间表示
  139. time_t expiration_vt; //文件实例名过期时间,以虚拟时间表示
  140. int instance_buf_len;
  141. char instance[MAX_FILEPATH_LENGTH]; //实际可用字节数由instance_buf_len指定,并分配内存相应内存
  142. };
  143. void set_log_filename(char *filepath_pattern);
  144. void set_log_keephours(int keep_hours);
  145. void set_log_stdout(int yn);
  146. int ablog_stdout();
  147. int ablog_file();
  148. /**
  149. * 实例化一个模式字符串
  150. * 模式中可以替换内容包括
  151. * [PID] 用当前进程id
  152. * [YYYY] 虚拟当前时间的年
  153. * [yyyy] 系统当前时间的年
  154. * [MM] 虚拟当前时间的月
  155. * [mm] 系统当前时间的月
  156. * [DD] 虚拟当前时间的日
  157. * [dd] 系统当前时间的日
  158. * [HH] 虚拟当前时间的时
  159. * [hh] 系统当前时间的时
  160. * [MI] 虚拟当前时间的分
  161. * [mi] 系统当前时间的分
  162. *
  163. * 返回是否有新的实例产生 1有 0没有
  164. */
  165. int instantiated_pattern(struct pattern_instance *pi);
  166. /**
  167. * 锁文件
  168. */
  169. int lock_file(char* filename, int wait);
  170. /**
  171. * 解锁文件
  172. */
  173. int unlock_file(int lockfd);
  174. ///////////////////////////// 格式化 /////////////////////////////
  175. char* int_string(int n);
  176. #define DEFINE_RETURN_CODE(N) set_code_string(N, #N);
  177. void set_code_string(int n, char* string);
  178. char* code_string(int n);
  179. int parse_hex(char* s, int*o, int default_value);
  180. int parse_int(char* s, int*o, int default_value);
  181. int parse_double(char* s, double*o, double default_value);
  182. char* format_int(char* format, int n);
  183. char* format_long(char* format, long n);
  184. char* format_double(char* format, double n);
  185. time_t parse_time(char *s, long *ms);
  186. struct timeval parse_time_tv(char *stime);
  187. long parse_time_lms(char *stime);
  188. char* time_string(time_t seconds);
  189. char* time_lms_string(time_long milliseconds);
  190. char* time_tv_string(struct timeval tv);
  191. ///////////////////////////// end of 格式化 /////////////////////////////
  192. /**
  193. * 字符串分割
  194. */
  195. char** split_string(char* source, char split_char, char*recv_buf, int recv_buf_bytes,
  196. char**recv_buf_array, int recv_buf_array_size);
  197. /**
  198. * 分割字符串,返回下一分割段的起始位置
  199. */
  200. char* split_string_next(char* source, char split_char, char* recv_buf, int recv_buf_len);
  201. /**
  202. * 字符串替换
  203. */
  204. int replace_string(char* source, char* find, char* replace, char* recv_buf, int recv_buf_len);
  205. /**
  206. * 转换字符串 str 为大写并存入 rbuf, 返回 rbuf
  207. * rbuf的可用内存大小必须大于 strlen(str)
  208. */
  209. char* toUpperCase(const char *str, char *rbuf);
  210. /**
  211. * 去掉字符串 str 左边的空白字符并存入 rbuf, 返回 rbuf
  212. * rbuf的可用内存大小必须大于 strlen(str)
  213. */
  214. char* trimLeft(const char *str, char *rbuf);
  215. /**
  216. * 去掉字符串 str 右边的空白字符并存入 rbuf, 返回 rbuf
  217. * rbuf的可用内存大小必须大于 strlen(str)
  218. */
  219. char* trimRight(const char *str, char *rbuf);
  220. /**
  221. * 去掉字符串 str 两边的空白字符并存入 rbuf, 返回 rbuf
  222. * rbuf的可用内存大小必须大于 strlen(str)
  223. */
  224. char* trim(const char *str, char *rbuf);
  225. /**
  226. * 读mapping文件
  227. */
  228. long read_mapping_file(int fd);
  229. /**
  230. * 写mapping文件
  231. */
  232. int write_mapping_file(int fd, long mid);
  233. int mk_parent_dir(char *filepath);
  234. ///////////////////////////// ABLOG_ComponentUnit /////////////////////////////
  235. #ifndef ABLOG_ComponentUnit
  236. #define ABLOG_ComponentUnit
  237. #ifdef _EDIT_IN_WINDOWS_ONLY
  238. #pragma GCC system_header
  239. #endif
  240. static RETURN_CODE_STRING ____RC2S____ = code_string;
  241. static char ____FunctionID____[] = "";
  242. static int ____LP_LEVEL____ = 0xFF;
  243. static int ____LP_FLOW____ = 0xFF;
  244. #endif//ABLOG_ComponentUnit
  245. ///////////////////////////// ABLOG_ComponentUnit /////////////////////////////
  246. #endif