RTU_ALL_AI/release/inc/myLog.h

69 lines
1.9 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 时自动初始化,无需手动 init。
*/
#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