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

- 一行只能有一条语句
- 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,88 +12,156 @@
#include <stddef.h>
#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))
/* ---- 遍历 ---- */
#define list_for_each(pos, head) \
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))
#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))
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;
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);
}
}

View File

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

View File

@ -9,28 +9,69 @@
#include <time.h>
#include <sys/time.h>
#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];
typedef struct
{
uint64_t flag;
char ver[16];
char author[16];
char modify[32];
uint16_t crc;
char desc[128], bak[54];
} file_info_t;
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

View File

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

View File

@ -9,28 +9,69 @@
#include <time.h>
#include <sys/time.h>
#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];
typedef struct
{
uint64_t flag;
char ver[16];
char author[16];
char modify[32];
uint16_t crc;
char desc[128], bak[54];
} file_info_t;
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

File diff suppressed because it is too large Load Diff

View File

@ -12,88 +12,156 @@
#include <stddef.h>
#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))
/* ---- 遍历 ---- */
#define list_for_each(pos, head) \
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))
#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))
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;
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);
}
}

View File

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

View File

@ -22,36 +22,40 @@
#include <stdlib.h>
/* ========== 可配置参数 ========== */
#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 {
typedef struct
{
uint32_t level;
char time_str[64];
char src_info[128]; /* "文件名/函数:行号" */
char content[CONTENT_MAX];
struct log_msg *next; /* 空闲链表 / 队列链表 */
} log_msg_t;
} stru_log_msg;
/** 线程安全队列 */
typedef struct {
log_msg_t *head, *tail;
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; /* 生产者等待(队列满) */
} msg_queue_t;
pthread_cond_t cond_pop;
pthread_cond_t cond_push;
} stru_msg_queue;
/** 日志系统全局状态 */
LOCAL struct {
msg_queue_t q;
typedef struct
{
stru_msg_queue q;
pthread_t worker;
volatile int running;
FILE *fp;
@ -59,11 +63,13 @@ LOCAL struct {
int to_file;
int to_console;
char dir[256];
} g_log;
} 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 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;
@ -71,28 +77,40 @@ 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; }
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;
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;
if (!m)
{
return;
}
pthread_mutex_lock(&g_pool_lock);
m->next = g_pool_free;
g_pool_free = m;
@ -102,14 +120,22 @@ LOCAL void pool_free(log_msg_t *m)
/* ========== 队列操作 ========== */
/** 入队(满则阻塞) */
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;
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);
@ -117,15 +143,24 @@ LOCAL void q_push(log_msg_t *m)
}
/** 出队(空则等待或返回 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;
}
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;
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);
@ -138,36 +173,49 @@ LOCAL log_msg_t *q_pop(void)
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);
}
}
/** 创建日志目录 */
LOCAL int ensure_dir(void)
{
if (!g_log.dir[0]) {
/* 默认放到 /tmp/logs */
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];
if (g_log.fp)
{
fclose(g_log.fp);
g_log.fp = NULL;
}
/* 删除最旧 */
file_path(old, sizeof(old), FILE_MAX_KEEP);
remove(old);
/* 依次后移 */
for (int i = FILE_MAX_KEEP - 1; i >= 1; i--) {
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);
@ -183,21 +231,32 @@ LOCAL void rotate_files(void)
/** 打开当前日志文件 */
LOCAL FILE *open_file(void)
{
if (ensure_dir() != 0) return NULL;
if (ensure_dir() != 0)
{
return NULL;
}
char path[512];
file_path(path, sizeof(path), 0);
/* 检查已有文件大小 */
struct stat st;
if (stat(path, &st) == 0) {
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); /* 行缓冲 */
if (fp)
{
setlinebuf(fp);
}
return fp;
}
@ -209,12 +268,15 @@ LOCAL void fmt_time(char *buf, size_t sz)
struct timeval tv;
gettimeofday(&tv, NULL);
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,
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 {
}
else
{
snprintf(buf, sz, "----.--.-- --:--:--.---");
}
}
@ -223,9 +285,16 @@ LOCAL void fmt_time(char *buf, size_t sz)
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; }
if (!file || !func)
{
snprintf(buf, sz, "?:?:0");
return;
}
const char *p = strrchr(file, '/');
if (!p) p = strrchr(file, '\\');
if (!p)
{
p = strrchr(file, '\\');
}
const char *name = p ? p + 1 : file;
snprintf(buf, sz, "%s/%s:%u", name, func, line);
}
@ -233,16 +302,21 @@ LOCAL void fmt_src(char *buf, size_t sz,
/* ========== 输出 ========== */
/** 批量写控制台 + 文件 */
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];
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],
@ -252,17 +326,29 @@ LOCAL void output_batch(log_msg_t **batch, int cnt)
}
/* 文件 */
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];
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;
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.file_bytes > FILE_MAX_SIZE)
{
rotate_files();
}
}
}
}
@ -272,21 +358,32 @@ LOCAL void output_batch(log_msg_t **batch, int cnt)
LOCAL void *worker_thread(void *arg)
{
(void)arg;
while (g_log.running) {
log_msg_t *batch[BATCH_MAX];
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;
for (int i = 0; i < BATCH_MAX; i++)
{
stru_log_msg *m = q_pop();
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]);
} else {
for (int i = 0; i < cnt; i++)
{
pool_free(batch[i]);
}
}
else
{
usleep(FLUSH_MS * 1000);
}
}
@ -299,7 +396,8 @@ LOCAL void global_init(void)
{
pool_init();
/* g_log.q 已在声明时用 {0} 初始化 */
g_log.q.head = g_log.q.tail = NULL;
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;
@ -312,8 +410,10 @@ LOCAL void global_init(void)
int log_init(const char *dir)
{
if (dir && dir[0])
{
snprintf(g_log.dir, sizeof(g_log.dir), "%s", dir);
pool_init(); /* 确保消息池就绪 */
}
pool_init();
g_log.to_file = 1;
return 0;
}
@ -323,11 +423,17 @@ void log_prt(uint32_t level, const char *file, const char *func,
{
/* 懒初始化 */
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();
if (!m) return; /* 池耗尽,丢弃本条 */
stru_log_msg *m = pool_alloc();
if (!m)
{
return;
}
m->level = level;
fmt_time(m->time_str, sizeof(m->time_str));
@ -337,41 +443,74 @@ void log_prt(uint32_t level, const char *file, const char *func,
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);
}
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 (;;) {
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;
if (c == 0)
{
break;
}
usleep(10000);
}
}
void log_cleanup(void)
{
if (!g_log.running) return;
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; }
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;
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);
}