/*************************************************************************** * * 通用代码 * 常用函数 * 程序日志输出 * 读取解析配置文件 * 代码转换函数 * * Author : Li Bo Feng * * Update History * DATE OWNER DESCRIPTION * ----------- ------------ ----------- * 2010-01-19 Li Bo Feng Generated * ***************************************************************************/ #ifndef _AB_CODE_H_ #define _AB_CODE_H_ #ifndef TRUE #define TRUE (1==1) #define FALSE !TRUE #endif #ifndef timelong typedef long long time_long; #endif ////////////// ABLOG definition //////////////////////////////////////////////// #define LPL_All 0x00FF #define LPL_Error 0x0008 #define LPL_Metrics 0x0004 #define LPL_State 0x0002 #define LPL_Detail 0x0001 #define LPL_Flow 0x00F0 #define LPL_Flow_Public 0x0010 #define LPL_Flow_Private 0x0020 #define LPL_Flow_Restricted 0x0040 #define LPL_Flow_Minor 0x0080 #ifndef ABLOG #define ABLOG ____FunctionID____,__FILE__,__LINE__ #endif #ifndef ABLOG_IsTracing #define ABLOG_IsTracing(_class) \ (____LP_LEVEL____ & LPL_##_class) #endif #ifndef ABLOG_Entry #define ABLOG_Entry(_FL_, _FID_) \ static char ____FunctionID____[] = #_FID_; \ static int ____LP_LEVEL____; \ static int ____LP_FLOW____;\ ____LP_LEVEL____ = get_log_print_level(); \ ____LP_FLOW____ = ____LP_LEVEL____ & LPL_Flow_##_FL_; \ if (____LP_FLOW____) {log_println(ABLOG,"Entry");} #endif #ifndef ABLOG_Return_String #define ABLOG_Return_String(_RC_) \ if(____LP_FLOW____){log_println(ABLOG,"Return: %s",_RC_);} return _RC_ #endif #ifndef ABLOG_Return_Int #define ABLOG_Return_Int(_RC_) \ if(____LP_FLOW____){log_println(ABLOG,"Return: %s",int_string(_RC_));} return _RC_ #endif #ifndef ABLOG_Return_Long #define ABLOG_Return_Long(_RC_) \ if(____LP_FLOW____){log_println(ABLOG,"Return: %s",format_long("%ld", _RC_));} return _RC_ #endif #ifndef ABLOG_Return_Code #define ABLOG_Return_Code(_RC_) \ if(____LP_FLOW____){log_println(ABLOG,"Return: %s",____RC2S____(_RC_));} return _RC_ #endif #ifndef ABLOG_Return_Point #define ABLOG_Return_Point(_RC_) \ if(____LP_FLOW____){log_println(ABLOG,"Return: 0x%08X", _RC_);} return _RC_ #endif #ifndef ABLOG_Return #define ABLOG_Return \ if(____LP_FLOW____){log_println(ABLOG,"Return");} return #endif #ifndef ABLOG_ #define ABLOG_(_class, ...) \ if (____LP_LEVEL____ & LPL_##_class) log_println(ABLOG, __VA_ARGS__) #endif #ifndef ABLOG_Printf #define ABLOG_Printf(_class, _args) \ if (____LP_LEVEL____ & LPL_##_class) log_println _args #endif #ifndef println #define println(_args) \ if (____LP_LEVEL____ & LPL_Detail) log_println _args #endif typedef char* (*RETURN_CODE_STRING)(int rc); typedef void (*function_on_ablog_reinitialized)(); // 输出日志重新初始化回调函数 extern function_on_ablog_reinitialized on_ablog_reinitialized; /** * 获取日志输出标记 */ int get_log_print_level(); /** * 格式化输出日志 */ void log_println(const char *func, const char *file, int line, const char *format, ...); void log_println_vl(const char *func, const char *file, int line, const char *format, va_list vl); ////////////// end of ABLOG definition //////////////////////////////////////////////// /** * 返回当前进程是否处于 debug 状态 */ int debug_current_process(); /** * 释放 ab_code 占用的资源 */ void clear_coding(); /** * 虚拟当前时间 virtual_current_time * 根据环境变量指定的 VIRTUAL_TIME = `date +"%Y-%m-%d %H:%M:%S"`确定时间偏移 * 返回计算后的虚拟当前时间 * 使用此函数前必须在main开始时调用 init_coding(); */ time_t vtime(); #define MAX_FILEPATH_LENGTH 256 /** * 模式替换实例化结构 */ struct pattern_instance { char pattern[MAX_FILEPATH_LENGTH]; time_t instantiated_time; //实例化时间 time_t expiration_ct; //系统当前时间 time_t expiration_vt; //虚拟当前时间 int instance_buf_len; char instance[MAX_FILEPATH_LENGTH]; //实际可用字节数由instance_buf_len指定,并分配内存相应内存 }; void set_log_filename(char *filepath_pattern); void set_log_keephours(int keep_hours); void set_log_stdout(int yn); /** * 实例化一个模式字符串 * 模式中可以替换内容包括 * [PID] 用当前进程id * [YYYY] 虚拟当前时间的年 * [yyyy] 系统当前时间的年 * [MM] 虚拟当前时间的月 * [mm] 系统当前时间的月 * [DD] 虚拟当前时间的日 * [dd] 系统当前时间的日 * [HH] 虚拟当前时间的时 * [hh] 系统当前时间的时 * [MI] 虚拟当前时间的分 * [mi] 系统当前时间的分 * * 返回是否有新的实例产生 1有 0没有 */ int instantiated_pattern(struct pattern_instance *pi); /** * 锁文件 */ int lock_file(char* filename, int wait); /** * 解锁文件 */ int unlock_file(int lockfd); ///////////////////////////// 格式化 ///////////////////////////// char* int_string(int n); #define DEFINE_RETURN_CODE(N) set_code_string(N, #N); void set_code_string(int n, char* string); char* code_string(int n); int parse_hex(char* s, int*o, int default_value); int parse_int(char* s, int*o, int default_value); int parse_double(char* s, double*o, double default_value); char* format_int(char* format, int n); char* format_long(char* format, long n); char* format_double(char* format, double n); time_t parse_time(char *s, long *ms); struct timeval parse_time_tv(char *stime); long parse_time_lms(char *stime); char* time_string(time_t seconds); char* time_lms_string(time_long milliseconds); char* time_tv_string(struct timeval tv); ///////////////////////////// end of 格式化 ///////////////////////////// /** * 字符串分割 */ char** split_string(char* source, char split_char, char*recv_buf, int recv_buf_bytes, char**recv_buf_array, int recv_buf_array_size); /** * 分割字符串,返回下一分割段的起始位置 */ char* split_string_next(char* source, char split_char, char* recv_buf, int recv_buf_len); /** * 字符串替换 */ int replace_string(char* source, char* find, char* replace, char* recv_buf, int recv_buf_len); /** * 转换字符串 str 为大写并存入 rbuf, 返回 rbuf * rbuf的可用内存大小必须大于 strlen(str) */ char* toUpperCase(const char *str, char *rbuf); /** * 去掉字符串 str 左边的空白字符并存入 rbuf, 返回 rbuf * rbuf的可用内存大小必须大于 strlen(str) */ char* trimLeft(const char *str, char *rbuf); /** * 去掉字符串 str 右边的空白字符并存入 rbuf, 返回 rbuf * rbuf的可用内存大小必须大于 strlen(str) */ char* trimRight(const char *str, char *rbuf); /** * 去掉字符串 str 两边的空白字符并存入 rbuf, 返回 rbuf * rbuf的可用内存大小必须大于 strlen(str) */ char* trim(const char *str, char *rbuf); /** * 读mapping文件 */ long read_mapping_file(int fd); /** * 写mapping文件 */ int write_mapping_file(int fd, long mid); ///////////////////////////// ABLOG_ComponentUnit ///////////////////////////// #ifndef ABLOG_ComponentUnit #define ABLOG_ComponentUnit #ifdef _EDIT_IN_WINDOWS_ONLY #pragma GCC system_header #endif static RETURN_CODE_STRING ____RC2S____ = code_string; static char ____FunctionID____[] = ""; static int ____LP_LEVEL____ = 0xFF; static int ____LP_FLOW____ = 0xFF; #endif//ABLOG_ComponentUnit ///////////////////////////// ABLOG_ComponentUnit ///////////////////////////// #endif