RTU_ALL_AI/release/inc/myLog.h

70 lines
2.0 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.

/**
* @file myLog.h
* @brief 异步日志系统接口(纯 C
* @details 支持 INFO/ERROR 两级日志,非阻塞写入。
* 首次调用 log_prt 时自动初始化pthread_once 懒启动),无需手动 init。
* 控制台支持彩色 ANSI 输出,文件支持按大小自动轮转。
*/
#ifndef _MY_LOG_H_
#define _MY_LOG_H_
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif
/** 日志级别INFO普通信息白色输出 */
#define LOG_INFO 0
/** 日志级别ERROR错误信息红色输出 */
#define LOG_ERROR 1
/**
* @brief 手动初始化日志系统(可选,首次 log_prt 会自动触发)
* @param dir 日志文件目录路径,传 NULL 则使用默认 /tmp/logs
* @return 0 成功
*/
int log_init(const char *dir);
/**
* @brief 核心日志打印函数(非阻塞,消息入队列后立即返回)
* @param level 日志级别LOG_INFO 或 LOG_ERROR
* @param file 源文件名(通常传 __FILE__
* @param func 函数名(通常传 __FUNCTION__
* @param line 行号(通常传 __LINE__
* @param fmt printf 格式化字符串
* @param ... 可变参数
*/
void log_prt(uint32_t level, const char *file, const char *func,
uint32_t line, const char *fmt, ...);
/** 开启日志文件写入 */
void log_file_on(void);
/** 关闭日志文件写入 */
void log_file_off(void);
/** 开启控制台彩色输出 */
void log_console_on(void);
/** 关闭控制台输出 */
void log_console_off(void);
/** 等待异步队列清空(阻塞直到所有消息写入完成) */
void log_flush(void);
/** 停止工作线程、关闭文件、释放消息池 */
void log_cleanup(void);
/** INFO 级别便捷日志宏 */
#define LOG_I(fmt, ...) log_prt(LOG_INFO, __FILE__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
/** ERROR 级别便捷日志宏 */
#define LOG_E(fmt, ...) log_prt(LOG_ERROR, __FILE__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
#ifdef __cplusplus
}
#endif
#endif