[风格] 统一代码风格规范

- 一行只能有一条语句
- if/for/while 即使单条也必须换行 + { },花括号左右各独占一行
- 结构体 typedef 加 stru_ 前缀 (stru_log_msg/stru_time_sys/...)
- 枚举 typedef 加 enum_ 前缀 (enum_ipc_resp)
- 结构体声明左花括号独占一行
- list.h: 函数体花括号独占行, 多语句分行
- myLog.c: 全局类型改名 + 规范花括号
- myFunc.h: 类型前缀 + extern \'C\' {}独占行
- myFunc.c: 全部 if/for/while 规范化, 类型引用更新
This commit is contained in:
ypc 2026-07-07 13:32:26 +08:00
parent 4ee054e583
commit b5e4a361b4
9 changed files with 1648 additions and 801 deletions

View File

@ -12,90 +12,158 @@
#include <stddef.h> #include <stddef.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C"
{
#endif #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_INIT(n) { &(n), &(n) }
#define LIST_HEAD(n) struct list_head n = LIST_HEAD_INIT(n) #define LIST_HEAD(n) struct list_head n = LIST_HEAD_INIT(n)
static inline void INIT_LIST_HEAD(struct list_head *h) 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 ---- */ /* ---- 内部:在 prev 和 next 之间插入 node ---- */
static inline void __list_add(struct list_head *node, static inline void __list_add(struct list_head *node,
struct list_head *prev, struct list_head *prev,
struct list_head *next) 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 之间的节点 ---- */ /* ---- 内部:删除 prev 和 next 之间的节点 ---- */
static inline void __list_del(struct list_head *prev, struct list_head *next) static inline void __list_del(struct list_head *prev,
{ next->prev = prev; prev->next = next; } struct list_head *next)
{
next->prev = prev;
prev->next = next;
}
/* ---- 添加 ---- */ /* ---- 添加 ---- */
static inline void list_add(struct list_head *node, struct list_head *head) 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) 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) 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) 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) 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) 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) 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) node->next = old->next;
{ list_replace(old,node); INIT_LIST_HEAD(old); } 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) 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) 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) \ #define list_entry(ptr, type, member) \
((type*)((char*)(ptr) - offsetof(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(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) \ #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) \ #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) \ #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) \ #define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, __typeof__(*pos), member); \ for (pos = list_first_entry(head, __typeof__(*pos), member); \
&pos->member != (head); \ &pos->member != (head); \
pos = list_entry(pos->member.next, __typeof__(*pos), member)) pos = list_entry(pos->member.next, __typeof__(*pos), member))
#define list_for_each_entry_safe(pos, n, head, member) \ #define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_first_entry(head, __typeof__(*pos), member), \ for (pos = list_first_entry(head, __typeof__(*pos), member), \
n = list_entry(pos->member.next, __typeof__(*pos), member); \ n = list_entry(pos->member.next, __typeof__(*pos), member); \
&pos->member != (head); \ &pos->member != (head); \
pos = n, n = list_entry(n->member.next, __typeof__(*n), member)) 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)) { 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; struct list_head *f = src->next;
INIT_LIST_HEAD(src); 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 #ifdef __cplusplus

View File

@ -18,7 +18,8 @@
#include <sys/stat.h> #include <sys/stat.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C"
{
#endif #endif
/* === 终端颜色 === */ /* === 终端颜色 === */

View File

@ -9,28 +9,69 @@
#include <time.h> #include <time.h>
#include <sys/time.h> #include <sys/time.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C"
{
#endif #endif
/* ---- 时间结构 ---- */ /* ---- 系统时间 ---- */
typedef struct { 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; uint32_t ms : 16;
typedef struct { time_t sec; time_t usec; } time_cal_t; 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
typedef struct { long id; ipc_text_t t; } ipc_msg_t; {
enum { IPC_RESP_NONE=1, IPC_RESP_ONCE, IPC_RESP_ALWAYS }; 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字节) ---- */ /* ---- 文件描述头(256字节) ---- */
#pragma pack(1) #pragma pack(1)
typedef struct { typedef struct
uint64_t flag; /* 魔数 0xA55AA55AA55AA55A */ {
char ver[16], author[16], modify[32]; uint64_t flag;
uint16_t crc; char ver[16];
char desc[128], bak[54]; char author[16];
} file_info_t; char modify[32];
uint16_t crc;
char desc[128];
char bak[54];
} stru_file_info;
#pragma pack() #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); 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); char *func_time_sys2str(char *buf, int sz, stru_time_sys *t);
int func_str2time_sys(const char *s, time_sys_t *t); int func_str2time_sys(const char *s, stru_time_sys *t);
int func_get_time(void *t, int type); /* type: 1=sys 2=cal */ int func_get_time(void *t, int type);
int func_set_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_sys2cal(stru_time_sys *src, stru_time_cal *dst);
void func_time_cal2sys(time_cal_t *src, time_sys_t *dst); void func_time_cal2sys(stru_time_cal *src, stru_time_sys *dst);
/* ===== 文件目录 ===== */ /* ===== 文件目录 ===== */
int func_dir_exist(const char *p); 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); 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); 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_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); 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_get_by_pid(int pid, int *qid);
int func_ipc_send_buf(int qid, uint8_t *tx, uint32_t n); 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_recv_buf(int qid, uint8_t *rx, uint32_t n);
int func_ipc_send_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, ipc_msg_t *m); int func_ipc_recv_msg(int qid, stru_ipc_msg *m);
int func_ipc_delete(int qid); int func_ipc_delete(int qid);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -7,7 +7,8 @@
#define _MY_LOG_H_ #define _MY_LOG_H_
#include <stdint.h> #include <stdint.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C"
{
#endif #endif
#define LOG_INFO 0 #define LOG_INFO 0

View File

@ -9,28 +9,69 @@
#include <time.h> #include <time.h>
#include <sys/time.h> #include <sys/time.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C"
{
#endif #endif
/* ---- 时间结构 ---- */ /* ---- 系统时间 ---- */
typedef struct { 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; uint32_t ms : 16;
typedef struct { time_t sec; time_t usec; } time_cal_t; 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
typedef struct { long id; ipc_text_t t; } ipc_msg_t; {
enum { IPC_RESP_NONE=1, IPC_RESP_ONCE, IPC_RESP_ALWAYS }; 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字节) ---- */ /* ---- 文件描述头(256字节) ---- */
#pragma pack(1) #pragma pack(1)
typedef struct { typedef struct
uint64_t flag; /* 魔数 0xA55AA55AA55AA55A */ {
char ver[16], author[16], modify[32]; uint64_t flag;
uint16_t crc; char ver[16];
char desc[128], bak[54]; char author[16];
} file_info_t; char modify[32];
uint16_t crc;
char desc[128];
char bak[54];
} stru_file_info;
#pragma pack() #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); 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); char *func_time_sys2str(char *buf, int sz, stru_time_sys *t);
int func_str2time_sys(const char *s, time_sys_t *t); int func_str2time_sys(const char *s, stru_time_sys *t);
int func_get_time(void *t, int type); /* type: 1=sys 2=cal */ int func_get_time(void *t, int type);
int func_set_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_sys2cal(stru_time_sys *src, stru_time_cal *dst);
void func_time_cal2sys(time_cal_t *src, time_sys_t *dst); void func_time_cal2sys(stru_time_cal *src, stru_time_sys *dst);
/* ===== 文件目录 ===== */ /* ===== 文件目录 ===== */
int func_dir_exist(const char *p); 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); 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); 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_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); 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_get_by_pid(int pid, int *qid);
int func_ipc_send_buf(int qid, uint8_t *tx, uint32_t n); 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_recv_buf(int qid, uint8_t *rx, uint32_t n);
int func_ipc_send_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, ipc_msg_t *m); int func_ipc_recv_msg(int qid, stru_ipc_msg *m);
int func_ipc_delete(int qid); int func_ipc_delete(int qid);
#ifdef __cplusplus #ifdef __cplusplus

File diff suppressed because it is too large Load Diff

View File

@ -12,90 +12,158 @@
#include <stddef.h> #include <stddef.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C"
{
#endif #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_INIT(n) { &(n), &(n) }
#define LIST_HEAD(n) struct list_head n = LIST_HEAD_INIT(n) #define LIST_HEAD(n) struct list_head n = LIST_HEAD_INIT(n)
static inline void INIT_LIST_HEAD(struct list_head *h) 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 ---- */ /* ---- 内部:在 prev 和 next 之间插入 node ---- */
static inline void __list_add(struct list_head *node, static inline void __list_add(struct list_head *node,
struct list_head *prev, struct list_head *prev,
struct list_head *next) 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 之间的节点 ---- */ /* ---- 内部:删除 prev 和 next 之间的节点 ---- */
static inline void __list_del(struct list_head *prev, struct list_head *next) static inline void __list_del(struct list_head *prev,
{ next->prev = prev; prev->next = next; } struct list_head *next)
{
next->prev = prev;
prev->next = next;
}
/* ---- 添加 ---- */ /* ---- 添加 ---- */
static inline void list_add(struct list_head *node, struct list_head *head) 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) 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) 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) 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) 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) 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) 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) node->next = old->next;
{ list_replace(old,node); INIT_LIST_HEAD(old); } 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) 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) 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) \ #define list_entry(ptr, type, member) \
((type*)((char*)(ptr) - offsetof(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(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) \ #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) \ #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) \ #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) \ #define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, __typeof__(*pos), member); \ for (pos = list_first_entry(head, __typeof__(*pos), member); \
&pos->member != (head); \ &pos->member != (head); \
pos = list_entry(pos->member.next, __typeof__(*pos), member)) pos = list_entry(pos->member.next, __typeof__(*pos), member))
#define list_for_each_entry_safe(pos, n, head, member) \ #define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_first_entry(head, __typeof__(*pos), member), \ for (pos = list_first_entry(head, __typeof__(*pos), member), \
n = list_entry(pos->member.next, __typeof__(*pos), member); \ n = list_entry(pos->member.next, __typeof__(*pos), member); \
&pos->member != (head); \ &pos->member != (head); \
pos = n, n = list_entry(n->member.next, __typeof__(*n), member)) 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)) { 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; struct list_head *f = src->next;
INIT_LIST_HEAD(src); 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 #ifdef __cplusplus

View File

@ -7,7 +7,8 @@
#define _MY_LOG_H_ #define _MY_LOG_H_
#include <stdint.h> #include <stdint.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C"
{
#endif #endif
#define LOG_INFO 0 #define LOG_INFO 0

View File

@ -22,114 +22,149 @@
#include <stdlib.h> #include <stdlib.h>
/* ========== 可配置参数 ========== */ /* ========== 可配置参数 ========== */
#define POOL_SIZE 10000 /* 消息池大小 */ #define POOL_SIZE 10000
#define BATCH_MAX 100 /* 单次批量处理上限 */ #define BATCH_MAX 100
#define FLUSH_MS 100 /* 空闲等待间隔(毫秒) */ #define FLUSH_MS 100
#define FILE_MAX_SIZE (10*1024*1024) /* 单文件 10MB */ #define FILE_MAX_SIZE (10 * 1024 * 1024)
#define FILE_MAX_KEEP 5 /* 保留历史文件数 */ #define FILE_MAX_KEEP 5
#define CONTENT_MAX 4096 /* 单条日志内容上限 */ #define CONTENT_MAX 4096
/* ========== 数据结构 ========== */ /* ========== 数据结构 ========== */
/** 单条日志消息 */ /** 单条日志消息 */
typedef struct log_msg { typedef struct
uint32_t level; {
char time_str[64]; uint32_t level;
char src_info[128]; /* "文件名/函数:行号" */ char time_str[64];
char content[CONTENT_MAX]; char src_info[128]; /* "文件名/函数:行号" */
struct log_msg *next; /* 空闲链表 / 队列链表 */ char content[CONTENT_MAX];
} log_msg_t; struct log_msg *next; /* 空闲链表 / 队列链表 */
} stru_log_msg;
/** 线程安全队列 */ /** 线程安全队列 */
typedef struct { typedef struct
log_msg_t *head, *tail; {
int count; stru_log_msg *head;
pthread_mutex_t lock; stru_log_msg *tail;
pthread_cond_t cond_pop; /* 消费者等待 */ int count;
pthread_cond_t cond_push; /* 生产者等待(队列满) */ pthread_mutex_t lock;
} msg_queue_t; pthread_cond_t cond_pop;
pthread_cond_t cond_push;
} stru_msg_queue;
/** 日志系统全局状态 */ /** 日志系统全局状态 */
LOCAL struct { typedef struct
msg_queue_t q; {
pthread_t worker; stru_msg_queue q;
volatile int running; pthread_t worker;
FILE *fp; volatile int running;
long file_bytes; FILE *fp;
int to_file; long file_bytes;
int to_console; int to_file;
char dir[256]; int to_console;
} g_log; char dir[256];
} stru_log_state;
LOCAL stru_log_state g_log;
/* ========== 消息池(预分配,无运行时 malloc) ========== */ /* ========== 消息池(预分配,无运行时 malloc) ========== */
LOCAL log_msg_t *g_pool_free; /* 空闲链表头 */ LOCAL stru_log_msg *g_pool_free;
LOCAL log_msg_t g_pool_buf[POOL_SIZE]; /* 预分配数组 */ LOCAL stru_log_msg g_pool_buf[POOL_SIZE];
LOCAL pthread_mutex_t g_pool_lock = PTHREAD_MUTEX_INITIALIZER; LOCAL pthread_mutex_t g_pool_lock = PTHREAD_MUTEX_INITIALIZER;
LOCAL pthread_once_t g_init_once = PTHREAD_ONCE_INIT; LOCAL pthread_once_t g_init_once = PTHREAD_ONCE_INIT;
/** 初始化消息池:把数组元素链成空闲链表 */ /** 初始化消息池:把数组元素链成空闲链表 */
LOCAL void pool_init(void) LOCAL void pool_init(void)
{ {
pthread_mutex_lock(&g_pool_lock); pthread_mutex_lock(&g_pool_lock);
if (g_pool_free != NULL) { pthread_mutex_unlock(&g_pool_lock); return; } if (g_pool_free != NULL)
for (int i = 0; i < POOL_SIZE - 1; i++) {
g_pool_buf[i].next = &g_pool_buf[i + 1]; pthread_mutex_unlock(&g_pool_lock);
g_pool_buf[POOL_SIZE - 1].next = NULL; return;
g_pool_free = &g_pool_buf[0]; }
pthread_mutex_unlock(&g_pool_lock); 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) */ /** 从池中取一个空闲消息块(失败返回 NULL) */
LOCAL log_msg_t *pool_alloc(void) LOCAL stru_log_msg *pool_alloc(void)
{ {
pthread_mutex_lock(&g_pool_lock); pthread_mutex_lock(&g_pool_lock);
log_msg_t *m = g_pool_free; stru_log_msg *m = g_pool_free;
if (m) g_pool_free = m->next; if (m)
pthread_mutex_unlock(&g_pool_lock); {
return 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; if (!m)
pthread_mutex_lock(&g_pool_lock); {
m->next = g_pool_free; return;
g_pool_free = m; }
pthread_mutex_unlock(&g_pool_lock); 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); pthread_mutex_lock(&g_log.q.lock);
while (g_log.q.count >= POOL_SIZE) while (g_log.q.count >= POOL_SIZE)
pthread_cond_wait(&g_log.q.cond_push, &g_log.q.lock); {
m->next = NULL; pthread_cond_wait(&g_log.q.cond_push, &g_log.q.lock);
if (!g_log.q.head) g_log.q.head = m; }
else g_log.q.tail->next = m; m->next = NULL;
g_log.q.tail = m; if (!g_log.q.head)
g_log.q.count++; {
pthread_cond_signal(&g_log.q.cond_pop); g_log.q.head = m;
pthread_mutex_unlock(&g_log.q.lock); }
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 */ /** 出队(空则等待或返回 NULL */
LOCAL log_msg_t *q_pop(void) LOCAL stru_log_msg *q_pop(void)
{ {
pthread_mutex_lock(&g_log.q.lock); pthread_mutex_lock(&g_log.q.lock);
while (g_log.q.count == 0 && g_log.running) 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; } pthread_cond_wait(&g_log.q.cond_pop, &g_log.q.lock);
log_msg_t *m = g_log.q.head; }
g_log.q.head = m->next; if (g_log.q.count == 0)
if (!g_log.q.head) g_log.q.tail = NULL; {
g_log.q.count--; pthread_mutex_unlock(&g_log.q.lock);
pthread_cond_signal(&g_log.q.cond_push); return NULL;
pthread_mutex_unlock(&g_log.q.lock); }
return m; 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 */ /** 拼接日志文件路径dir/app.log 或 dir/app.log.N */
LOCAL void file_path(char *buf, size_t sz, int idx) LOCAL void file_path(char *buf, size_t sz, int idx)
{ {
if (idx == 0) if (idx == 0)
snprintf(buf, sz, "%s/app.log", g_log.dir); {
else snprintf(buf, sz, "%s/app.log", g_log.dir);
snprintf(buf, sz, "%s/app.log.%d", g_log.dir, idx); }
else
{
snprintf(buf, sz, "%s/app.log.%d", g_log.dir, idx);
}
} }
/** 创建日志目录 */ /** 创建日志目录 */
LOCAL int ensure_dir(void) LOCAL int ensure_dir(void)
{ {
if (!g_log.dir[0]) { if (!g_log.dir[0])
/* 默认放到 /tmp/logs */ {
snprintf(g_log.dir, sizeof(g_log.dir), "/tmp/logs"); snprintf(g_log.dir, sizeof(g_log.dir), "/tmp/logs");
} }
struct stat st; struct stat st;
if (stat(g_log.dir, &st) != 0) if (stat(g_log.dir, &st) != 0)
return mkdir(g_log.dir, 0755); {
return 0; return mkdir(g_log.dir, 0755);
}
return 0;
} }
/** 轮转日志文件app.log→.1→.2→...→.N(max),删除最旧的 */ /** 轮转日志文件app.log→.1→.2→...→.N(max),删除最旧的 */
LOCAL void rotate_files(void) LOCAL void rotate_files(void)
{ {
char old[512], neo[512]; char old[512];
if (g_log.fp) { fclose(g_log.fp); g_log.fp = NULL; } char neo[512];
/* 删除最旧 */ if (g_log.fp)
file_path(old, sizeof(old), FILE_MAX_KEEP); {
remove(old); fclose(g_log.fp);
g_log.fp = NULL;
}
/* 依次后移 */ /* 删除最旧 */
for (int i = FILE_MAX_KEEP - 1; i >= 1; i--) { file_path(old, sizeof(old), FILE_MAX_KEEP);
file_path(old, sizeof(old), i); remove(old);
file_path(neo, sizeof(neo), i + 1);
rename(old, neo);
}
/* 当前文件 → .1 */ /* 依次后移 */
file_path(old, sizeof(old), 0); for (int i = FILE_MAX_KEEP - 1; i >= 1; i--)
file_path(neo, sizeof(neo), 1); {
rename(old, neo); file_path(old, sizeof(old), i);
g_log.file_bytes = 0; 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) LOCAL FILE *open_file(void)
{ {
if (ensure_dir() != 0) return NULL; if (ensure_dir() != 0)
char path[512]; {
file_path(path, sizeof(path), 0); return NULL;
}
char path[512];
file_path(path, sizeof(path), 0);
/* 检查已有文件大小 */ /* 检查已有文件大小 */
struct stat st; struct stat st;
if (stat(path, &st) == 0) { if (stat(path, &st) == 0)
if (st.st_size > FILE_MAX_SIZE) {
rotate_files(); if (st.st_size > FILE_MAX_SIZE)
else {
g_log.file_bytes = st.st_size; rotate_files();
} }
else
{
g_log.file_bytes = st.st_size;
}
}
FILE *fp = fopen(path, "a"); FILE *fp = fopen(path, "a");
if (fp) setlinebuf(fp); /* 行缓冲 */ if (fp)
return fp; {
setlinebuf(fp);
}
return fp;
} }
/* ========== 格式化 ========== */ /* ========== 格式化 ========== */
@ -206,172 +265,252 @@ LOCAL FILE *open_file(void)
/** 当前时间字符串YYYY-MM-DD HH:MM:SS.mmm */ /** 当前时间字符串YYYY-MM-DD HH:MM:SS.mmm */
LOCAL void fmt_time(char *buf, size_t sz) LOCAL void fmt_time(char *buf, size_t sz)
{ {
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
struct tm *t = localtime(&tv.tv_sec); struct tm *t = localtime(&tv.tv_sec);
if (t) { if (t)
snprintf(buf, sz, "%04d-%02d-%02d %02d:%02d:%02d.%03d", {
t->tm_year+1900, t->tm_mon+1, t->tm_mday, snprintf(buf, sz, "%04d-%02d-%02d %02d:%02d:%02d.%03d",
t->tm_hour, t->tm_min, t->tm_sec, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
(int)(tv.tv_usec / 1000)); t->tm_hour, t->tm_min, t->tm_sec,
} else { (int)(tv.tv_usec / 1000));
snprintf(buf, sz, "----.--.-- --:--:--.---"); }
} else
{
snprintf(buf, sz, "----.--.-- --:--:--.---");
}
} }
/** 提取文件名(去掉路径) 并拼接成 "file/func:line" */ /** 提取文件名(去掉路径) 并拼接成 "file/func:line" */
LOCAL void fmt_src(char *buf, size_t sz, LOCAL void fmt_src(char *buf, size_t sz,
const char *file, const char *func, uint32_t line) const char *file, const char *func, uint32_t line)
{ {
if (!file || !func) { snprintf(buf, sz, "?:?:0"); return; } if (!file || !func)
const char *p = strrchr(file, '/'); {
if (!p) p = strrchr(file, '\\'); snprintf(buf, sz, "?:?:0");
const char *name = p ? p + 1 : file; return;
snprintf(buf, sz, "%s/%s:%u", name, func, line); }
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; if (cnt == 0)
static const char *titles[] = {"INFO", "ERROR"}; {
static const char *colors[] = {COLOR_WHITE, COLOR_RED}; return;
}
static const char *titles[] = { "INFO", "ERROR" };
static const char *colors[] = { COLOR_WHITE, COLOR_RED };
/* 控制台(带颜色) */ /* 控制台(带颜色) */
if (g_log.to_console) { if (g_log.to_console)
for (int i = 0; i < cnt; i++) { {
log_msg_t *m = batch[i]; for (int i = 0; i < cnt; i++)
printf("%s[%s] [%s%s%s] %s\n", {
colors[m->level], m->time_str, stru_log_msg *m = batch[i];
colors[m->level], titles[m->level], printf("%s[%s] [%s%s%s] %s\n",
COLOR_RESET, m->content); colors[m->level], m->time_str,
} colors[m->level], titles[m->level],
fflush(stdout); COLOR_RESET, m->content);
} }
fflush(stdout);
}
/* 文件 */ /* 文件 */
if (g_log.to_file) { if (g_log.to_file)
if (!g_log.fp) g_log.fp = open_file(); {
if (g_log.fp) { if (!g_log.fp)
for (int i = 0; i < cnt; i++) { {
log_msg_t *m = batch[i]; g_log.fp = open_file();
int n = fprintf(g_log.fp, "[%s] [%s] %s\n", }
titles[m->level], m->time_str, m->content); if (g_log.fp)
if (n > 0) g_log.file_bytes += n; {
} for (int i = 0; i < cnt; i++)
fflush(g_log.fp); {
if (g_log.file_bytes > FILE_MAX_SIZE) rotate_files(); 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) LOCAL void *worker_thread(void *arg)
{ {
(void)arg; (void)arg;
while (g_log.running) { while (g_log.running)
log_msg_t *batch[BATCH_MAX]; {
int cnt = 0; stru_log_msg *batch[BATCH_MAX];
int cnt = 0;
/* 批量取消息 */ /* 批量取消息 */
for (int i = 0; i < BATCH_MAX; i++) { for (int i = 0; i < BATCH_MAX; i++)
log_msg_t *m = q_pop(); {
if (!m) break; stru_log_msg *m = q_pop();
batch[cnt++] = m; if (!m)
} {
break;
}
batch[cnt++] = m;
}
if (cnt > 0) { if (cnt > 0)
output_batch(batch, cnt); {
for (int i = 0; i < cnt; i++) pool_free(batch[i]); output_batch(batch, cnt);
} else { for (int i = 0; i < cnt; i++)
usleep(FLUSH_MS * 1000); {
} pool_free(batch[i]);
} }
return NULL; }
else
{
usleep(FLUSH_MS * 1000);
}
}
return NULL;
} }
/* ========== 全局初始化(pthread_once) ========== */ /* ========== 全局初始化(pthread_once) ========== */
LOCAL void global_init(void) LOCAL void global_init(void)
{ {
pool_init(); pool_init();
/* g_log.q 已在声明时用 {0} 初始化 */ /* g_log.q 已在声明时用 {0} 初始化 */
g_log.q.head = g_log.q.tail = NULL; g_log.q.head = NULL;
g_log.q.count = 0; g_log.q.tail = NULL;
g_log.to_console = 1; g_log.q.count = 0;
g_log.to_file = 0; g_log.to_console = 1;
g_log.running = 1; g_log.to_file = 0;
pthread_create(&g_log.worker, NULL, worker_thread, NULL); g_log.running = 1;
pthread_create(&g_log.worker, NULL, worker_thread, NULL);
} }
/* ========== 公开 API ========== */ /* ========== 公开 API ========== */
int log_init(const char *dir) int log_init(const char *dir)
{ {
if (dir && dir[0]) if (dir && dir[0])
snprintf(g_log.dir, sizeof(g_log.dir), "%s", dir); {
pool_init(); /* 确保消息池就绪 */ snprintf(g_log.dir, sizeof(g_log.dir), "%s", dir);
g_log.to_file = 1; }
return 0; pool_init();
g_log.to_file = 1;
return 0;
} }
void log_prt(uint32_t level, const char *file, const char *func, void log_prt(uint32_t level, const char *file, const char *func,
uint32_t line, const char *fmt, ...) uint32_t line, const char *fmt, ...)
{ {
/* 懒初始化 */ /* 懒初始化 */
pthread_once(&g_init_once, global_init); pthread_once(&g_init_once, global_init);
if (level > LOG_ERROR || !fmt || !g_log.running) return; if (level > LOG_ERROR || !fmt || !g_log.running)
{
return;
}
/* 从消息池取空闲块 */ /* 从消息池取空闲块 */
log_msg_t *m = pool_alloc(); stru_log_msg *m = pool_alloc();
if (!m) return; /* 池耗尽,丢弃本条 */ if (!m)
{
return;
}
m->level = level; m->level = level;
fmt_time(m->time_str, sizeof(m->time_str)); fmt_time(m->time_str, sizeof(m->time_str));
fmt_src(m->src_info, sizeof(m->src_info), file, func, line); fmt_src(m->src_info, sizeof(m->src_info), file, func, line);
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
int pos = snprintf(m->content, sizeof(m->content), "[%s] ", m->src_info); int pos = snprintf(m->content, sizeof(m->content), "[%s] ", m->src_info);
if (pos < (int)sizeof(m->content)) if (pos < (int)sizeof(m->content))
vsnprintf(m->content + pos, sizeof(m->content) - pos, fmt, ap); {
va_end(ap); 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_on(void)
void log_file_off(void) { g_log.to_file = 0; } {
void log_console_on(void) { g_log.to_console = 1; } g_log.to_file = 1;
void log_console_off(void) { g_log.to_console = 0; } }
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) void log_flush(void)
{ {
/* 轮询等待队列归零 */ for (;;)
for (;;) { {
pthread_mutex_lock(&g_log.q.lock); pthread_mutex_lock(&g_log.q.lock);
int c = g_log.q.count; int c = g_log.q.count;
pthread_mutex_unlock(&g_log.q.lock); pthread_mutex_unlock(&g_log.q.lock);
if (c == 0) break; if (c == 0)
usleep(10000); {
} break;
}
usleep(10000);
}
} }
void log_cleanup(void) void log_cleanup(void)
{ {
if (!g_log.running) return; if (!g_log.running)
g_log.running = 0; {
pthread_cond_signal(&g_log.q.cond_pop); return;
pthread_join(g_log.worker, NULL); }
if (g_log.fp) { fclose(g_log.fp); g_log.fp = NULL; } g_log.running = 0;
/* 释放池中残留消息 */ pthread_cond_signal(&g_log.q.cond_pop);
pthread_mutex_lock(&g_log.q.lock); pthread_join(g_log.worker, NULL);
log_msg_t *cur = g_log.q.head, *tmp; if (g_log.fp)
while (cur) { tmp = cur; cur = cur->next; pool_free(tmp); } {
g_log.q.head = g_log.q.tail = NULL; fclose(g_log.fp);
g_log.q.count = 0; g_log.fp = NULL;
pthread_mutex_unlock(&g_log.q.lock); }
/* 释放池中残留消息 */
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);
} }