首页 > 解决方案 > 在 C++ 中打开文件时出现分段错误

问题描述

这是我用 C++ 打印日志的代码。编译后,当我尝试执行时,它会Segmentation faultinitLogger()返回. 经过一番尝试,我仍然找不到解决方案。由于我对文件流的操作不是很清楚,所以想请教一下。非常感谢!

记录器.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__)

记录器.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();
    }
}

测试.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;
}

标签: c++

解决方案


推荐阅读