cocoon/log.h
xfy911 0dc2bb5b4e feat(server): 连接空闲超时 + 最大并发限制 + 分级日志
- 新增 log.c/log.h 分级日志系统(error/warn/info/debug)
- 新增连接空闲超时管理:使用 coco_timer + shutdown + coco_cancel
  自动清理长时间无数据的僵尸连接,默认 30 秒
- 新增最大并发连接数限制(-m <num>),超限返回 503
- 命令行新增选项:-m 最大连接数, -o 超时毫秒, -l 日志级别
- 替换所有 printf 为 log_info/log_error/log_debug 等分级输出
- 编译通过,单线程/多线程模式均正常
2026-06-03 18:13:29 +08:00

56 lines
1.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* log.h - 简单日志系统
*
* 支持分级日志输出ERROR、WARN、INFO、DEBUG。
* 线程安全(使用 printf底层为线程安全
*
* @author xfy
*/
#ifndef COCOON_LOG_H
#define COCOON_LOG_H
#include <stdbool.h>
/* === 日志级别 === */
typedef enum {
LOG_LEVEL_ERROR = 0, /**< 错误,程序可能无法继续 */
LOG_LEVEL_WARN = 1, /**< 警告,程序可继续但需注意 */
LOG_LEVEL_INFO = 2, /**< 信息,正常运行输出 */
LOG_LEVEL_DEBUG = 3, /**< 调试,详细运行状态 */
} log_level_t;
/**
* log_set_level - 设置全局日志级别
*
* 低于此级别的日志将被忽略。
*
* @param level 最低输出级别
*/
void log_set_level(log_level_t level);
/**
* log_set_prefix - 设置日志前缀(默认 [Cocoon]
*
* @param prefix 前缀字符串(如 "[Cocoon]"NULL 表示无前缀
*/
void log_set_prefix(const char *prefix);
/**
* log_get_level - 获取当前日志级别
* @return 当前日志级别
*/
log_level_t log_get_level(void);
/**
* log_error / log_warn / log_info / log_debug - 分级日志输出
*
* 使用 printf 风格格式化字符串,自动追加换行。
*/
void log_error(const char *fmt, ...);
void log_warn(const char *fmt, ...);
void log_info(const char *fmt, ...);
void log_debug(const char *fmt, ...);
#endif /* COCOON_LOG_H */