ZBLOG

#pragma once #ifndef __LOG_H__ #define __LOG_H__ #include #include #include #include #include #include "Database.h" #include #include "ConnectionPool.h" #define MAX_SIZE 50*1024*1024 //...

这是一个C++的日志库,其中包含了一些宏定义和Log类的声明。该日志库支持不同级别的日志输出,并且可以将日志保存到文件中。

在使用该库之前,需要包含相应的头文件(”Database.h”、和”ConnectionPool.h”)以及命名空间CFX::Logger。

以下是该代码片段的摘要:

#pragma once
#ifndef __LOG_H__
#define __LOG_H__

#include <string>
#include <fstream>
#include <map>
#include <mutex>
#include <thread>
#include "Database.h"
#include <mysql/mysql.h>
#include "ConnectionPool.h"

// 定义日志输出的宏

// 日志等级为DEBUG
#define debug(format, ...) \
	Log::instance()->log(Log::F_DEBUG, __FUNCTION__, __LINE__, format, ##__VA_ARGS__);

// 日志等级为INFO
#define info(format, ...) \
	Log::instance()->log(Log::F_INFO, __FUNCTION__, __LINE__, format, ##__VA_ARGS__);

// 日志等级为WARN
#define warn(format, ...) \
	Log::instance()->log(Log::F_WARN, __FUNCTION__, __LINE__, format, ##__VA_ARGS__);

// 日志等级为ERROR
#define error(format, ...) \
	Log::instance()->log(Log::F_ERROR, __FUNCTION__, __LINE__, format, ##__VA_ARGS__);

// 日志等级为FATAL
#define fatal(format, ...) \
	Log::instance()->log(Log::F_FATAL, __FUNCTION__, __LINE__, format, ##__VA_ARGS__);

namespace CFX {
	namespace Logger {
		// Log类定义

		class Log {
		public:
			enum Level {
				F_DEBUG = 0,
				F_INFO,
				F_WARN,
				F_ERROR,
				F_FATAL,
				LEVEL_COUNT,
			};
			
			// 单例模式获取Log实例
			static Log* instance();
			
			// 连接到数据库
			void connectSql();
			
			// 打开日志文件
			void open(const string& filename);
			
			// 关闭日志文件
			void close();
			
			// 记录日志的函数,根据传入的level进行相应级别的输出
			void log(Level level, const std::string& functionname, int line, const char* format, ...);
			
			// 设置日志等级
			void setLevel(Level level);

			// 设置最大长度
            void setmax();

		private:
		    // 构造函数和析构函数私有化,保证单例模式和不可外部创建对象
		    
		    // 日志翻滚,超过最大长度保存到另一个文件中
		    void rotate();

		    // 私有成员变量和方法

		    string m_filename;
		    ofstream m_fout;
		    Level m_level;
		    Level m_Flevel;
		    mutex m_mutex;
		    string m_functionname;
		    int m_line;
		    int m_max; 
	        string m_logContent;
            streamoff m_len;

            static const char* s_level[LEVEL_COUNT];
            static Log* m_instance;

        };
    }
}

#endif  // __LOG_H__

这段代码定义了一个名为Log的类,具有连接到数据库、打开关闭日志文件、记录日志等功能。同时提供了一些宏定义,用于方便地输出不同级别的日志。

需要注意的是,该代码中使用了一些外部依赖库(”Database.h”、和”ConnectionPool.h”),在使用之前需要确保这些库已正确引入并配置。

希望以上信息能对您有所帮助。如果还有其他问题,请随时提问。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=11178

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?