C++实现日志

日志

 日志一般用于记录系统运行异常信息、状态信息、性能指标

级别

 ERROR(错误):此信息输出后,主体系统核心模块不能正常工作,需要修复才能正常工作。
 WARN(警告):此信息输出后,系统一般模块存在问题,不影响系统运行。
 INFO(通知):此信息输出后,主要是记录系统运行状态等关联信息。
 DEBUG(调试):最细粒度的输出,除却上面各种情况后,你希望输出的相关信息,都可以在这里输出。
 TRACE(跟踪):最细粒度的输出,除却上面各种情况后,你希望输出的相关信息,都可以在这里输出。

日志模块实现:
/*
 *logger.h
 *@brief:日志模块
 */
#ifndef _logger_
#define _logger_

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <stdint.h>
 ///\ brief日志文件类型
typedef enum log_rank
{
	INFO,
	WARRING,
	ERROR,
	FATAL
} log_rank_t;

/// \brief 初始化日志文件
void init_logger(const std::string & info_long_filename,
				 const std::string & warn_long_filename,
				 const std::string & error_long_filename);
///\ brief 日志文件类
class Logger
{
	friend void init_logger(const std::string & info_long_filename,
							const std::string & warn_long_filename,
							const std::string & error_long_filename);
public:

	static Logger *Log(log_rank_t log_rank)
	{
		static Logger log;
		log.set_log_rank(log_rank);
		return &log;
	}

	Logger();
	~Logger();
	/// \brief 写入日志前,先记录源文件名称、行号、函数名
	/// \brief log_rank 日志级别
	/// \brief line 行号
	/// \brief function 函数
	static std::ostream & start_logger(log_rank_t log_rank, const int & line, const std::string & function);

	void set_log_rank(log_rank_t log_rank) { m_log_rank = log_rank; }

private:
	///\ brief 根据等级获取日志输出流
	static std::ostream & get_stream(log_rank_t long_rank);

	static std::ofstream  info_log_file;			///< 运行信息输出流
	static std::ofstream  warn_log_file;			///< 警告信息输出流
	static std::ofstream  error_log_file;			///< 错误信息输出流

	log_rank_t m_log_rank;							///< 日志级别
};

#define LOG(log_rank)										\
Logger::Log(log_rank)->start_logger(log_rank, __LINE__, __FUNCTION__)

#endif // _logger_

std::ofstream Logger::info_log_file;
std::ofstream Logger::warn_log_file;
std::ofstream Logger::error_log_file;

Logger::Logger()
{
	init_logger("./info.txt", "./warn.txt", "./error.txt");
}

Logger::~Logger()
{
	get_stream(m_log_rank) << std::endl << std::flush;

	if (FATAL == m_log_rank) {
		info_log_file.close();
		warn_log_file.close();
		error_log_file.close();
		abort();
	}
}

std::ostream & Logger::start_logger(log_rank_t log_rank, const int & line, const std::string & function)
{
	time_t t;
	time(&t);
	char time_str[128];
	ctime_s(time_str, sizeof(time_str), &t);
	time_str[strlen(time_str) - 1] = '\0';
	std::string log_rank_str = (log_rank == INFO) ? ("INFO") : ((log_rank == WARRING) ? "WARRING" : "ERROR");
	return get_stream(log_rank) << std::endl
		   << "[" << time_str << " " << log_rank_str
		   << " function:" << function << " " << "line:" << line << " ]:";
}

std::ostream & Logger::get_stream(log_rank_t long_rank)
{
	bool b = info_log_file.is_open();
	return (INFO == long_rank) ? (info_log_file.is_open() ? info_log_file : std::cout)
		: ((WARRING == long_rank) ? (warn_log_file.is_open() ? warn_log_file : std::cerr)
			: (error_log_file.is_open() ? error_log_file : std::cerr));
}

void init_logger(const std::string & info_long_filename, const std::string & warn_long_filename, const std::string & error_long_filename)
{
	Logger::info_log_file.open(info_long_filename.c_str(), std::ios::app);
	Logger::warn_log_file.open(warn_long_filename.c_str(), std::ios::app);
	Logger::error_log_file.open(error_long_filename.c_str(), std::ios::app);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章