新写的一个日志接口
主要是为了记录宏中使用可变参数。如果没有可变参数,也不会有逗号的影响。 主要是##grgs的使用,至于具体gnu gcc版本对可变参数的支持程度,尚未进行充分测试。
头文件
#pragma once #include <string> typedef void (*LogCallBack)(unsigned short logType, const char* log); void set_log_callback(LogCallBack callback); void set_log_type_of_info(unsigned short logType); void set_log_type_of_trace(unsigned short logType); void set_log_type_of_debug(unsigned short logType); void set_log_type_of_error(unsigned short logType); void log_write(const char* file, const char* function, int line, unsigned short logType, const char* fmt, ...); void log_info(const char* file, const char* function, int line, const char* fmt, ...); void log_trace(const char* file, const char* function, int line, const char* fmt, ...); void log_debug(const char* file, const char* function, int line, const char* fmt, ...); void log_error(const char* file, const char* function, int line, const char* fmt, ...); #define log_WRITE(logType, format, args...) \ log_write(__FILE__,__FUNCTION__, __LINE__, logType, format, ##args) #define log_INFO(format, args...) \ log_info(__FILE__,__FUNCTION__, __LINE__, format, ##args) #define log_TRACE(format, args...) \ log_trace(__FILE__,__FUNCTION__, __LINE__, format, ##args) #define log_DEBUG(format, args...) \ log_debug(__FILE__,__FUNCTION__, __LINE__, format, ##args) #define log_ERROR(format, args...) \ log_error(__FILE__,__FUNCTION__, __LINE__, format, ##args)
源文件:
#include "Log.h" #include <stdarg.h> #include <sstream> #include <stdio.h> #include "string.h" using namespace std; static LogCallBack callback_function = NULL; static unsigned short log_info = 0; static unsigned short log_trace = 0; static unsigned short log_debug = 0; static unsigned short log_error = 0; static void write_log(unsigned short logType, const char* file, const char* function, int line, const char* fmt, va_list args); void log_write(const char* file, const char* function, int line, unsigned short logType, const char* fmt, ...) { if (NULL == callback_function) { return; } va_list arguments; va_start(arguments, fmt); write_log(logType, file, function, line, fmt, arguments); va_end(arguments); } void log_info(const char* file, const char* function, int line, const char* fmt, ...) { if (NULL == callback_function) { return; } va_list arguments; va_start(arguments, fmt); write_log(log_info, file, function, line, fmt, arguments); va_end(arguments); } void log_trace(const char* file, const char* function, int line, const char* fmt, ...) { if (NULL == callback_function) { return; } va_list arguments; va_start(arguments, fmt); write_log(log_trace, file, function, line, fmt, arguments); va_end(arguments); } void log_debug(const char* file, const char* function, int line, const char* fmt, ...) { if (NULL == callback_function) { return; } va_list arguments; va_start(arguments, fmt); write_log(log_debug, file, function, line, fmt, arguments); va_end(arguments); } void log_error(const char* file, const char* function, int line, const char* fmt, ...) { if (NULL == callback_function) { return; } va_list arguments; va_start(arguments, fmt); write_log(log_error, file, function, line, fmt, arguments); va_end(arguments); } void set_log_callback(LogCallBack callback) { callback_function = callback; } void set_log_type_of_info(unsigned short logType) { log_info = logType; } void set_log_type_of_debug(unsigned short logType) { log_debug = logType; } void set_log_type_of_trace(unsigned short logType) { log_trace = logType; } void set_log_type_of_error(unsigned short logType) { log_error = logType; } static void write_log(unsigned short logType, const char* file, const char* function, int line, const char* fmt, va_list args) { char chArr[2048]; memset(chArr, 0 , sizeof(chArr)); vsnprintf(chArr, 2047, fmt, args); stringstream oss; oss<<file<<"::"<<function<<"::"<<line<<"::"<<chArr<<"\n"<<endl; callback_function(logType, oss.str().c_str()); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?