This is my code for printing logs in C++. After compiling, when I tried to execute, it will return Segmentation fault
in initLogger()
. After some trying, I still can't find the solution. Since I don't clearly understand the operation of the file stream, I would like to ask for help. Thank you very much!
Logger.h:
#ifndef __logger__
#define __logger__
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <stdint.h>
///
/// \brief log type
///
typedef enum log_rank {
INFO,
WARNING,
ERROR,
FATAL
}log_rank_t;
///
/// \brief 初始化日志文件
/// \param info_log_filename info file
/// \param warn_log_filename warn file
/// \param error_log_filename error file
void initLogger(const std::string& info_log_filename,
const std::string& warn_log_filename,
const std::string& erro_log_filename){};
class Logger {
public:
Logger(log_rank_t log_rank) : m_log_rank(log_rank) {};
~Logger(){};
static std::ostream& start(log_rank_t log_rank,
const int32_t line,
const std::string& function){};
friend void initLogger(const std::string& info_log_filename,
const std::string& warn_log_filename,
const std::string& erro_log_filename);
private:
static std::ostream& getStream(log_rank_t log_rank){};
static std::ofstream m_info_log_file; ///< output stream of info file
static std::ofstream m_warn_log_file; ///< op stream of warn file
static std::ofstream m_error_log_file; ///< op stream of error file
log_rank_t m_log_rank; ///< log rank
};
///
/// \brief write different files depend on the log rank
///
#define LOG(log_rank) \
Logger(log_rank).start(log_rank, __LINE__,__FUNCTION__)
Logger.cpp
#include "Logger.h"
#include <cstdlib>
#include <ctime>
std::ofstream Logger::m_error_log_file;
std::ofstream Logger::m_info_log_file;
std::ofstream Logger::m_warn_log_file;
void initLogger(const std::string&info_log_filename,
const std::string&warn_log_filename,
const std::string&error_log_filename){
Logger::m_info_log_file.open(info_log_filename.c_str());
Logger::m_warn_log_file.open(warn_log_filename.c_str());
Logger::m_error_log_file.open(error_log_filename.c_str());
}
std::ostream& Logger::getStream(log_rank_t log_rank){
return (INFO == log_rank) ?
(m_info_log_file.is_open() ?m_info_log_file : std::cout) :
(WARNING == log_rank ?
(m_warn_log_file.is_open()? m_warn_log_file : std::cerr) :
(m_error_log_file.is_open()? m_error_log_file : std::cerr));
}
std::ostream& Logger::start(log_rank_t log_rank,
const int32_t line,
const std::string&function) {
time_t tm;
time(&tm);
char time_string[128];
ctime_r(&tm, time_string);
return getStream(log_rank) << time_string
<< "function (" << function << ")"
<< "line " << line
<<std::flush;
}
Logger::~Logger(){
getStream(m_log_rank) << std::endl << std::flush;
if (FATAL == m_log_rank) {
m_info_log_file.close();
m_warn_log_file.close();
m_error_log_file.close();
abort();
}
}
test.cpp
#include "Logger.h"
#include <iostream>
using namespace std;
int main(int argc, char** argv){
initLogger("/log/info.txt","/log/warn.txt","/log/error.txt");
cout<<"Initialized"<<endl;
return 0;
}