当前位置: 首页 > 工具软件 > libhv > 使用案例 >

libhv 日志

武琛
2023-12-01

日志快捷输出可以参考:C++ 日志输出

libhv日志封装

#pragma once
#include <hv/hlog.h>

#ifdef _ENBLE_LOG4CPLUS
#include "LogUtil.h"
#endif

class XLog
{
public:
	static XLog& Instance()
	{
		static XLog instance;
		return instance;
	}

	logger_t* GetLogger() {
		return m_log;
	}

	void SetLevel(int log_level = LOG_LEVEL_DEBUG) {
		logger_set_level(m_log, log_level);
	}
protected:
	XLog()
	{
		m_log = logger_create();
		logger_set_file(m_log, "./log/xyBar.log");
	}

private:
	logger_t* m_log = nullptr;
};



#ifndef _ENBLE_LOG4CPLUS

#define logger_stream_print(level,info_ostream)\
{\
	std::stringstream ss; \
	ss << info_ostream; \
	logger_print(XLog::Instance().GetLogger(), level, "[%s:%d:%s] " "%s" "\n", __FILENAME__, __LINE__, __FUNCTION__,ss.str().c_str());\
}

	#define XLOG_LEVEL(level) XLog::Instance().SetLevel(level);
	//格式化日志XLOGD("123%d",123);
	#define XLOGD(fmt, ...) logger_print(XLog::Instance().GetLogger(), LOG_LEVEL_DEBUG, "[%s:%d:%s] " fmt "\n",  __FILENAME__, __LINE__, __FUNCTION__,## __VA_ARGS__)
	#define XLOGI(fmt, ...) logger_print(XLog::Instance().GetLogger(), LOG_LEVEL_INFO,  "[%s:%d:%s] " fmt "\n",  __FILENAME__, __LINE__, __FUNCTION__,## __VA_ARGS__)
	#define XLOGW(fmt, ...) logger_print(XLog::Instance().GetLogger(), LOG_LEVEL_WARN,	"[%s:%d:%s] " fmt "\n",  __FILENAME__, __LINE__, __FUNCTION__,## __VA_ARGS__)
	#define XLOGE(fmt, ...) logger_print(XLog::Instance().GetLogger(), LOG_LEVEL_ERROR, "[%s:%d:%s] " fmt "\n",  __FILENAME__, __LINE__, __FUNCTION__,## __VA_ARGS__)
	#define XLOGF(fmt, ...) logger_print(XLog::Instance().GetLogger(), LOG_LEVEL_FATAL, "[%s:%d:%s] " fmt "\n",  __FILENAME__, __LINE__, __FUNCTION__,## __VA_ARGS__)
	//流式日志 XLOGD_O("123"<<123);
	#define XLOGD_O(info_ostream) logger_stream_print(LOG_LEVEL_DEBUG,info_ostream);
	#define XLOGI_O(info_ostream) logger_stream_print(LOG_LEVEL_INFO,info_ostream);
	#define XLOGW_O(info_ostream) logger_stream_print(LOG_LEVEL_WARN,info_ostream);
	#define XLOGE_O(info_ostream) logger_stream_print(LOG_LEVEL_ERROR,info_ostream);
	#define XLOGF_O(info_ostream) logger_stream_print(LOG_LEVEL_FATAL,info_ostream);
#else
	#define XLOGD(...) LOG_DEBUG_F(__VA_ARGS__)
	#define XLOGI(...) LOG_INFO_F(__VA_ARGS__)
	#define XLOGW(...) LOG_WARN_F(__VA_ARGS__)
	#define XLOGE(...) LOG_ERROR_F(__VA_ARGS__)
	#define XLOGF(...) LOG_FATAL_F(__VA_ARGS__)

	#define XLOGD_O(info_ostream) LOG_DEBUG(__VA_ARGS__);
	#define XLOGI_O(info_ostream) LOG_INFO(__VA_ARGS__);
	#define XLOGW_O(info_ostream) LOG_WARN(__VA_ARGS__);
	#define XLOGE_O(info_ostream) LOG_ERROR(__VA_ARGS__);
	#define XLOGF_O(info_ostream) LOG_FATAL(__VA_ARGS__);
#endif

使用

//格式化日志
XLOGI("app start,cmdLine:%s",lpCmdLine ? lpCmdLine:"");
//流式日志
XLOGI_O("app start,cmdLine:"<<(lpCmdLine ? lpCmdLine:""));
 类似资料: