日誌
日誌一般用於記錄系統運行異常信息、狀態信息、性能指標
級別
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);
}