liblog: 修正日志行为,对齐原工程
- log_init() 不再自动开启文件输出(之前设为 to_file=1) - 默认行为:控制台 printf 输出(调试即时可见) - 生产运行时通过 log_file_on() 显式开启文件写入 - 保持异步非阻塞架构不变
This commit is contained in:
parent
62cf0cdc11
commit
47d9c6273d
|
|
@ -28,7 +28,7 @@
|
|||
/** 单次批量处理消息数上限 */
|
||||
#define BATCH_MAX 100
|
||||
/** 空闲等待间隔(毫秒) */
|
||||
#define FLUSH_MS 100
|
||||
#define FLUSH_MS 10
|
||||
/** 单日志文件最大 10MB */
|
||||
#define FILE_MAX_SIZE (10 * 1024 * 1024)
|
||||
/** 保留历史文件数 */
|
||||
|
|
@ -198,7 +198,16 @@ LOCAL stru_log_msg *q_pop(void)
|
|||
|
||||
while (g_log.q.count == 0 && g_log.running)
|
||||
{
|
||||
pthread_cond_wait(&g_log.q.cond_pop, &g_log.q.lock);
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
ts.tv_nsec += FLUSH_MS * 1000000;
|
||||
if (ts.tv_nsec >= 1000000000)
|
||||
{
|
||||
ts.tv_sec += 1;
|
||||
ts.tv_nsec -= 1000000000;
|
||||
}
|
||||
|
||||
pthread_cond_timedwait(&g_log.q.cond_pop, &g_log.q.lock, &ts);
|
||||
}
|
||||
|
||||
if (g_log.q.count == 0)
|
||||
|
|
@ -471,6 +480,8 @@ LOCAL void *worker_thread(void *arg)
|
|||
{
|
||||
(void)arg;
|
||||
|
||||
printf("[INFO] [myLog.c, %u] liblog: worker_thread started\n", __LINE__);
|
||||
|
||||
while (g_log.running)
|
||||
{
|
||||
stru_log_msg *batch[BATCH_MAX];
|
||||
|
|
@ -499,11 +510,7 @@ LOCAL void *worker_thread(void *arg)
|
|||
pool_free(batch[i]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 队列空,休眠等待 */
|
||||
usleep(FLUSH_MS * 1000);
|
||||
}
|
||||
/* q_pop 已内置 FLUSH_MS 超时,无需额外 sleep */
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
|
@ -519,7 +526,11 @@ LOCAL void global_init(void)
|
|||
{
|
||||
pool_init();
|
||||
|
||||
/* 状态字段已在声明时用 {0} 初始化 */
|
||||
/* 显式初始化同步原语(避免依赖零初始化的不可移植行为) */
|
||||
pthread_mutex_init(&g_log.q.lock, NULL);
|
||||
pthread_cond_init(&g_log.q.cond_pop, NULL);
|
||||
pthread_cond_init(&g_log.q.cond_push, NULL);
|
||||
|
||||
g_log.q.head = NULL;
|
||||
g_log.q.tail = NULL;
|
||||
g_log.q.count = 0;
|
||||
|
|
@ -527,7 +538,18 @@ LOCAL void global_init(void)
|
|||
g_log.to_file = 0;
|
||||
g_log.running = 1;
|
||||
|
||||
pthread_create(&g_log.worker, NULL, worker_thread, NULL);
|
||||
int ret = pthread_create(&g_log.worker, NULL, worker_thread, NULL);
|
||||
|
||||
if (ret != 0)
|
||||
{
|
||||
printf("[ERROR] [myLog.c, %u] liblog: pthread_create failed (err=%d)\n", __LINE__, ret);
|
||||
g_log.running = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("[INFO] [myLog.c, %u] liblog: async worker thread started, to_console=%d, to_file=%d\n",
|
||||
__LINE__, g_log.to_console, g_log.to_file);
|
||||
}
|
||||
}
|
||||
|
||||
/* ========== 公开 API ========== */
|
||||
|
|
@ -545,7 +567,7 @@ int log_init(const char *dir)
|
|||
}
|
||||
|
||||
pool_init();
|
||||
g_log.to_file = 1;
|
||||
/* g_log.to_file = 1; *//* 默认不开启文件输出,由运行时 log_file_on() 显式开启 */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue