diff --git a/release/inc/list.h b/release/inc/list.h index 2622d49..20c6465 100644 --- a/release/inc/list.h +++ b/release/inc/list.h @@ -12,90 +12,158 @@ #include #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /* ---- 节点 ---- */ -struct list_head { struct list_head *next, *prev; }; +struct list_head +{ + struct list_head *next; + struct list_head *prev; +}; /* ---- 初始化 ---- */ #define LIST_HEAD_INIT(n) { &(n), &(n) } #define LIST_HEAD(n) struct list_head n = LIST_HEAD_INIT(n) static inline void INIT_LIST_HEAD(struct list_head *h) -{ h->next = h; h->prev = h; } +{ + h->next = h; + h->prev = h; +} /* ---- 内部:在 prev 和 next 之间插入 node ---- */ static inline void __list_add(struct list_head *node, struct list_head *prev, struct list_head *next) -{ next->prev = node; node->next = next; node->prev = prev; prev->next = node; } +{ + next->prev = node; + node->next = next; + node->prev = prev; + prev->next = node; +} /* ---- 内部:删除 prev 和 next 之间的节点 ---- */ -static inline void __list_del(struct list_head *prev, struct list_head *next) -{ next->prev = prev; prev->next = next; } +static inline void __list_del(struct list_head *prev, + struct list_head *next) +{ + next->prev = prev; + prev->next = next; +} /* ---- 添加 ---- */ static inline void list_add(struct list_head *node, struct list_head *head) -{ __list_add(node, head, head->next); } +{ + __list_add(node, head, head->next); +} + static inline void list_add_tail(struct list_head *node, struct list_head *head) -{ __list_add(node, head->prev, head); } +{ + __list_add(node, head->prev, head); +} /* ---- 删除 ---- */ static inline void list_del(struct list_head *e) -{ __list_del(e->prev, e->next); } +{ + __list_del(e->prev, e->next); +} + static inline void list_del_init(struct list_head *e) -{ __list_del(e->prev, e->next); INIT_LIST_HEAD(e); } +{ + __list_del(e->prev, e->next); + INIT_LIST_HEAD(e); +} /* ---- 移动 ---- */ static inline void list_move(struct list_head *l, struct list_head *h) -{ __list_del(l->prev, l->next); list_add(l, h); } +{ + __list_del(l->prev, l->next); + list_add(l, h); +} + static inline void list_move_tail(struct list_head *l, struct list_head *h) -{ __list_del(l->prev, l->next); list_add_tail(l, h); } +{ + __list_del(l->prev, l->next); + list_add_tail(l, h); +} /* ---- 替换 ---- */ static inline void list_replace(struct list_head *old, struct list_head *node) -{ node->next=old->next; node->next->prev=node; node->prev=old->prev; node->prev->next=node; } -static inline void list_replace_init(struct list_head *old, struct list_head *node) -{ list_replace(old,node); INIT_LIST_HEAD(old); } +{ + node->next = old->next; + node->next->prev = node; + node->prev = old->prev; + node->prev->next = node; +} + +static inline void list_replace_init(struct list_head *old, + struct list_head *node) +{ + list_replace(old, node); + INIT_LIST_HEAD(old); +} /* ---- 判断 ---- */ static inline int list_empty(const struct list_head *h) -{ return h->next == h; } +{ + return h->next == h; +} + static inline int list_is_linked(const struct list_head *e) -{ return e->next != e; } +{ + return e->next != e; +} /* ---- 宿主指针 ---- */ #define list_entry(ptr, type, member) \ - ((type*)((char*)(ptr) - offsetof(type, member))) -#define list_first_entry(h, type, member) list_entry((h)->next, type, member) -#define list_last_entry(h, type, member) list_entry((h)->prev, type, member) + ((type *)((char *)(ptr) - offsetof(type, member))) + +#define list_first_entry(h, type, member) \ + list_entry((h)->next, type, member) + +#define list_last_entry(h, type, member) \ + list_entry((h)->prev, type, member) + #define list_first_entry_or_null(h, type, member) \ - (list_empty(h) ? NULL : list_first_entry(h, type, member)) + (list_empty(h) ? NULL : list_first_entry(h, type, member)) /* ---- 遍历 ---- */ #define list_for_each(pos, head) \ - for (pos = (head)->next; pos != (head); pos = pos->next) + for (pos = (head)->next; pos != (head); pos = pos->next) + #define list_for_each_safe(pos, n, head) \ - for (pos = (head)->next, n = pos->next; pos != (head); pos = n, n = pos->next) + for (pos = (head)->next, n = pos->next; pos != (head); \ + pos = n, n = pos->next) + #define list_for_each_entry(pos, head, member) \ - for (pos = list_first_entry(head, __typeof__(*pos), member); \ - &pos->member != (head); \ - pos = list_entry(pos->member.next, __typeof__(*pos), member)) + for (pos = list_first_entry(head, __typeof__(*pos), member); \ + &pos->member != (head); \ + pos = list_entry(pos->member.next, __typeof__(*pos), member)) + #define list_for_each_entry_safe(pos, n, head, member) \ - for (pos = list_first_entry(head, __typeof__(*pos), member), \ - n = list_entry(pos->member.next, __typeof__(*pos), member); \ - &pos->member != (head); \ - pos = n, n = list_entry(n->member.next, __typeof__(*n), member)) + for (pos = list_first_entry(head, __typeof__(*pos), member), \ + n = list_entry(pos->member.next, __typeof__(*pos), member); \ + &pos->member != (head); \ + pos = n, \ + n = list_entry(n->member.next, __typeof__(*n), member)) /* ---- 拼接 ---- */ -static inline void list_splice_init(struct list_head *src, struct list_head *dst) +static inline void list_splice_init(struct list_head *src, + struct list_head *dst) { - if (!list_empty(src)) { - struct list_head *f = src->next, *l = src->prev, *a = dst->next; - f->prev = dst; dst->next = f; l->next = a; a->prev = l; - INIT_LIST_HEAD(src); - } + if (!list_empty(src)) + { + struct list_head *f = src->next; + struct list_head *l = src->prev; + struct list_head *a = dst->next; + + f->prev = dst; + dst->next = f; + l->next = a; + a->prev = l; + INIT_LIST_HEAD(src); + } } #ifdef __cplusplus diff --git a/release/inc/myBase.h b/release/inc/myBase.h index 5b2d30c..6c06e24 100644 --- a/release/inc/myBase.h +++ b/release/inc/myBase.h @@ -18,7 +18,8 @@ #include #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /* === 终端颜色 === */ diff --git a/release/inc/myFunc.h b/release/inc/myFunc.h index ee65f44..6cd97bb 100644 --- a/release/inc/myFunc.h +++ b/release/inc/myFunc.h @@ -9,28 +9,69 @@ #include #include #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif -/* ---- 时间结构 ---- */ -typedef struct { - uint32_t ms:16, min:6, :2, hour:5, :3, day:5, week:3, month:4, :4, year:7, :1; -} time_sys_t; -typedef struct { time_t sec; time_t usec; } time_cal_t; +/* ---- 系统时间 ---- */ +typedef struct +{ + uint32_t ms : 16; + uint32_t min : 6; + uint32_t : 2; + uint32_t hour: 5; + uint32_t : 3; + uint32_t day : 5; + uint32_t week: 3; + uint32_t month:4; + uint32_t : 4; + uint32_t year: 7; + uint32_t : 1; +} stru_time_sys; -/* ---- IPC ---- */ -typedef struct { int qid, type, len; char text[4096]; } ipc_text_t; -typedef struct { long id; ipc_text_t t; } ipc_msg_t; -enum { IPC_RESP_NONE=1, IPC_RESP_ONCE, IPC_RESP_ALWAYS }; +/* ---- 日历时间 ---- */ +typedef struct +{ + time_t sec; + time_t usec; +} stru_time_cal; + +/* ---- IPC 消息文本 ---- */ +typedef struct +{ + int qid; + int type; + int len; + char text[4096]; +} stru_ipc_text; + +/* ---- IPC 响应类型 ---- */ +typedef enum +{ + IPC_RESP_NONE = 1, + IPC_RESP_ONCE, + IPC_RESP_ALWAYS +} enum_ipc_resp; + +/* ---- IPC 消息 ---- */ +typedef struct +{ + long id; + stru_ipc_text t; +} stru_ipc_msg; /* ---- 文件描述头(256字节) ---- */ #pragma pack(1) -typedef struct { - uint64_t flag; /* 魔数 0xA55AA55AA55AA55A */ - char ver[16], author[16], modify[32]; - uint16_t crc; - char desc[128], bak[54]; -} file_info_t; +typedef struct +{ + uint64_t flag; + char ver[16]; + char author[16]; + char modify[32]; + uint16_t crc; + char desc[128]; + char bak[54]; +} stru_file_info; #pragma pack() /* ===== 校验 ===== */ @@ -54,12 +95,12 @@ char *func_buf2str(char *buf, int sz, const char *src, int n); int func_str2argv(char *s, uint8_t *argc, char *argv[], uint8_t max); /* ===== 时间 ===== */ -char *func_time_sys2str(char *buf, int sz, time_sys_t *t); -int func_str2time_sys(const char *s, time_sys_t *t); -int func_get_time(void *t, int type); /* type: 1=sys 2=cal */ +char *func_time_sys2str(char *buf, int sz, stru_time_sys *t); +int func_str2time_sys(const char *s, stru_time_sys *t); +int func_get_time(void *t, int type); int func_set_time(void *t, int type); -void func_time_sys2cal(time_sys_t *src, time_cal_t *dst); -void func_time_cal2sys(time_cal_t *src, time_sys_t *dst); +void func_time_sys2cal(stru_time_sys *src, stru_time_cal *dst); +void func_time_cal2sys(stru_time_cal *src, stru_time_sys *dst); /* ===== 文件目录 ===== */ int func_dir_exist(const char *p); @@ -71,7 +112,7 @@ uint32_t func_file_size(const char *p); int func_read_file(const char *p, char *buf, uint32_t cap, uint32_t *out); uint8_t *func_read_file_alloc(const char *p, uint32_t *out); int func_write_file(const char *p, const char *buf, uint32_t n); -int func_read_file_info(const char *p, file_info_t *info, int check); +int func_read_file_info(const char *p, stru_file_info *info, int check); /* ===== 进程 ===== */ int func_proc_self_name(char *buf, uint32_t sz); @@ -105,8 +146,8 @@ int func_ipc_get_by_name(const char *name, int *qid); int func_ipc_get_by_pid(int pid, int *qid); int func_ipc_send_buf(int qid, uint8_t *tx, uint32_t n); int func_ipc_recv_buf(int qid, uint8_t *rx, uint32_t n); -int func_ipc_send_msg(int qid, ipc_msg_t *m); -int func_ipc_recv_msg(int qid, ipc_msg_t *m); +int func_ipc_send_msg(int qid, stru_ipc_msg *m); +int func_ipc_recv_msg(int qid, stru_ipc_msg *m); int func_ipc_delete(int qid); #ifdef __cplusplus diff --git a/release/inc/myLog.h b/release/inc/myLog.h index 97a3bce..d7b3d42 100644 --- a/release/inc/myLog.h +++ b/release/inc/myLog.h @@ -7,7 +7,8 @@ #define _MY_LOG_H_ #include #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif #define LOG_INFO 0 diff --git a/src/public/libfunc/inc/myFunc.h b/src/public/libfunc/inc/myFunc.h index ee65f44..6cd97bb 100644 --- a/src/public/libfunc/inc/myFunc.h +++ b/src/public/libfunc/inc/myFunc.h @@ -9,28 +9,69 @@ #include #include #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif -/* ---- 时间结构 ---- */ -typedef struct { - uint32_t ms:16, min:6, :2, hour:5, :3, day:5, week:3, month:4, :4, year:7, :1; -} time_sys_t; -typedef struct { time_t sec; time_t usec; } time_cal_t; +/* ---- 系统时间 ---- */ +typedef struct +{ + uint32_t ms : 16; + uint32_t min : 6; + uint32_t : 2; + uint32_t hour: 5; + uint32_t : 3; + uint32_t day : 5; + uint32_t week: 3; + uint32_t month:4; + uint32_t : 4; + uint32_t year: 7; + uint32_t : 1; +} stru_time_sys; -/* ---- IPC ---- */ -typedef struct { int qid, type, len; char text[4096]; } ipc_text_t; -typedef struct { long id; ipc_text_t t; } ipc_msg_t; -enum { IPC_RESP_NONE=1, IPC_RESP_ONCE, IPC_RESP_ALWAYS }; +/* ---- 日历时间 ---- */ +typedef struct +{ + time_t sec; + time_t usec; +} stru_time_cal; + +/* ---- IPC 消息文本 ---- */ +typedef struct +{ + int qid; + int type; + int len; + char text[4096]; +} stru_ipc_text; + +/* ---- IPC 响应类型 ---- */ +typedef enum +{ + IPC_RESP_NONE = 1, + IPC_RESP_ONCE, + IPC_RESP_ALWAYS +} enum_ipc_resp; + +/* ---- IPC 消息 ---- */ +typedef struct +{ + long id; + stru_ipc_text t; +} stru_ipc_msg; /* ---- 文件描述头(256字节) ---- */ #pragma pack(1) -typedef struct { - uint64_t flag; /* 魔数 0xA55AA55AA55AA55A */ - char ver[16], author[16], modify[32]; - uint16_t crc; - char desc[128], bak[54]; -} file_info_t; +typedef struct +{ + uint64_t flag; + char ver[16]; + char author[16]; + char modify[32]; + uint16_t crc; + char desc[128]; + char bak[54]; +} stru_file_info; #pragma pack() /* ===== 校验 ===== */ @@ -54,12 +95,12 @@ char *func_buf2str(char *buf, int sz, const char *src, int n); int func_str2argv(char *s, uint8_t *argc, char *argv[], uint8_t max); /* ===== 时间 ===== */ -char *func_time_sys2str(char *buf, int sz, time_sys_t *t); -int func_str2time_sys(const char *s, time_sys_t *t); -int func_get_time(void *t, int type); /* type: 1=sys 2=cal */ +char *func_time_sys2str(char *buf, int sz, stru_time_sys *t); +int func_str2time_sys(const char *s, stru_time_sys *t); +int func_get_time(void *t, int type); int func_set_time(void *t, int type); -void func_time_sys2cal(time_sys_t *src, time_cal_t *dst); -void func_time_cal2sys(time_cal_t *src, time_sys_t *dst); +void func_time_sys2cal(stru_time_sys *src, stru_time_cal *dst); +void func_time_cal2sys(stru_time_cal *src, stru_time_sys *dst); /* ===== 文件目录 ===== */ int func_dir_exist(const char *p); @@ -71,7 +112,7 @@ uint32_t func_file_size(const char *p); int func_read_file(const char *p, char *buf, uint32_t cap, uint32_t *out); uint8_t *func_read_file_alloc(const char *p, uint32_t *out); int func_write_file(const char *p, const char *buf, uint32_t n); -int func_read_file_info(const char *p, file_info_t *info, int check); +int func_read_file_info(const char *p, stru_file_info *info, int check); /* ===== 进程 ===== */ int func_proc_self_name(char *buf, uint32_t sz); @@ -105,8 +146,8 @@ int func_ipc_get_by_name(const char *name, int *qid); int func_ipc_get_by_pid(int pid, int *qid); int func_ipc_send_buf(int qid, uint8_t *tx, uint32_t n); int func_ipc_recv_buf(int qid, uint8_t *rx, uint32_t n); -int func_ipc_send_msg(int qid, ipc_msg_t *m); -int func_ipc_recv_msg(int qid, ipc_msg_t *m); +int func_ipc_send_msg(int qid, stru_ipc_msg *m); +int func_ipc_recv_msg(int qid, stru_ipc_msg *m); int func_ipc_delete(int qid); #ifdef __cplusplus diff --git a/src/public/libfunc/src/myFunc.c b/src/public/libfunc/src/myFunc.c index 40c23ed..af65a91 100644 --- a/src/public/libfunc/src/myFunc.c +++ b/src/public/libfunc/src/myFunc.c @@ -21,588 +21,1075 @@ * 1. 校验计算 * ================================================================= */ -/* CRC16 查找表 (Modbus 多项式 0xA001) */ LOCAL const uint16_t _crc16_tab[256] = { - 0x0000,0xc0c1,0xc181,0x0140,0xc301,0x03c0,0x0280,0xc241,0xc601,0x06c0,0x0780,0xc741,0x0500,0xc5c1,0xc481,0x0440, - 0xcc01,0x0cc0,0x0d80,0xcd41,0x0f00,0xcfc1,0xce81,0x0e40,0x0a00,0xcac1,0xcb81,0x0b40,0xc901,0x09c0,0x0880,0xc841, - 0xd801,0x18c0,0x1980,0xd941,0x1b00,0xdbc1,0xda81,0x1a40,0x1e00,0xdec1,0xdf81,0x1f40,0xdd01,0x1dc0,0x1c80,0xdc41, - 0x1400,0xd4c1,0xd581,0x1540,0xd701,0x17c0,0x1680,0xd641,0xd201,0x12c0,0x1380,0xd341,0x1100,0xd1c1,0xd081,0x1040, - 0xf001,0x30c0,0x3180,0xf141,0x3300,0xf3c1,0xf281,0x3240,0x3600,0xf6c1,0xf781,0x3740,0xf501,0x35c0,0x3480,0xf441, - 0x3c00,0xfcc1,0xfd81,0x3d40,0xff01,0x3fc0,0x3e80,0xfe41,0xfa01,0x3ac0,0x3b80,0xfb41,0x3900,0xf9c1,0xf881,0x3840, - 0x2800,0xe8c1,0xe981,0x2940,0xeb01,0x2bc0,0x2a80,0xea41,0xee01,0x2ec0,0x2f80,0xef41,0x2d00,0xedc1,0xec81,0x2c40, - 0xe401,0x24c0,0x2580,0xe541,0x2700,0xe7c1,0xe681,0x2640,0x2200,0xe2c1,0xe381,0x2340,0xe101,0x21c0,0x2080,0xe041, - 0xa001,0x60c0,0x6180,0xa141,0x6300,0xa3c1,0xa281,0x6240,0x6600,0xa6c1,0xa781,0x6740,0xa501,0x65c0,0x6480,0xa441, - 0x6c00,0xacc1,0xad81,0x6d40,0xaf01,0x6fc0,0x6e80,0xae41,0xaa01,0x6ac0,0x6b80,0xab41,0x6900,0xa9c1,0xa881,0x6840, - 0x7800,0xb8c1,0xb981,0x7940,0xbb01,0x7bc0,0x7a80,0xba41,0xbe01,0x7ec0,0x7f80,0xbf41,0x7d00,0xbdc1,0xbc81,0x7c40, - 0xb401,0x74c0,0x7580,0xb541,0x7700,0xb7c1,0xb681,0x7640,0x7200,0xb2c1,0xb381,0x7340,0xb101,0x71c0,0x7080,0xb041, - 0x5000,0x90c1,0x9181,0x5140,0x9301,0x53c0,0x5280,0x9241,0x9601,0x56c0,0x5780,0x9741,0x5500,0x95c1,0x9481,0x5440, - 0x9c01,0x5cc0,0x5d80,0x9d41,0x5f00,0x9fc1,0x9e81,0x5e40,0x5a00,0x9ac1,0x9b81,0x5b40,0x9901,0x59c0,0x5880,0x9841, - 0x8801,0x48c0,0x4980,0x8941,0x4b00,0x8bc1,0x8a81,0x4a40,0x4e00,0x8ec1,0x8f81,0x4f40,0x8d01,0x4dc0,0x4c80,0x8c41, - 0x4400,0x84c1,0x8581,0x4540,0x8701,0x47c0,0x4680,0x8641,0x8201,0x42c0,0x4380,0x8341,0x4100,0x81c1,0x8081,0x4040, + 0x0000,0xc0c1,0xc181,0x0140,0xc301,0x03c0,0x0280,0xc241,0xc601,0x06c0,0x0780,0xc741,0x0500,0xc5c1,0xc481,0x0440, + 0xcc01,0x0cc0,0x0d80,0xcd41,0x0f00,0xcfc1,0xce81,0x0e40,0x0a00,0xcac1,0xcb81,0x0b40,0xc901,0x09c0,0x0880,0xc841, + 0xd801,0x18c0,0x1980,0xd941,0x1b00,0xdbc1,0xda81,0x1a40,0x1e00,0xdec1,0xdf81,0x1f40,0xdd01,0x1dc0,0x1c80,0xdc41, + 0x1400,0xd4c1,0xd581,0x1540,0xd701,0x17c0,0x1680,0xd641,0xd201,0x12c0,0x1380,0xd341,0x1100,0xd1c1,0xd081,0x1040, + 0xf001,0x30c0,0x3180,0xf141,0x3300,0xf3c1,0xf281,0x3240,0x3600,0xf6c1,0xf781,0x3740,0xf501,0x35c0,0x3480,0xf441, + 0x3c00,0xfcc1,0xfd81,0x3d40,0xff01,0x3fc0,0x3e80,0xfe41,0xfa01,0x3ac0,0x3b80,0xfb41,0x3900,0xf9c1,0xf881,0x3840, + 0x2800,0xe8c1,0xe981,0x2940,0xeb01,0x2bc0,0x2a80,0xea41,0xee01,0x2ec0,0x2f80,0xef41,0x2d00,0xedc1,0xec81,0x2c40, + 0xe401,0x24c0,0x2580,0xe541,0x2700,0xe7c1,0xe681,0x2640,0x2200,0xe2c1,0xe381,0x2340,0xe101,0x21c0,0x2080,0xe041, + 0xa001,0x60c0,0x6180,0xa141,0x6300,0xa3c1,0xa281,0x6240,0x6600,0xa6c1,0xa781,0x6740,0xa501,0x65c0,0x6480,0xa441, + 0x6c00,0xacc1,0xad81,0x6d40,0xaf01,0x6fc0,0x6e80,0xae41,0xaa01,0x6ac0,0x6b80,0xab41,0x6900,0xa9c1,0xa881,0x6840, + 0x7800,0xb8c1,0xb981,0x7940,0xbb01,0x7bc0,0x7a80,0xba41,0xbe01,0x7ec0,0x7f80,0xbf41,0x7d00,0xbdc1,0xbc81,0x7c40, + 0xb401,0x74c0,0x7580,0xb541,0x7700,0xb7c1,0xb681,0x7640,0x7200,0xb2c1,0xb381,0x7340,0xb101,0x71c0,0x7080,0xb041, + 0x5000,0x90c1,0x9181,0x5140,0x9301,0x53c0,0x5280,0x9241,0x9601,0x56c0,0x5780,0x9741,0x5500,0x95c1,0x9481,0x5440, + 0x9c01,0x5cc0,0x5d80,0x9d41,0x5f00,0x9fc1,0x9e81,0x5e40,0x5a00,0x9ac1,0x9b81,0x5b40,0x9901,0x59c0,0x5880,0x9841, + 0x8801,0x48c0,0x4980,0x8941,0x4b00,0x8bc1,0x8a81,0x4a40,0x4e00,0x8ec1,0x8f81,0x4f40,0x8d01,0x4dc0,0x4c80,0x8c41, + 0x4400,0x84c1,0x8581,0x4540,0x8701,0x47c0,0x4680,0x8641,0x8201,0x42c0,0x4380,0x8341,0x4100,0x81c1,0x8081,0x4040, }; -/* CRC32 查找表 */ LOCAL const uint32_t _crc32_tab[256] = { - 0x00000000,0x77073096,0xEE0E612C,0x990951BA,0x076DC419,0x706AF48F,0xE963A535,0x9E6495A3, - 0x0EDB8832,0x79DCB8A4,0xE0D5E91E,0x97D2D988,0x09B64C2B,0x7EB17CBD,0xE7B82D07,0x90BF1D91, - 0x1DB71064,0x6AB020F2,0xF3B97148,0x84BE41DE,0x1ADAD47D,0x6DDDE4EB,0xF4D4B551,0x83D385C7, - 0x136C9856,0x646BA8C0,0xFD62F97A,0x8A65C9EC,0x14015C4F,0x63066CD9,0xFA0F3D63,0x8D080DF5, - 0x3B6E20C8,0x4C69105E,0xD56041E4,0xA2677172,0x3C03E4D1,0x4B04D447,0xD20D85FD,0xA50AB56B, - 0x35B5A8FA,0x42B2986C,0xDBBBC9D6,0xACBCF940,0x32D86CE3,0x45DF5C75,0xDCD60DCF,0xABD13D59, - 0x26D930AC,0x51DE003A,0xC8D75180,0xBFD06116,0x21B4F4B5,0x56B3C423,0xCFBA9599,0xB8BDA50F, - 0x2802B89E,0x5F058808,0xC60CD9B2,0xB10BE924,0x2F6F7C87,0x58684C11,0xC1611DAB,0xB6662D3D, - 0x76DC4190,0x01DB7106,0x98D220BC,0xEFD5102A,0x71B18589,0x06B6B51F,0x9FBFE4A5,0xE8B8D433, - 0x7807C9A2,0x0F00F934,0x9609A88E,0xE10E9818,0x7F6A0DBB,0x086D3D2D,0x91646C97,0xE6635C01, - 0x6B6B51F4,0x1C6C6162,0x856530D8,0xF262004E,0x6C0695ED,0x1B01A57B,0x8208F4C1,0xF50FC457, - 0x65B0D9C6,0x12B7E950,0x8BBEB8EA,0xFCB9887C,0x62DD1DDF,0x15DA2D49,0x8CD37CF3,0xFBD44C65, - 0x4DB26158,0x3AB551CE,0xA3BC0074,0xD4BB30E2,0x4ADFA541,0x3DD895D7,0xA4D1C46D,0xD3D6F4FB, - 0x4369E96A,0x346ED9FC,0xAD678846,0xDA60B8D0,0x44042D73,0x33031DE5,0xAA0A4C5F,0xDD0D7CC9, - 0x5005713C,0x270241AA,0xBE0B1010,0xC90C2086,0x5768B525,0x206F85B3,0xB966D409,0xCE61E49F, - 0x5EDEF90E,0x29D9C998,0xB0D09822,0xC7D7A8B4,0x59B33D17,0x2EB40D81,0xB7BD5C3B,0xC0BA6CAD, - 0xEDB88320,0x9ABFB3B6,0x03B6E20C,0x74B1D29A,0xEAD54739,0x9DD277AF,0x04DB2615,0x73DC1683, - 0xE3630B12,0x94643B84,0x0D6D6A3E,0x7A6A5AA8,0xE40ECF0B,0x9309FF9D,0x0A00AE27,0x7D079EB1, - 0xF00F9344,0x8708A3D2,0x1E01F268,0x6906C2FE,0xF762575D,0x806567CB,0x196C3671,0x6E6B06E7, - 0xFED41B76,0x89D32BE0,0x10DA7A5A,0x67DD4ACC,0xF9B9DF6F,0x8EBEEFF9,0x17B7BE43,0x60B08ED5, - 0xD6D6A3E8,0xA1D1937E,0x38D8C2C4,0x4FDFF252,0xD1BB67F1,0xA6BC5767,0x3FB506DD,0x48B2364B, - 0xD80D2BDA,0xAF0A1B4C,0x36034AF6,0x41047A60,0xDF60EFC3,0xA867DF55,0x316E8EEF,0x4669BE79, - 0xCB61B38C,0xBC66831A,0x256FD2A0,0x5268E236,0xCC0C7795,0xBB0B4703,0x220216B9,0x5505262F, - 0xC5BA3BBE,0xB2BD0B28,0x2BB45A92,0x5CB36A04,0xC2D7FFA7,0xB5D0CF31,0x2CD99E8B,0x5BDEAE1D, - 0x9B64C2B0,0xEC63F226,0x756AA39C,0x026D930A,0x9C0906A9,0xEB0E363F,0x72076785,0x05005713, - 0x95BF4A82,0xE2B87A14,0x7BB12BAE,0x0CB61B38,0x92D28E9B,0xE5D5BE0D,0x7CDCEFB7,0x0BDBDF21, - 0x86D3D2D4,0xF1D4E242,0x68DDB3F8,0x1FDA836E,0x81BE16CD,0xF6B9265B,0x6FB077E1,0x18B74777, - 0x88085AE6,0xFF0F6A70,0x66063BCA,0x11010B5C,0x8F659EFF,0xF862AE69,0x616BFFD3,0x166CCF45, - 0xA00AE278,0xD70DD2EE,0x4E048354,0x3903B3C2,0xA7672661,0xD06016F7,0x4969474D,0x3E6E77DB, - 0xAED16A4A,0xD9D65ADC,0x40DF0B66,0x37D83BF0,0xA9BCAE53,0xDEBB9EC5,0x47B2CF7F,0x30B5FFE9, - 0xBDBDF21C,0xCABAC28A,0x53B39330,0x24B4A3A6,0xBAD03605,0xCDD70693,0x54DE5729,0x23D967BF, - 0xB3667A2E,0xC4614AB8,0x5D681B02,0x2A6F2B94,0xB40BBE37,0xC30C8EA1,0x5A05DF1B,0x2D02EF8D, + 0x00000000,0x77073096,0xEE0E612C,0x990951BA,0x076DC419,0x706AF48F,0xE963A535,0x9E6495A3, + 0x0EDB8832,0x79DCB8A4,0xE0D5E91E,0x97D2D988,0x09B64C2B,0x7EB17CBD,0xE7B82D07,0x90BF1D91, + 0x1DB71064,0x6AB020F2,0xF3B97148,0x84BE41DE,0x1ADAD47D,0x6DDDE4EB,0xF4D4B551,0x83D385C7, + 0x136C9856,0x646BA8C0,0xFD62F97A,0x8A65C9EC,0x14015C4F,0x63066CD9,0xFA0F3D63,0x8D080DF5, + 0x3B6E20C8,0x4C69105E,0xD56041E4,0xA2677172,0x3C03E4D1,0x4B04D447,0xD20D85FD,0xA50AB56B, + 0x35B5A8FA,0x42B2986C,0xDBBBC9D6,0xACBCF940,0x32D86CE3,0x45DF5C75,0xDCD60DCF,0xABD13D59, + 0x26D930AC,0x51DE003A,0xC8D75180,0xBFD06116,0x21B4F4B5,0x56B3C423,0xCFBA9599,0xB8BDA50F, + 0x2802B89E,0x5F058808,0xC60CD9B2,0xB10BE924,0x2F6F7C87,0x58684C11,0xC1611DAB,0xB6662D3D, + 0x76DC4190,0x01DB7106,0x98D220BC,0xEFD5102A,0x71B18589,0x06B6B51F,0x9FBFE4A5,0xE8B8D433, + 0x7807C9A2,0x0F00F934,0x9609A88E,0xE10E9818,0x7F6A0DBB,0x086D3D2D,0x91646C97,0xE6635C01, + 0x6B6B51F4,0x1C6C6162,0x856530D8,0xF262004E,0x6C0695ED,0x1B01A57B,0x8208F4C1,0xF50FC457, + 0x65B0D9C6,0x12B7E950,0x8BBEB8EA,0xFCB9887C,0x62DD1DDF,0x15DA2D49,0x8CD37CF3,0xFBD44C65, + 0x4DB26158,0x3AB551CE,0xA3BC0074,0xD4BB30E2,0x4ADFA541,0x3DD895D7,0xA4D1C46D,0xD3D6F4FB, + 0x4369E96A,0x346ED9FC,0xAD678846,0xDA60B8D0,0x44042D73,0x33031DE5,0xAA0A4C5F,0xDD0D7CC9, + 0x5005713C,0x270241AA,0xBE0B1010,0xC90C2086,0x5768B525,0x206F85B3,0xB966D409,0xCE61E49F, + 0x5EDEF90E,0x29D9C998,0xB0D09822,0xC7D7A8B4,0x59B33D17,0x2EB40D81,0xB7BD5C3B,0xC0BA6CAD, + 0xEDB88320,0x9ABFB3B6,0x03B6E20C,0x74B1D29A,0xEAD54739,0x9DD277AF,0x04DB2615,0x73DC1683, + 0xE3630B12,0x94643B84,0x0D6D6A3E,0x7A6A5AA8,0xE40ECF0B,0x9309FF9D,0x0A00AE27,0x7D079EB1, + 0xF00F9344,0x8708A3D2,0x1E01F268,0x6906C2FE,0xF762575D,0x806567CB,0x196C3671,0x6E6B06E7, + 0xFED41B76,0x89D32BE0,0x10DA7A5A,0x67DD4ACC,0xF9B9DF6F,0x8EBEEFF9,0x17B7BE43,0x60B08ED5, + 0xD6D6A3E8,0xA1D1937E,0x38D8C2C4,0x4FDFF252,0xD1BB67F1,0xA6BC5767,0x3FB506DD,0x48B2364B, + 0xD80D2BDA,0xAF0A1B4C,0x36034AF6,0x41047A60,0xDF60EFC3,0xA867DF55,0x316E8EEF,0x4669BE79, + 0xCB61B38C,0xBC66831A,0x256FD2A0,0x5268E236,0xCC0C7795,0xBB0B4703,0x220216B9,0x5505262F, + 0xC5BA3BBE,0xB2BD0B28,0x2BB45A92,0x5CB36A04,0xC2D7FFA7,0xB5D0CF31,0x2CD99E8B,0x5BDEAE1D, + 0x9B64C2B0,0xEC63F226,0x756AA39C,0x026D930A,0x9C0906A9,0xEB0E363F,0x72076785,0x05005713, + 0x95BF4A82,0xE2B87A14,0x7BB12BAE,0x0CB61B38,0x92D28E9B,0xE5D5BE0D,0x7CDCEFB7,0x0BDBDF21, + 0x86D3D2D4,0xF1D4E242,0x68DDB3F8,0x1FDA836E,0x81BE16CD,0xF6B9265B,0x6FB077E1,0x18B74777, + 0x88085AE6,0xFF0F6A70,0x66063BCA,0x11010B5C,0x8F659EFF,0xF862AE69,0x616BFFD3,0x166CCF45, + 0xA00AE278,0xD70DD2EE,0x4E048354,0x3903B3C2,0xA7672661,0xD06016F7,0x4969474D,0x3E6E77DB, + 0xAED16A4A,0xD9D65ADC,0x40DF0B66,0x37D83BF0,0xA9BCAE53,0xDEBB9EC5,0x47B2CF7F,0x30B5FFE9, + 0xBDBDF21C,0xCABAC28A,0x53B39330,0x24B4A3A6,0xBAD03605,0xCDD70693,0x54DE5729,0x23D967BF, + 0xB3667A2E,0xC4614AB8,0x5D681B02,0x2A6F2B94,0xB40BBE37,0xC30C8EA1,0x5A05DF1B,0x2D02EF8D, }; -uint8_t func_cal_sum_byte(const uint8_t *d, uint32_t n) { - if (!d || !n) return 0; - uint8_t s = 0; - for (uint32_t i = 0; i < n; i++) s += d[i]; - return s; +uint8_t func_cal_sum_byte(const uint8_t *d, uint32_t n) +{ + if (!d || !n) + { + return 0; + } + uint8_t s = 0; + for (uint32_t i = 0; i < n; i++) + { + s += d[i]; + } + return s; } -uint16_t func_cal_sum_word(const uint8_t *d, uint32_t n) { - if (!d || !n) return 0; - uint16_t s = 0; - for (uint32_t i = 0; i < n; i++) s += d[i]; - return s; +uint16_t func_cal_sum_word(const uint8_t *d, uint32_t n) +{ + if (!d || !n) + { + return 0; + } + uint16_t s = 0; + for (uint32_t i = 0; i < n; i++) + { + s += d[i]; + } + return s; } -uint16_t func_cal_crc16(const uint8_t *d, uint32_t n) { - if (!d || !n) return 0; - uint16_t crc = 0xFFFF; - for (uint32_t i = 0; i < n; i++) - crc = (crc >> 8) ^ _crc16_tab[(uint8_t)(crc ^ d[i])]; - return crc; +uint16_t func_cal_crc16(const uint8_t *d, uint32_t n) +{ + if (!d || !n) + { + return 0; + } + uint16_t crc = 0xFFFF; + for (uint32_t i = 0; i < n; i++) + { + crc = (crc >> 8) ^ _crc16_tab[(uint8_t)(crc ^ d[i])]; + } + return crc; } -uint32_t func_cal_crc32(const uint8_t *d, uint32_t n) { - if (!d || !n) return 0; - uint32_t crc = 0xFFFFFFFF; - for (uint32_t i = 0; i < n; i++) - crc = (crc >> 8) ^ _crc32_tab[(uint8_t)(crc ^ d[i])]; - return crc ^ 0xFFFFFFFF; +uint32_t func_cal_crc32(const uint8_t *d, uint32_t n) +{ + if (!d || !n) + { + return 0; + } + uint32_t crc = 0xFFFFFFFF; + for (uint32_t i = 0; i < n; i++) + { + crc = (crc >> 8) ^ _crc32_tab[(uint8_t)(crc ^ d[i])]; + } + return crc ^ 0xFFFFFFFF; } -uint16_t func_cal_file_crc16(const char *path) { - if (!path) return 0; - FILE *fp = fopen(path, "rb"); - if (!fp) return 0; - uint16_t crc = 0xFFFF; - uint8_t buf[4096]; size_t n; - while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) - for (size_t i = 0; i < n; i++) - crc = (crc >> 8) ^ _crc16_tab[(uint8_t)(crc ^ buf[i])]; - fclose(fp); - return crc; +uint16_t func_cal_file_crc16(const char *path) +{ + if (!path) + { + return 0; + } + FILE *fp = fopen(path, "rb"); + if (!fp) + { + return 0; + } + uint16_t crc = 0xFFFF; + uint8_t buf[4096]; + size_t n; + while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) + { + for (size_t i = 0; i < n; i++) + { + crc = (crc >> 8) ^ _crc16_tab[(uint8_t)(crc ^ buf[i])]; + } + } + fclose(fp); + return crc; } -uint32_t func_cal_file_crc32(const char *path) { - if (!path) return 0; - FILE *fp = fopen(path, "rb"); - if (!fp) return 0; - uint32_t crc = 0xFFFFFFFF; - uint8_t buf[4096]; size_t n; - while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) - for (size_t i = 0; i < n; i++) - crc = (crc >> 8) ^ _crc32_tab[(uint8_t)(crc ^ buf[i])]; - fclose(fp); - return crc ^ 0xFFFFFFFF; +uint32_t func_cal_file_crc32(const char *path) +{ + if (!path) + { + return 0; + } + FILE *fp = fopen(path, "rb"); + if (!fp) + { + return 0; + } + uint32_t crc = 0xFFFFFFFF; + uint8_t buf[4096]; + size_t n; + while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) + { + for (size_t i = 0; i < n; i++) + { + crc = (crc >> 8) ^ _crc32_tab[(uint8_t)(crc ^ buf[i])]; + } + } + fclose(fp); + return crc ^ 0xFFFFFFFF; } /* ================================================================= * 2. 字符串转换 * ================================================================= */ -uint8_t func_hex_ch(char c) { - if (c >= '0' && c <= '9') return (uint8_t)(c - '0'); - if (c >= 'a' && c <= 'f') return (uint8_t)(c - 'a' + 10); - if (c >= 'A' && c <= 'F') return (uint8_t)(c - 'A' + 10); - return 0; +uint8_t func_hex_ch(char c) +{ + if (c >= '0' && c <= '9') + { + return (uint8_t)(c - '0'); + } + if (c >= 'a' && c <= 'f') + { + return (uint8_t)(c - 'a' + 10); + } + if (c >= 'A' && c <= 'F') + { + return (uint8_t)(c - 'A' + 10); + } + return 0; } -uint8_t func_dec_ch(char c) { - if (c >= '0' && c <= '9') return (uint8_t)(c - '0'); - return 0; +uint8_t func_dec_ch(char c) +{ + if (c >= '0' && c <= '9') + { + return (uint8_t)(c - '0'); + } + return 0; } -/* 十六进制字符串 "FF" → int 255 */ -int func_hex2dec(char *s) { - if (!s) return 0; - int v = 0; - while (*s) { v = (v << 4) | func_hex_ch(*s); s++; } - return v; +int func_hex2dec(char *s) +{ + if (!s) + { + return 0; + } + int v = 0; + while (*s) + { + v = (v << 4) | func_hex_ch(*s); + s++; + } + return v; } -/* 十进制字符串 "255" → int 255 */ -int func_dec2dec(char *s) { - if (!s) return 0; - int v = 0, sign = 1; - if (*s == '-') { sign = -1; s++; } - while (*s >= '0' && *s <= '9') { v = v * 10 + (*s - '0'); s++; } - return v * sign; +int func_dec2dec(char *s) +{ + if (!s) + { + return 0; + } + int v = 0; + int sign = 1; + if (*s == '-') + { + sign = -1; + s++; + } + while (*s >= '0' && *s <= '9') + { + v = v * 10 + (*s - '0'); + s++; + } + return v * sign; } -/* 十六进制字符串 "0102FF" → 字节缓冲 {0x01,0x02,0xFF} */ -int func_hex2buf(const char *s, char *dst, int *len) { - if (!s || !dst || !len) return -1; - int n = (int)strlen(s); - if (n & 1) return -1; /* 奇数长度报错 */ - for (int i = 0; i < n / 2; i++) - dst[i] = (char)((func_hex_ch(s[i*2]) << 4) | func_hex_ch(s[i*2+1])); - *len = n / 2; - return 0; +int func_hex2buf(const char *s, char *dst, int *len) +{ + if (!s || !dst || !len) + { + return -1; + } + int n = (int)strlen(s); + if (n & 1) + { + return -1; + } + for (int i = 0; i < n / 2; i++) + { + dst[i] = (char)((func_hex_ch(s[i * 2]) << 4) | func_hex_ch(s[i * 2 + 1])); + } + *len = n / 2; + return 0; } -/* int → 十进制字符串,右对齐,不足 width 左补 0 */ -char *func_dec2str(char *buf, int sz, int val, int width) { - if (!buf || sz < 2) return NULL; - char fmt[16]; - snprintf(fmt, sizeof(fmt), "%%0%dd", width); - snprintf(buf, sz, fmt, val); - return buf; +char *func_dec2str(char *buf, int sz, int val, int width) +{ + if (!buf || sz < 2) + { + return NULL; + } + char fmt[16]; + snprintf(fmt, sizeof(fmt), "%%0%dd", width); + snprintf(buf, sz, fmt, val); + return buf; } -/* int → 十六进制字符串,width=0 时不补零 */ -char *func_hex2str(char *buf, int sz, int val, int width) { - if (!buf || sz < 2) return NULL; - if (width) - snprintf(buf, sz, "%0*X", width, val); - else - snprintf(buf, sz, "%X", val); - return buf; +char *func_hex2str(char *buf, int sz, int val, int width) +{ + if (!buf || sz < 2) + { + return NULL; + } + if (width) + { + snprintf(buf, sz, "%0*X", width, val); + } + else + { + snprintf(buf, sz, "%X", val); + } + return buf; } -/* float → 字符串 */ -char *func_float2str(char *buf, int sz, float f, int width) { - if (!buf || sz < 2) return NULL; - if (width) snprintf(buf, sz, "%*.*f", width, width > 2 ? width - 2 : 1, f); - else snprintf(buf, sz, "%.6g", (double)f); - return buf; +char *func_float2str(char *buf, int sz, float f, int width) +{ + if (!buf || sz < 2) + { + return NULL; + } + if (width) + { + snprintf(buf, sz, "%*.*f", width, width > 2 ? width - 2 : 1, f); + } + else + { + snprintf(buf, sz, "%.6g", (double)f); + } + return buf; } -/* 字节数组 → 十六进制字符串 */ -char *func_buf2str(char *buf, int sz, const char *src, int n) { - if (!buf || sz < 2 || !src) return NULL; - int pos = 0; - for (int i = 0; i < n && pos + 3 < sz; i++) - pos += snprintf(buf + pos, sz - pos, "%02X", (unsigned char)src[i]); - return buf; +char *func_buf2str(char *buf, int sz, const char *src, int n) +{ + if (!buf || sz < 2 || !src) + { + return NULL; + } + int pos = 0; + for (int i = 0; i < n && pos + 3 < sz; i++) + { + pos += snprintf(buf + pos, sz - pos, "%02X", (unsigned char)src[i]); + } + return buf; } -/* 字符串分割成 argc/argv (空格分隔) */ -int func_str2argv(char *s, uint8_t *argc, char *argv[], uint8_t max) { - if (!s || !argc || !argv) return -1; - *argc = 0; - while (*s && *argc < max) { - while (*s && isspace((unsigned char)*s)) s++; - if (!*s) break; - argv[(*argc)++] = s; - while (*s && !isspace((unsigned char)*s)) s++; - if (*s) { *s = '\0'; s++; } - } - return 0; +int func_str2argv(char *s, uint8_t *argc, char *argv[], uint8_t max) +{ + if (!s || !argc || !argv) + { + return -1; + } + *argc = 0; + while (*s && *argc < max) + { + while (*s && isspace((unsigned char)*s)) + { + s++; + } + if (!*s) + { + break; + } + argv[(*argc)++] = s; + while (*s && !isspace((unsigned char)*s)) + { + s++; + } + if (*s) + { + *s = '\0'; + s++; + } + } + return 0; } /* ================================================================= * 3. 时间处理 * ================================================================= */ -char *func_time_sys2str(char *buf, int sz, time_sys_t *t) { - if (!buf || sz < 20 || !t) return NULL; - snprintf(buf, sz, "%04u-%02u-%02u %02u:%02u:%02u.%03u", - t->year, t->month, t->day, - t->hour, t->min, t->ms / 1000, - t->ms % 1000); - return buf; +char *func_time_sys2str(char *buf, int sz, stru_time_sys *t) +{ + if (!buf || sz < 20 || !t) + { + return NULL; + } + snprintf(buf, sz, "%04u-%02u-%02u %02u:%02u:%02u.%03u", + t->year, t->month, t->day, + t->hour, t->min, t->ms / 1000, + t->ms % 1000); + return buf; } -int func_str2time_sys(const char *s, time_sys_t *t) { - if (!s || !t) return -1; - memset(t, 0, sizeof(*t)); - uint32_t y, mo, d, h, mi, sec, ms = 0; - if (sscanf(s, "%u-%u-%u %u:%u:%u.%u", - &y, &mo, &d, &h, &mi, &sec, &ms) < 6) return -1; - t->year = y; t->month = mo; t->day = d; - t->hour = h; t->min = mi; t->ms = (uint16_t)(sec * 1000 + ms); - return 0; +int func_str2time_sys(const char *s, stru_time_sys *t) +{ + if (!s || !t) + { + return -1; + } + memset(t, 0, sizeof(*t)); + uint32_t y, mo, d, h, mi, sec, ms = 0; + if (sscanf(s, "%u-%u-%u %u:%u:%u.%u", + &y, &mo, &d, &h, &mi, &sec, &ms) < 6) + { + return -1; + } + t->year = y; + t->month = mo; + t->day = d; + t->hour = h; + t->min = mi; + t->ms = (uint16_t)(sec * 1000 + ms); + return 0; } -int func_get_time(void *t, int type) { - if (!t) return -1; - struct timeval tv; - gettimeofday(&tv, NULL); - if (type == 1) { /* sys */ - time_sys_t *ts = (time_sys_t *)t; - struct tm *g = localtime(&tv.tv_sec); - if (!g) return -1; - ts->year = (uint32_t)(g->tm_year + 1900); - ts->month = (uint32_t)(g->tm_mon + 1); - ts->day = (uint32_t)g->tm_mday; - ts->hour = (uint32_t)g->tm_hour; - ts->min = (uint32_t)g->tm_min; - ts->ms = (uint16_t)(g->tm_sec * 1000 + tv.tv_usec / 1000); - } else { /* cal */ - time_cal_t *tc = (time_cal_t *)t; - tc->sec = tv.tv_sec; - tc->usec = tv.tv_usec; - } - return 0; +int func_get_time(void *t, int type) +{ + if (!t) + { + return -1; + } + struct timeval tv; + gettimeofday(&tv, NULL); + if (type == 1) + { + stru_time_sys *ts = (stru_time_sys *)t; + struct tm *g = localtime(&tv.tv_sec); + if (!g) + { + return -1; + } + ts->year = (uint32_t)(g->tm_year + 1900); + ts->month = (uint32_t)(g->tm_mon + 1); + ts->day = (uint32_t)g->tm_mday; + ts->hour = (uint32_t)g->tm_hour; + ts->min = (uint32_t)g->tm_min; + ts->ms = (uint16_t)(g->tm_sec * 1000 + tv.tv_usec / 1000); + } + else + { + stru_time_cal *tc = (stru_time_cal *)t; + tc->sec = tv.tv_sec; + tc->usec = tv.tv_usec; + } + return 0; } -int func_set_time(void *t, int type) { - if (!t) return -1; - struct timeval tv; - if (type == 1) { - time_sys_t *ts = (time_sys_t *)t; - struct tm g = {0}; - g.tm_year = (int)ts->year - 1900; g.tm_mon = (int)ts->month - 1; - g.tm_mday = (int)ts->day; g.tm_hour = (int)ts->hour; - g.tm_min = (int)ts->min; g.tm_sec = (int)(ts->ms / 1000); - tv.tv_sec = mktime(&g); - tv.tv_usec = (ts->ms % 1000) * 1000; - } else { - time_cal_t *tc = (time_cal_t *)t; - tv.tv_sec = tc->sec; tv.tv_usec = tc->usec; - } - return settimeofday(&tv, NULL); +int func_set_time(void *t, int type) +{ + if (!t) + { + return -1; + } + struct timeval tv; + if (type == 1) + { + stru_time_sys *ts = (stru_time_sys *)t; + struct tm g = {0}; + g.tm_year = (int)ts->year - 1900; + g.tm_mon = (int)ts->month - 1; + g.tm_mday = (int)ts->day; + g.tm_hour = (int)ts->hour; + g.tm_min = (int)ts->min; + g.tm_sec = (int)(ts->ms / 1000); + tv.tv_sec = mktime(&g); + tv.tv_usec = (ts->ms % 1000) * 1000; + } + else + { + stru_time_cal *tc = (stru_time_cal *)t; + tv.tv_sec = tc->sec; + tv.tv_usec = tc->usec; + } + return settimeofday(&tv, NULL); } -void func_time_sys2cal(time_sys_t *src, time_cal_t *dst) { - if (!src || !dst) return; - struct tm g = {0}; - g.tm_year=(int)src->year-1900; g.tm_mon=(int)src->month-1; - g.tm_mday=(int)src->day; g.tm_hour=(int)src->hour; - g.tm_min=(int)src->min; g.tm_sec=(int)(src->ms/1000); - dst->sec = mktime(&g); - dst->usec = (src->ms % 1000) * 1000; +void func_time_sys2cal(stru_time_sys *src, stru_time_cal *dst) +{ + if (!src || !dst) + { + return; + } + struct tm g = {0}; + g.tm_year = (int)src->year - 1900; + g.tm_mon = (int)src->month - 1; + g.tm_mday = (int)src->day; + g.tm_hour = (int)src->hour; + g.tm_min = (int)src->min; + g.tm_sec = (int)(src->ms / 1000); + dst->sec = mktime(&g); + dst->usec = (src->ms % 1000) * 1000; } -void func_time_cal2sys(time_cal_t *src, time_sys_t *dst) { - if (!src || !dst) return; - struct tm *g = localtime(&src->sec); - if (!g) return; - dst->year=g->tm_year+1900; dst->month=g->tm_mon+1; dst->day=g->tm_mday; - dst->hour=g->tm_hour; dst->min=g->tm_min; - dst->ms=(uint16_t)(g->tm_sec*1000 + src->usec/1000); +void func_time_cal2sys(stru_time_cal *src, stru_time_sys *dst) +{ + if (!src || !dst) + { + return; + } + struct tm *g = localtime(&src->sec); + if (!g) + { + return; + } + dst->year = g->tm_year + 1900; + dst->month = g->tm_mon + 1; + dst->day = g->tm_mday; + dst->hour = g->tm_hour; + dst->min = g->tm_min; + dst->ms = (uint16_t)(g->tm_sec * 1000 + src->usec / 1000); } /* ================================================================= * 4. 文件目录操作 * ================================================================= */ -int func_dir_exist(const char *p) { - if (!p) return 0; - struct stat st; - return (stat(p, &st) == 0 && S_ISDIR(st.st_mode)) ? 1 : 0; +int func_dir_exist(const char *p) +{ + if (!p) + { + return 0; + } + struct stat st; + if (stat(p, &st) == 0 && S_ISDIR(st.st_mode)) + { + return 1; + } + return 0; } -/* 递归创建目录 */ -LOCAL int _mkdir(const char *p) { - if (!p) return -1; - char tmp[256]; - strncpy(tmp, p, sizeof(tmp)-1); tmp[sizeof(tmp)-1]='\0'; - for (char *x = tmp + 1; *x; x++) { - if (*x == '/') { *x = '\0'; mkdir(tmp, 0755); *x = '/'; } - } - return mkdir(tmp, 0755); +LOCAL int _mkdir(const char *p) +{ + if (!p) + { + return -1; + } + char tmp[256]; + strncpy(tmp, p, sizeof(tmp) - 1); + tmp[sizeof(tmp) - 1] = '\0'; + for (char *x = tmp + 1; *x; x++) + { + if (*x == '/') + { + *x = '\0'; + mkdir(tmp, 0755); + *x = '/'; + } + } + return mkdir(tmp, 0755); } -int func_make_dirs(const char *p) { - return _mkdir(p); +int func_make_dirs(const char *p) +{ + return _mkdir(p); } -/* 递归删除 */ -LOCAL int _rmdir(const char *p) { - if (!p) return -1; - DIR *d = opendir(p); - if (!d) return remove(p); - struct dirent *e; - while ((e = readdir(d))) { - if (strcmp(e->d_name,".")==0 || strcmp(e->d_name,"..")==0) continue; - char full[512]; - snprintf(full, sizeof(full), "%s/%s", p, e->d_name); - _rmdir(full); - } - closedir(d); - return rmdir(p); +LOCAL int _rmdir(const char *p) +{ + if (!p) + { + return -1; + } + DIR *d = opendir(p); + if (!d) + { + return remove(p); + } + struct dirent *e; + while ((e = readdir(d))) + { + if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) + { + continue; + } + char full[512]; + snprintf(full, sizeof(full), "%s/%s", p, e->d_name); + _rmdir(full); + } + closedir(d); + return rmdir(p); } -int func_del_dirs(const char *p) { return _rmdir(p); } - -int func_del_dirs_cmd(const char *p) { - if (!p) return -1; - char cmd[512]; - snprintf(cmd, sizeof(cmd), "rm -rf '%s'", p); - return system(cmd); +int func_del_dirs(const char *p) +{ + return _rmdir(p); } -int func_file_exist(const char *p) { - if (!p) return 0; - struct stat st; - return (stat(p, &st) == 0 && S_ISREG(st.st_mode)) ? 1 : 0; +int func_del_dirs_cmd(const char *p) +{ + if (!p) + { + return -1; + } + char cmd[512]; + snprintf(cmd, sizeof(cmd), "rm -rf '%s'", p); + return system(cmd); } -uint32_t func_file_size(const char *p) { - if (!p) return 0; - struct stat st; - return (stat(p, &st) == 0) ? (uint32_t)st.st_size : 0; +int func_file_exist(const char *p) +{ + if (!p) + { + return 0; + } + struct stat st; + if (stat(p, &st) == 0 && S_ISREG(st.st_mode)) + { + return 1; + } + return 0; } -int func_read_file(const char *p, char *buf, uint32_t cap, uint32_t *out) { - if (!p || !buf || !cap || !out) return -1; - FILE *fp = fopen(p, "rb"); - if (!fp) return -1; - *out = (uint32_t)fread(buf, 1, cap, fp); - fclose(fp); - return 0; +uint32_t func_file_size(const char *p) +{ + if (!p) + { + return 0; + } + struct stat st; + if (stat(p, &st) == 0) + { + return (uint32_t)st.st_size; + } + return 0; } -uint8_t *func_read_file_alloc(const char *p, uint32_t *out) { - if (!p || !out) return NULL; - *out = 0; - struct stat st; - if (stat(p, &st) != 0) return NULL; - uint8_t *buf = (uint8_t *)malloc((size_t)st.st_size + 1); - if (!buf) return NULL; - FILE *fp = fopen(p, "rb"); - if (!fp) { free(buf); return NULL; } - *out = (uint32_t)fread(buf, 1, (size_t)st.st_size, fp); - buf[*out] = '\0'; - fclose(fp); - return buf; +int func_read_file(const char *p, char *buf, uint32_t cap, uint32_t *out) +{ + if (!p || !buf || !cap || !out) + { + return -1; + } + FILE *fp = fopen(p, "rb"); + if (!fp) + { + return -1; + } + *out = (uint32_t)fread(buf, 1, cap, fp); + fclose(fp); + return 0; } -int func_write_file(const char *p, const char *buf, uint32_t n) { - if (!p || !buf) return -1; - FILE *fp = fopen(p, "wb"); - if (!fp) return -1; - fwrite(buf, 1, n, fp); - fclose(fp); - return 0; +uint8_t *func_read_file_alloc(const char *p, uint32_t *out) +{ + if (!p || !out) + { + return NULL; + } + *out = 0; + struct stat st; + if (stat(p, &st) != 0) + { + return NULL; + } + uint8_t *buf = (uint8_t *)malloc((size_t)st.st_size + 1); + if (!buf) + { + return NULL; + } + FILE *fp = fopen(p, "rb"); + if (!fp) + { + free(buf); + return NULL; + } + *out = (uint32_t)fread(buf, 1, (size_t)st.st_size, fp); + buf[*out] = '\0'; + fclose(fp); + return buf; } -int func_read_file_info(const char *p, file_info_t *info, int check) { - if (!p || !info) return -1; - FILE *fp = fopen(p, "rb"); - if (!fp) return -1; - if (fread(info, 1, sizeof(*info), fp) != sizeof(*info)) { - fclose(fp); return -1; - } - fclose(fp); - if (check) { - if (info->flag != 0xA55AA55AA55AA55AULL) return -1; - uint16_t saved = info->crc; - info->crc = 0; - uint16_t calced = func_cal_crc16((uint8_t*)info, sizeof(*info)); - info->crc = saved; - if (saved != calced) return -1; - } - return 0; +int func_write_file(const char *p, const char *buf, uint32_t n) +{ + if (!p || !buf) + { + return -1; + } + FILE *fp = fopen(p, "wb"); + if (!fp) + { + return -1; + } + fwrite(buf, 1, n, fp); + fclose(fp); + return 0; +} + +int func_read_file_info(const char *p, stru_file_info *info, int check) +{ + if (!p || !info) + { + return -1; + } + FILE *fp = fopen(p, "rb"); + if (!fp) + { + return -1; + } + if (fread(info, 1, sizeof(*info), fp) != sizeof(*info)) + { + fclose(fp); + return -1; + } + fclose(fp); + if (check) + { + if (info->flag != 0xA55AA55AA55AA55AULL) + { + return -1; + } + uint16_t saved = info->crc; + info->crc = 0; + uint16_t calced = func_cal_crc16((uint8_t *)info, sizeof(*info)); + info->crc = saved; + if (saved != calced) + { + return -1; + } + } + return 0; } /* ================================================================= * 5. 进程管理 * ================================================================= */ -int func_proc_self_name(char *buf, uint32_t sz) { - if (!buf || sz == 0) return -1; - FILE *fp = fopen("/proc/self/comm", "r"); - if (!fp) return -1; - if (!fgets(buf, (int)sz, fp)) { fclose(fp); return -1; } - size_t len = strlen(buf); - if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0'; - fclose(fp); - return 0; +int func_proc_self_name(char *buf, uint32_t sz) +{ + if (!buf || sz == 0) + { + return -1; + } + FILE *fp = fopen("/proc/self/comm", "r"); + if (!fp) + { + return -1; + } + if (!fgets(buf, (int)sz, fp)) + { + fclose(fp); + return -1; + } + size_t len = strlen(buf); + if (len > 0 && buf[len - 1] == '\n') + { + buf[len - 1] = '\0'; + } + fclose(fp); + return 0; } -int func_proc_name_by_pid(int pid, char *buf, uint32_t sz) { - if (!buf || sz == 0) return -1; - char path[64]; - snprintf(path, sizeof(path), "/proc/%d/comm", pid); - FILE *fp = fopen(path, "r"); - if (!fp) return -1; - if (!fgets(buf, (int)sz, fp)) { fclose(fp); return -1; } - size_t len = strlen(buf); - if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0'; - fclose(fp); - return 0; +int func_proc_name_by_pid(int pid, char *buf, uint32_t sz) +{ + if (!buf || sz == 0) + { + return -1; + } + char path[64]; + snprintf(path, sizeof(path), "/proc/%d/comm", pid); + FILE *fp = fopen(path, "r"); + if (!fp) + { + return -1; + } + if (!fgets(buf, (int)sz, fp)) + { + fclose(fp); + return -1; + } + size_t len = strlen(buf); + if (len > 0 && buf[len - 1] == '\n') + { + buf[len - 1] = '\0'; + } + fclose(fp); + return 0; } -int func_proc_pid(void) { return (int)getpid(); } - -int func_proc_pid_by_name(const char *name) { - if (!name) return -1; - DIR *d = opendir("/proc"); - if (!d) return -1; - struct dirent *e; - while ((e = readdir(d))) { - if (!isdigit((unsigned char)e->d_name[0])) continue; - char comm[64], path[128]; - snprintf(path, sizeof(path), "/proc/%s/comm", e->d_name); - FILE *fp = fopen(path, "r"); - if (!fp) continue; - if (fgets(comm, sizeof(comm), fp)) { - size_t len = strlen(comm); - if (len > 0 && comm[len-1] == '\n') comm[len-1] = '\0'; - if (strcmp(comm, name) == 0) { fclose(fp); closedir(d); return atoi(e->d_name); } - } - fclose(fp); - } - closedir(d); - return -1; +int func_proc_pid(void) +{ + return (int)getpid(); } -int func_proc_exist_by_pid(int pid) { - char path[64]; - snprintf(path, sizeof(path), "/proc/%d", pid); - struct stat st; - return (stat(path, &st) == 0) ? 1 : 0; +int func_proc_pid_by_name(const char *name) +{ + if (!name) + { + return -1; + } + DIR *d = opendir("/proc"); + if (!d) + { + return -1; + } + struct dirent *e; + while ((e = readdir(d))) + { + if (!isdigit((unsigned char)e->d_name[0])) + { + continue; + } + char comm[64]; + char path[128]; + snprintf(path, sizeof(path), "/proc/%s/comm", e->d_name); + FILE *fp = fopen(path, "r"); + if (!fp) + { + continue; + } + if (fgets(comm, sizeof(comm), fp)) + { + size_t len = strlen(comm); + if (len > 0 && comm[len - 1] == '\n') + { + comm[len - 1] = '\0'; + } + if (strcmp(comm, name) == 0) + { + fclose(fp); + closedir(d); + return atoi(e->d_name); + } + } + fclose(fp); + } + closedir(d); + return -1; } -int func_proc_exist_by_name(const char *name) { - return func_proc_pid_by_name(name) > 0 ? 1 : 0; +int func_proc_exist_by_pid(int pid) +{ + char path[64]; + snprintf(path, sizeof(path), "/proc/%d", pid); + struct stat st; + if (stat(path, &st) == 0) + { + return 1; + } + return 0; } -int func_proc_path_by_pid(int pid, char *buf, uint32_t sz) { - if (!buf || sz == 0) return -1; - char link[64]; - snprintf(link, sizeof(link), "/proc/%d/exe", pid); - ssize_t n = readlink(link, buf, sz - 1); - if (n < 0) return -1; - buf[n] = '\0'; - return 0; +int func_proc_exist_by_name(const char *name) +{ + if (func_proc_pid_by_name(name) > 0) + { + return 1; + } + return 0; } -int func_proc_self_path(char *buf, uint32_t sz) { - return func_proc_path_by_pid(func_proc_pid(), buf, sz); +int func_proc_path_by_pid(int pid, char *buf, uint32_t sz) +{ + if (!buf || sz == 0) + { + return -1; + } + char link[64]; + snprintf(link, sizeof(link), "/proc/%d/exe", pid); + ssize_t n = readlink(link, buf, sz - 1); + if (n < 0) + { + return -1; + } + buf[n] = '\0'; + return 0; } -int func_proc_self_dir(char *buf, uint32_t sz) { - if (func_proc_self_path(buf, sz) != 0) return -1; - char *slash = strrchr(buf, '/'); - if (slash) *slash = '\0'; - return 0; +int func_proc_self_path(char *buf, uint32_t sz) +{ + return func_proc_path_by_pid(func_proc_pid(), buf, sz); +} + +int func_proc_self_dir(char *buf, uint32_t sz) +{ + if (func_proc_self_path(buf, sz) != 0) + { + return -1; + } + char *slash = strrchr(buf, '/'); + if (slash) + { + *slash = '\0'; + } + return 0; } /* ================================================================= * 6. 环境变量路径 * ================================================================= */ -#define ENV_GET(key, buf, sz) do { \ - if (!buf || sz == 0) return -1; \ - const char *v = getenv(key); \ - if (!v) return -1; \ - snprintf(buf, sz, "%s", v); \ - return 0; \ -} while(0) +int func_get_work_path(char *b, uint32_t s) +{ + const char *v = getenv("WORK_PATH"); + if (!v) + { + return -1; + } + snprintf(b, s, "%s", v); + return 0; +} -int func_get_work_path(char *b, uint32_t s) { ENV_GET("WORK_PATH", b, s); } -int func_get_syscfg_path(char *b, uint32_t s) { ENV_GET("SYS_CFG_PATH", b, s); } -int func_get_app_root(char *b, uint32_t s) { ENV_GET("APP_ROOT_PATH", b, s); } -int func_get_his_root(char *b, uint32_t s) { ENV_GET("HIS_ROOT_PATH", b, s); } -int func_get_log_root(char *b, uint32_t s) { ENV_GET("LOG_ROOT_PATH", b, s); } -int func_get_dbc_root(char *b, uint32_t s) { ENV_GET("DBC_ROOT_PATH", b, s); } -int func_get_shell_path(char *b, uint32_t s) { ENV_GET("SHELL_PATH", b, s); } -int func_get_update_path(char *b, uint32_t s) { ENV_GET("UPDATE_PATH", b, s); } +int func_get_syscfg_path(char *b, uint32_t s) +{ + const char *v = getenv("SYS_CFG_PATH"); + if (!v) + { + return -1; + } + snprintf(b, s, "%s", v); + return 0; +} + +int func_get_app_root(char *b, uint32_t s) +{ + const char *v = getenv("APP_ROOT_PATH"); + if (!v) + { + return -1; + } + snprintf(b, s, "%s", v); + return 0; +} + +int func_get_his_root(char *b, uint32_t s) +{ + const char *v = getenv("HIS_ROOT_PATH"); + if (!v) + { + return -1; + } + snprintf(b, s, "%s", v); + return 0; +} + +int func_get_log_root(char *b, uint32_t s) +{ + const char *v = getenv("LOG_ROOT_PATH"); + if (!v) + { + return -1; + } + snprintf(b, s, "%s", v); + return 0; +} + +int func_get_dbc_root(char *b, uint32_t s) +{ + const char *v = getenv("DBC_ROOT_PATH"); + if (!v) + { + return -1; + } + snprintf(b, s, "%s", v); + return 0; +} + +int func_get_shell_path(char *b, uint32_t s) +{ + const char *v = getenv("SHELL_PATH"); + if (!v) + { + return -1; + } + snprintf(b, s, "%s", v); + return 0; +} + +int func_get_update_path(char *b, uint32_t s) +{ + const char *v = getenv("UPDATE_PATH"); + if (!v) + { + return -1; + } + snprintf(b, s, "%s", v); + return 0; +} -/* 拼接路径:root/app_name[/postfix] */ LOCAL int _make_path(const char *key, const char *app, const char *pfx, char *buf, uint32_t sz) { - if (!buf || sz == 0 || !app) return -1; - char root[256]; - const char *v = getenv(key); - if (!v) v = "/tmp"; - snprintf(root, sizeof(root), "%s", v); - if (pfx && pfx[0]) - snprintf(buf, sz, "%s/%s/%s", root, app, pfx); - else - snprintf(buf, sz, "%s/%s", root, app); - return 0; + if (!buf || sz == 0 || !app) + { + return -1; + } + char root[256]; + const char *v = getenv(key); + if (!v) + { + v = "/tmp"; + } + snprintf(root, sizeof(root), "%s", v); + if (pfx && pfx[0]) + { + snprintf(buf, sz, "%s/%s/%s", root, app, pfx); + } + else + { + snprintf(buf, sz, "%s/%s", root, app); + } + return 0; } int func_get_app_path(const char *a, char *b, uint32_t s) -{ return _make_path("APP_ROOT_PATH", a, NULL, b, s); } +{ + return _make_path("APP_ROOT_PATH", a, NULL, b, s); +} + int func_get_app_cfg(const char *a, const char *e, char *b, uint32_t s) -{ return _make_path("APP_ROOT_PATH", a, e, b, s); } +{ + return _make_path("APP_ROOT_PATH", a, e, b, s); +} + int func_get_app_his(const char *a, char *b, uint32_t s) -{ return _make_path("HIS_ROOT_PATH", a, NULL, b, s); } +{ + return _make_path("HIS_ROOT_PATH", a, NULL, b, s); +} + int func_get_app_log(const char *a, char *b, uint32_t s) -{ return _make_path("LOG_ROOT_PATH", a, NULL, b, s); } +{ + return _make_path("LOG_ROOT_PATH", a, NULL, b, s); +} /* ================================================================= * 7. IPC 消息队列 (System V) * ================================================================= */ -int func_ipc_create_by_name(char *name, int *qid) { - if (!name || !qid) return -1; - int pid = func_proc_pid_by_name(name); - if (pid < 0) return -1; - return func_ipc_create_by_pid(pid, qid); +int func_ipc_create_by_name(char *name, int *qid) +{ + if (!name || !qid) + { + return -1; + } + int pid = func_proc_pid_by_name(name); + if (pid < 0) + { + return -1; + } + return func_ipc_create_by_pid(pid, qid); } -int func_ipc_create_by_pid(int pid, int *qid) { - if (!qid || pid < 1) return -1; - key_t k = ftok("/tmp", pid & 0xFF); - *qid = msgget(k, IPC_CREAT | 0666); - return (*qid >= 0) ? 0 : -1; +int func_ipc_create_by_pid(int pid, int *qid) +{ + if (!qid || pid < 1) + { + return -1; + } + key_t k = ftok("/tmp", pid & 0xFF); + *qid = msgget(k, IPC_CREAT | 0666); + if (*qid >= 0) + { + return 0; + } + return -1; } -int func_ipc_get_by_name(const char *name, int *qid) { - return func_ipc_create_by_name((char*)name, qid); /* 同名:先获取 */ +int func_ipc_get_by_name(const char *name, int *qid) +{ + return func_ipc_create_by_name((char *)name, qid); } -int func_ipc_get_by_pid(int pid, int *qid) { - return func_ipc_create_by_pid(pid, qid); +int func_ipc_get_by_pid(int pid, int *qid) +{ + return func_ipc_create_by_pid(pid, qid); } -int func_ipc_send_buf(int qid, uint8_t *tx, uint32_t n) { - if (!tx || n == 0) return -1; - return msgsnd(qid, tx, n, 0); +int func_ipc_send_buf(int qid, uint8_t *tx, uint32_t n) +{ + if (!tx || n == 0) + { + return -1; + } + return msgsnd(qid, tx, n, 0); } -int func_ipc_recv_buf(int qid, uint8_t *rx, uint32_t n) { - if (!rx || n == 0) return -1; - return (int)msgrcv(qid, rx, n, 0, 0); +int func_ipc_recv_buf(int qid, uint8_t *rx, uint32_t n) +{ + if (!rx || n == 0) + { + return -1; + } + return (int)msgrcv(qid, rx, n, 0, 0); } -int func_ipc_send_msg(int qid, ipc_msg_t *m) { - if (!m) return -1; - return msgsnd(qid, &m->t, sizeof(m->t) - sizeof(m->t.len) + (size_t)m->t.len, 0); +int func_ipc_send_msg(int qid, stru_ipc_msg *m) +{ + if (!m) + { + return -1; + } + return msgsnd(qid, &m->t, sizeof(m->t) - sizeof(m->t.len) + (size_t)m->t.len, 0); } -int func_ipc_recv_msg(int qid, ipc_msg_t *m) { - if (!m) return -1; - ssize_t n = msgrcv(qid, &m->t, sizeof(m->t), m->id, 0); - if (n < 0) return -1; - m->id = (long)n; - return 0; +int func_ipc_recv_msg(int qid, stru_ipc_msg *m) +{ + if (!m) + { + return -1; + } + ssize_t n = msgrcv(qid, &m->t, sizeof(m->t), m->id, 0); + if (n < 0) + { + return -1; + } + m->id = (long)n; + return 0; } -int func_ipc_delete(int qid) { - return msgctl(qid, IPC_RMID, NULL); +int func_ipc_delete(int qid) +{ + return msgctl(qid, IPC_RMID, NULL); } diff --git a/src/public/liblist/inc/list.h b/src/public/liblist/inc/list.h index 2622d49..20c6465 100644 --- a/src/public/liblist/inc/list.h +++ b/src/public/liblist/inc/list.h @@ -12,90 +12,158 @@ #include #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /* ---- 节点 ---- */ -struct list_head { struct list_head *next, *prev; }; +struct list_head +{ + struct list_head *next; + struct list_head *prev; +}; /* ---- 初始化 ---- */ #define LIST_HEAD_INIT(n) { &(n), &(n) } #define LIST_HEAD(n) struct list_head n = LIST_HEAD_INIT(n) static inline void INIT_LIST_HEAD(struct list_head *h) -{ h->next = h; h->prev = h; } +{ + h->next = h; + h->prev = h; +} /* ---- 内部:在 prev 和 next 之间插入 node ---- */ static inline void __list_add(struct list_head *node, struct list_head *prev, struct list_head *next) -{ next->prev = node; node->next = next; node->prev = prev; prev->next = node; } +{ + next->prev = node; + node->next = next; + node->prev = prev; + prev->next = node; +} /* ---- 内部:删除 prev 和 next 之间的节点 ---- */ -static inline void __list_del(struct list_head *prev, struct list_head *next) -{ next->prev = prev; prev->next = next; } +static inline void __list_del(struct list_head *prev, + struct list_head *next) +{ + next->prev = prev; + prev->next = next; +} /* ---- 添加 ---- */ static inline void list_add(struct list_head *node, struct list_head *head) -{ __list_add(node, head, head->next); } +{ + __list_add(node, head, head->next); +} + static inline void list_add_tail(struct list_head *node, struct list_head *head) -{ __list_add(node, head->prev, head); } +{ + __list_add(node, head->prev, head); +} /* ---- 删除 ---- */ static inline void list_del(struct list_head *e) -{ __list_del(e->prev, e->next); } +{ + __list_del(e->prev, e->next); +} + static inline void list_del_init(struct list_head *e) -{ __list_del(e->prev, e->next); INIT_LIST_HEAD(e); } +{ + __list_del(e->prev, e->next); + INIT_LIST_HEAD(e); +} /* ---- 移动 ---- */ static inline void list_move(struct list_head *l, struct list_head *h) -{ __list_del(l->prev, l->next); list_add(l, h); } +{ + __list_del(l->prev, l->next); + list_add(l, h); +} + static inline void list_move_tail(struct list_head *l, struct list_head *h) -{ __list_del(l->prev, l->next); list_add_tail(l, h); } +{ + __list_del(l->prev, l->next); + list_add_tail(l, h); +} /* ---- 替换 ---- */ static inline void list_replace(struct list_head *old, struct list_head *node) -{ node->next=old->next; node->next->prev=node; node->prev=old->prev; node->prev->next=node; } -static inline void list_replace_init(struct list_head *old, struct list_head *node) -{ list_replace(old,node); INIT_LIST_HEAD(old); } +{ + node->next = old->next; + node->next->prev = node; + node->prev = old->prev; + node->prev->next = node; +} + +static inline void list_replace_init(struct list_head *old, + struct list_head *node) +{ + list_replace(old, node); + INIT_LIST_HEAD(old); +} /* ---- 判断 ---- */ static inline int list_empty(const struct list_head *h) -{ return h->next == h; } +{ + return h->next == h; +} + static inline int list_is_linked(const struct list_head *e) -{ return e->next != e; } +{ + return e->next != e; +} /* ---- 宿主指针 ---- */ #define list_entry(ptr, type, member) \ - ((type*)((char*)(ptr) - offsetof(type, member))) -#define list_first_entry(h, type, member) list_entry((h)->next, type, member) -#define list_last_entry(h, type, member) list_entry((h)->prev, type, member) + ((type *)((char *)(ptr) - offsetof(type, member))) + +#define list_first_entry(h, type, member) \ + list_entry((h)->next, type, member) + +#define list_last_entry(h, type, member) \ + list_entry((h)->prev, type, member) + #define list_first_entry_or_null(h, type, member) \ - (list_empty(h) ? NULL : list_first_entry(h, type, member)) + (list_empty(h) ? NULL : list_first_entry(h, type, member)) /* ---- 遍历 ---- */ #define list_for_each(pos, head) \ - for (pos = (head)->next; pos != (head); pos = pos->next) + for (pos = (head)->next; pos != (head); pos = pos->next) + #define list_for_each_safe(pos, n, head) \ - for (pos = (head)->next, n = pos->next; pos != (head); pos = n, n = pos->next) + for (pos = (head)->next, n = pos->next; pos != (head); \ + pos = n, n = pos->next) + #define list_for_each_entry(pos, head, member) \ - for (pos = list_first_entry(head, __typeof__(*pos), member); \ - &pos->member != (head); \ - pos = list_entry(pos->member.next, __typeof__(*pos), member)) + for (pos = list_first_entry(head, __typeof__(*pos), member); \ + &pos->member != (head); \ + pos = list_entry(pos->member.next, __typeof__(*pos), member)) + #define list_for_each_entry_safe(pos, n, head, member) \ - for (pos = list_first_entry(head, __typeof__(*pos), member), \ - n = list_entry(pos->member.next, __typeof__(*pos), member); \ - &pos->member != (head); \ - pos = n, n = list_entry(n->member.next, __typeof__(*n), member)) + for (pos = list_first_entry(head, __typeof__(*pos), member), \ + n = list_entry(pos->member.next, __typeof__(*pos), member); \ + &pos->member != (head); \ + pos = n, \ + n = list_entry(n->member.next, __typeof__(*n), member)) /* ---- 拼接 ---- */ -static inline void list_splice_init(struct list_head *src, struct list_head *dst) +static inline void list_splice_init(struct list_head *src, + struct list_head *dst) { - if (!list_empty(src)) { - struct list_head *f = src->next, *l = src->prev, *a = dst->next; - f->prev = dst; dst->next = f; l->next = a; a->prev = l; - INIT_LIST_HEAD(src); - } + if (!list_empty(src)) + { + struct list_head *f = src->next; + struct list_head *l = src->prev; + struct list_head *a = dst->next; + + f->prev = dst; + dst->next = f; + l->next = a; + a->prev = l; + INIT_LIST_HEAD(src); + } } #ifdef __cplusplus diff --git a/src/public/liblog/inc/myLog.h b/src/public/liblog/inc/myLog.h index 97a3bce..d7b3d42 100644 --- a/src/public/liblog/inc/myLog.h +++ b/src/public/liblog/inc/myLog.h @@ -7,7 +7,8 @@ #define _MY_LOG_H_ #include #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif #define LOG_INFO 0 diff --git a/src/public/liblog/src/myLog.c b/src/public/liblog/src/myLog.c index 6104662..ead8d2c 100644 --- a/src/public/liblog/src/myLog.c +++ b/src/public/liblog/src/myLog.c @@ -22,114 +22,149 @@ #include /* ========== 可配置参数 ========== */ -#define POOL_SIZE 10000 /* 消息池大小 */ -#define BATCH_MAX 100 /* 单次批量处理上限 */ -#define FLUSH_MS 100 /* 空闲等待间隔(毫秒) */ -#define FILE_MAX_SIZE (10*1024*1024) /* 单文件 10MB */ -#define FILE_MAX_KEEP 5 /* 保留历史文件数 */ -#define CONTENT_MAX 4096 /* 单条日志内容上限 */ +#define POOL_SIZE 10000 +#define BATCH_MAX 100 +#define FLUSH_MS 100 +#define FILE_MAX_SIZE (10 * 1024 * 1024) +#define FILE_MAX_KEEP 5 +#define CONTENT_MAX 4096 /* ========== 数据结构 ========== */ /** 单条日志消息 */ -typedef struct log_msg { - uint32_t level; - char time_str[64]; - char src_info[128]; /* "文件名/函数:行号" */ - char content[CONTENT_MAX]; - struct log_msg *next; /* 空闲链表 / 队列链表 */ -} log_msg_t; +typedef struct +{ + uint32_t level; + char time_str[64]; + char src_info[128]; /* "文件名/函数:行号" */ + char content[CONTENT_MAX]; + struct log_msg *next; /* 空闲链表 / 队列链表 */ +} stru_log_msg; /** 线程安全队列 */ -typedef struct { - log_msg_t *head, *tail; - int count; - pthread_mutex_t lock; - pthread_cond_t cond_pop; /* 消费者等待 */ - pthread_cond_t cond_push; /* 生产者等待(队列满) */ -} msg_queue_t; +typedef struct +{ + stru_log_msg *head; + stru_log_msg *tail; + int count; + pthread_mutex_t lock; + pthread_cond_t cond_pop; + pthread_cond_t cond_push; +} stru_msg_queue; /** 日志系统全局状态 */ -LOCAL struct { - msg_queue_t q; - pthread_t worker; - volatile int running; - FILE *fp; - long file_bytes; - int to_file; - int to_console; - char dir[256]; -} g_log; +typedef struct +{ + stru_msg_queue q; + pthread_t worker; + volatile int running; + FILE *fp; + long file_bytes; + int to_file; + int to_console; + char dir[256]; +} stru_log_state; + +LOCAL stru_log_state g_log; /* ========== 消息池(预分配,无运行时 malloc) ========== */ -LOCAL log_msg_t *g_pool_free; /* 空闲链表头 */ -LOCAL log_msg_t g_pool_buf[POOL_SIZE]; /* 预分配数组 */ -LOCAL pthread_mutex_t g_pool_lock = PTHREAD_MUTEX_INITIALIZER; -LOCAL pthread_once_t g_init_once = PTHREAD_ONCE_INIT; +LOCAL stru_log_msg *g_pool_free; +LOCAL stru_log_msg g_pool_buf[POOL_SIZE]; +LOCAL pthread_mutex_t g_pool_lock = PTHREAD_MUTEX_INITIALIZER; +LOCAL pthread_once_t g_init_once = PTHREAD_ONCE_INIT; /** 初始化消息池:把数组元素链成空闲链表 */ LOCAL void pool_init(void) { - pthread_mutex_lock(&g_pool_lock); - if (g_pool_free != NULL) { pthread_mutex_unlock(&g_pool_lock); return; } - for (int i = 0; i < POOL_SIZE - 1; i++) - g_pool_buf[i].next = &g_pool_buf[i + 1]; - g_pool_buf[POOL_SIZE - 1].next = NULL; - g_pool_free = &g_pool_buf[0]; - pthread_mutex_unlock(&g_pool_lock); + pthread_mutex_lock(&g_pool_lock); + if (g_pool_free != NULL) + { + pthread_mutex_unlock(&g_pool_lock); + return; + } + for (int i = 0; i < POOL_SIZE - 1; i++) + { + g_pool_buf[i].next = &g_pool_buf[i + 1]; + } + g_pool_buf[POOL_SIZE - 1].next = NULL; + g_pool_free = &g_pool_buf[0]; + pthread_mutex_unlock(&g_pool_lock); } /** 从池中取一个空闲消息块(失败返回 NULL) */ -LOCAL log_msg_t *pool_alloc(void) +LOCAL stru_log_msg *pool_alloc(void) { - pthread_mutex_lock(&g_pool_lock); - log_msg_t *m = g_pool_free; - if (m) g_pool_free = m->next; - pthread_mutex_unlock(&g_pool_lock); - return m; + pthread_mutex_lock(&g_pool_lock); + stru_log_msg *m = g_pool_free; + if (m) + { + g_pool_free = m->next; + } + pthread_mutex_unlock(&g_pool_lock); + return m; } /** 归还消息块到池 */ -LOCAL void pool_free(log_msg_t *m) +LOCAL void pool_free(stru_log_msg *m) { - if (!m) return; - pthread_mutex_lock(&g_pool_lock); - m->next = g_pool_free; - g_pool_free = m; - pthread_mutex_unlock(&g_pool_lock); + if (!m) + { + return; + } + pthread_mutex_lock(&g_pool_lock); + m->next = g_pool_free; + g_pool_free = m; + pthread_mutex_unlock(&g_pool_lock); } /* ========== 队列操作 ========== */ /** 入队(满则阻塞) */ -LOCAL void q_push(log_msg_t *m) +LOCAL void q_push(stru_log_msg *m) { - pthread_mutex_lock(&g_log.q.lock); - while (g_log.q.count >= POOL_SIZE) - pthread_cond_wait(&g_log.q.cond_push, &g_log.q.lock); - m->next = NULL; - if (!g_log.q.head) g_log.q.head = m; - else g_log.q.tail->next = m; - g_log.q.tail = m; - g_log.q.count++; - pthread_cond_signal(&g_log.q.cond_pop); - pthread_mutex_unlock(&g_log.q.lock); + pthread_mutex_lock(&g_log.q.lock); + while (g_log.q.count >= POOL_SIZE) + { + pthread_cond_wait(&g_log.q.cond_push, &g_log.q.lock); + } + m->next = NULL; + if (!g_log.q.head) + { + g_log.q.head = m; + } + else + { + g_log.q.tail->next = m; + } + g_log.q.tail = m; + g_log.q.count++; + pthread_cond_signal(&g_log.q.cond_pop); + pthread_mutex_unlock(&g_log.q.lock); } /** 出队(空则等待或返回 NULL) */ -LOCAL log_msg_t *q_pop(void) +LOCAL stru_log_msg *q_pop(void) { - pthread_mutex_lock(&g_log.q.lock); - while (g_log.q.count == 0 && g_log.running) - pthread_cond_wait(&g_log.q.cond_pop, &g_log.q.lock); - if (g_log.q.count == 0) { pthread_mutex_unlock(&g_log.q.lock); return NULL; } - log_msg_t *m = g_log.q.head; - g_log.q.head = m->next; - if (!g_log.q.head) g_log.q.tail = NULL; - g_log.q.count--; - pthread_cond_signal(&g_log.q.cond_push); - pthread_mutex_unlock(&g_log.q.lock); - return m; + pthread_mutex_lock(&g_log.q.lock); + while (g_log.q.count == 0 && g_log.running) + { + pthread_cond_wait(&g_log.q.cond_pop, &g_log.q.lock); + } + if (g_log.q.count == 0) + { + pthread_mutex_unlock(&g_log.q.lock); + return NULL; + } + stru_log_msg *m = g_log.q.head; + g_log.q.head = m->next; + if (!g_log.q.head) + { + g_log.q.tail = NULL; + } + g_log.q.count--; + pthread_cond_signal(&g_log.q.cond_push); + pthread_mutex_unlock(&g_log.q.lock); + return m; } /* ========== 文件管理 ========== */ @@ -137,68 +172,92 @@ LOCAL log_msg_t *q_pop(void) /** 拼接日志文件路径:dir/app.log 或 dir/app.log.N */ LOCAL void file_path(char *buf, size_t sz, int idx) { - if (idx == 0) - snprintf(buf, sz, "%s/app.log", g_log.dir); - else - snprintf(buf, sz, "%s/app.log.%d", g_log.dir, idx); + if (idx == 0) + { + snprintf(buf, sz, "%s/app.log", g_log.dir); + } + else + { + snprintf(buf, sz, "%s/app.log.%d", g_log.dir, idx); + } } /** 创建日志目录 */ LOCAL int ensure_dir(void) { - if (!g_log.dir[0]) { - /* 默认放到 /tmp/logs */ - snprintf(g_log.dir, sizeof(g_log.dir), "/tmp/logs"); - } - struct stat st; - if (stat(g_log.dir, &st) != 0) - return mkdir(g_log.dir, 0755); - return 0; + if (!g_log.dir[0]) + { + snprintf(g_log.dir, sizeof(g_log.dir), "/tmp/logs"); + } + struct stat st; + if (stat(g_log.dir, &st) != 0) + { + return mkdir(g_log.dir, 0755); + } + return 0; } /** 轮转日志文件:app.log→.1→.2→...→.N(max),删除最旧的 */ LOCAL void rotate_files(void) { - char old[512], neo[512]; - if (g_log.fp) { fclose(g_log.fp); g_log.fp = NULL; } + char old[512]; + char neo[512]; - /* 删除最旧 */ - file_path(old, sizeof(old), FILE_MAX_KEEP); - remove(old); + if (g_log.fp) + { + fclose(g_log.fp); + g_log.fp = NULL; + } - /* 依次后移 */ - for (int i = FILE_MAX_KEEP - 1; i >= 1; i--) { - file_path(old, sizeof(old), i); - file_path(neo, sizeof(neo), i + 1); - rename(old, neo); - } + /* 删除最旧 */ + file_path(old, sizeof(old), FILE_MAX_KEEP); + remove(old); - /* 当前文件 → .1 */ - file_path(old, sizeof(old), 0); - file_path(neo, sizeof(neo), 1); - rename(old, neo); - g_log.file_bytes = 0; + /* 依次后移 */ + for (int i = FILE_MAX_KEEP - 1; i >= 1; i--) + { + file_path(old, sizeof(old), i); + file_path(neo, sizeof(neo), i + 1); + rename(old, neo); + } + + /* 当前文件 → .1 */ + file_path(old, sizeof(old), 0); + file_path(neo, sizeof(neo), 1); + rename(old, neo); + g_log.file_bytes = 0; } /** 打开当前日志文件 */ LOCAL FILE *open_file(void) { - if (ensure_dir() != 0) return NULL; - char path[512]; - file_path(path, sizeof(path), 0); + if (ensure_dir() != 0) + { + return NULL; + } + char path[512]; + file_path(path, sizeof(path), 0); - /* 检查已有文件大小 */ - struct stat st; - if (stat(path, &st) == 0) { - if (st.st_size > FILE_MAX_SIZE) - rotate_files(); - else - g_log.file_bytes = st.st_size; - } + /* 检查已有文件大小 */ + struct stat st; + if (stat(path, &st) == 0) + { + if (st.st_size > FILE_MAX_SIZE) + { + rotate_files(); + } + else + { + g_log.file_bytes = st.st_size; + } + } - FILE *fp = fopen(path, "a"); - if (fp) setlinebuf(fp); /* 行缓冲 */ - return fp; + FILE *fp = fopen(path, "a"); + if (fp) + { + setlinebuf(fp); + } + return fp; } /* ========== 格式化 ========== */ @@ -206,172 +265,252 @@ LOCAL FILE *open_file(void) /** 当前时间字符串:YYYY-MM-DD HH:MM:SS.mmm */ LOCAL void fmt_time(char *buf, size_t sz) { - struct timeval tv; - gettimeofday(&tv, NULL); - struct tm *t = localtime(&tv.tv_sec); - if (t) { - snprintf(buf, sz, "%04d-%02d-%02d %02d:%02d:%02d.%03d", - t->tm_year+1900, t->tm_mon+1, t->tm_mday, - t->tm_hour, t->tm_min, t->tm_sec, - (int)(tv.tv_usec / 1000)); - } else { - snprintf(buf, sz, "----.--.-- --:--:--.---"); - } + struct timeval tv; + gettimeofday(&tv, NULL); + struct tm *t = localtime(&tv.tv_sec); + if (t) + { + snprintf(buf, sz, "%04d-%02d-%02d %02d:%02d:%02d.%03d", + t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, + t->tm_hour, t->tm_min, t->tm_sec, + (int)(tv.tv_usec / 1000)); + } + else + { + snprintf(buf, sz, "----.--.-- --:--:--.---"); + } } /** 提取文件名(去掉路径) 并拼接成 "file/func:line" */ LOCAL void fmt_src(char *buf, size_t sz, const char *file, const char *func, uint32_t line) { - if (!file || !func) { snprintf(buf, sz, "?:?:0"); return; } - const char *p = strrchr(file, '/'); - if (!p) p = strrchr(file, '\\'); - const char *name = p ? p + 1 : file; - snprintf(buf, sz, "%s/%s:%u", name, func, line); + if (!file || !func) + { + snprintf(buf, sz, "?:?:0"); + return; + } + const char *p = strrchr(file, '/'); + if (!p) + { + p = strrchr(file, '\\'); + } + const char *name = p ? p + 1 : file; + snprintf(buf, sz, "%s/%s:%u", name, func, line); } /* ========== 输出 ========== */ /** 批量写控制台 + 文件 */ -LOCAL void output_batch(log_msg_t **batch, int cnt) +LOCAL void output_batch(stru_log_msg **batch, int cnt) { - if (cnt == 0) return; - static const char *titles[] = {"INFO", "ERROR"}; - static const char *colors[] = {COLOR_WHITE, COLOR_RED}; + if (cnt == 0) + { + return; + } + static const char *titles[] = { "INFO", "ERROR" }; + static const char *colors[] = { COLOR_WHITE, COLOR_RED }; - /* 控制台(带颜色) */ - if (g_log.to_console) { - for (int i = 0; i < cnt; i++) { - log_msg_t *m = batch[i]; - printf("%s[%s] [%s%s%s] %s\n", - colors[m->level], m->time_str, - colors[m->level], titles[m->level], - COLOR_RESET, m->content); - } - fflush(stdout); - } + /* 控制台(带颜色) */ + if (g_log.to_console) + { + for (int i = 0; i < cnt; i++) + { + stru_log_msg *m = batch[i]; + printf("%s[%s] [%s%s%s] %s\n", + colors[m->level], m->time_str, + colors[m->level], titles[m->level], + COLOR_RESET, m->content); + } + fflush(stdout); + } - /* 文件 */ - if (g_log.to_file) { - if (!g_log.fp) g_log.fp = open_file(); - if (g_log.fp) { - for (int i = 0; i < cnt; i++) { - log_msg_t *m = batch[i]; - int n = fprintf(g_log.fp, "[%s] [%s] %s\n", - titles[m->level], m->time_str, m->content); - if (n > 0) g_log.file_bytes += n; - } - fflush(g_log.fp); - if (g_log.file_bytes > FILE_MAX_SIZE) rotate_files(); - } - } + /* 文件 */ + if (g_log.to_file) + { + if (!g_log.fp) + { + g_log.fp = open_file(); + } + if (g_log.fp) + { + for (int i = 0; i < cnt; i++) + { + stru_log_msg *m = batch[i]; + int n = fprintf(g_log.fp, "[%s] [%s] %s\n", + titles[m->level], m->time_str, m->content); + if (n > 0) + { + g_log.file_bytes += n; + } + } + fflush(g_log.fp); + if (g_log.file_bytes > FILE_MAX_SIZE) + { + rotate_files(); + } + } + } } /* ========== 工作线程 ========== */ LOCAL void *worker_thread(void *arg) { - (void)arg; - while (g_log.running) { - log_msg_t *batch[BATCH_MAX]; - int cnt = 0; + (void)arg; + while (g_log.running) + { + stru_log_msg *batch[BATCH_MAX]; + int cnt = 0; - /* 批量取消息 */ - for (int i = 0; i < BATCH_MAX; i++) { - log_msg_t *m = q_pop(); - if (!m) break; - batch[cnt++] = m; - } + /* 批量取消息 */ + for (int i = 0; i < BATCH_MAX; i++) + { + stru_log_msg *m = q_pop(); + if (!m) + { + break; + } + batch[cnt++] = m; + } - if (cnt > 0) { - output_batch(batch, cnt); - for (int i = 0; i < cnt; i++) pool_free(batch[i]); - } else { - usleep(FLUSH_MS * 1000); - } - } - return NULL; + if (cnt > 0) + { + output_batch(batch, cnt); + for (int i = 0; i < cnt; i++) + { + pool_free(batch[i]); + } + } + else + { + usleep(FLUSH_MS * 1000); + } + } + return NULL; } /* ========== 全局初始化(pthread_once) ========== */ LOCAL void global_init(void) { - pool_init(); - /* g_log.q 已在声明时用 {0} 初始化 */ - g_log.q.head = g_log.q.tail = NULL; - g_log.q.count = 0; - g_log.to_console = 1; - g_log.to_file = 0; - g_log.running = 1; - pthread_create(&g_log.worker, NULL, worker_thread, NULL); + pool_init(); + /* g_log.q 已在声明时用 {0} 初始化 */ + g_log.q.head = NULL; + g_log.q.tail = NULL; + g_log.q.count = 0; + g_log.to_console = 1; + g_log.to_file = 0; + g_log.running = 1; + pthread_create(&g_log.worker, NULL, worker_thread, NULL); } /* ========== 公开 API ========== */ int log_init(const char *dir) { - if (dir && dir[0]) - snprintf(g_log.dir, sizeof(g_log.dir), "%s", dir); - pool_init(); /* 确保消息池就绪 */ - g_log.to_file = 1; - return 0; + if (dir && dir[0]) + { + snprintf(g_log.dir, sizeof(g_log.dir), "%s", dir); + } + pool_init(); + g_log.to_file = 1; + return 0; } void log_prt(uint32_t level, const char *file, const char *func, uint32_t line, const char *fmt, ...) { - /* 懒初始化 */ - pthread_once(&g_init_once, global_init); - if (level > LOG_ERROR || !fmt || !g_log.running) return; + /* 懒初始化 */ + pthread_once(&g_init_once, global_init); + if (level > LOG_ERROR || !fmt || !g_log.running) + { + return; + } - /* 从消息池取空闲块 */ - log_msg_t *m = pool_alloc(); - if (!m) return; /* 池耗尽,丢弃本条 */ + /* 从消息池取空闲块 */ + stru_log_msg *m = pool_alloc(); + if (!m) + { + return; + } - m->level = level; - fmt_time(m->time_str, sizeof(m->time_str)); - fmt_src(m->src_info, sizeof(m->src_info), file, func, line); + m->level = level; + fmt_time(m->time_str, sizeof(m->time_str)); + fmt_src(m->src_info, sizeof(m->src_info), file, func, line); - va_list ap; - va_start(ap, fmt); - int pos = snprintf(m->content, sizeof(m->content), "[%s] ", m->src_info); - if (pos < (int)sizeof(m->content)) - vsnprintf(m->content + pos, sizeof(m->content) - pos, fmt, ap); - va_end(ap); + va_list ap; + va_start(ap, fmt); + int pos = snprintf(m->content, sizeof(m->content), "[%s] ", m->src_info); + if (pos < (int)sizeof(m->content)) + { + vsnprintf(m->content + pos, sizeof(m->content) - pos, fmt, ap); + } + va_end(ap); - q_push(m); + q_push(m); } -void log_file_on(void) { g_log.to_file = 1; } -void log_file_off(void) { g_log.to_file = 0; } -void log_console_on(void) { g_log.to_console = 1; } -void log_console_off(void) { g_log.to_console = 0; } +void log_file_on(void) +{ + g_log.to_file = 1; +} + +void log_file_off(void) +{ + g_log.to_file = 0; +} + +void log_console_on(void) +{ + g_log.to_console = 1; +} + +void log_console_off(void) +{ + g_log.to_console = 0; +} void log_flush(void) { - /* 轮询等待队列归零 */ - for (;;) { - pthread_mutex_lock(&g_log.q.lock); - int c = g_log.q.count; - pthread_mutex_unlock(&g_log.q.lock); - if (c == 0) break; - usleep(10000); - } + for (;;) + { + pthread_mutex_lock(&g_log.q.lock); + int c = g_log.q.count; + pthread_mutex_unlock(&g_log.q.lock); + if (c == 0) + { + break; + } + usleep(10000); + } } void log_cleanup(void) { - if (!g_log.running) return; - g_log.running = 0; - pthread_cond_signal(&g_log.q.cond_pop); - pthread_join(g_log.worker, NULL); - if (g_log.fp) { fclose(g_log.fp); g_log.fp = NULL; } - /* 释放池中残留消息 */ - pthread_mutex_lock(&g_log.q.lock); - log_msg_t *cur = g_log.q.head, *tmp; - while (cur) { tmp = cur; cur = cur->next; pool_free(tmp); } - g_log.q.head = g_log.q.tail = NULL; - g_log.q.count = 0; - pthread_mutex_unlock(&g_log.q.lock); + if (!g_log.running) + { + return; + } + g_log.running = 0; + pthread_cond_signal(&g_log.q.cond_pop); + pthread_join(g_log.worker, NULL); + if (g_log.fp) + { + fclose(g_log.fp); + g_log.fp = NULL; + } + /* 释放池中残留消息 */ + pthread_mutex_lock(&g_log.q.lock); + stru_log_msg *cur = g_log.q.head; + while (cur) + { + stru_log_msg *tmp = cur; + cur = cur->next; + pool_free(tmp); + } + g_log.q.head = NULL; + g_log.q.tail = NULL; + g_log.q.count = 0; + pthread_mutex_unlock(&g_log.q.lock); }