samedi 11 avril 2020

ofstream alwasy fails to open file successfully

I have written c++ code which will act as a logger for my other libraries. I am creating a singleton class called Logger and using a static get_instance() function to access it's intance like this -

Logger&
Logger::get_instance()
{
    static Logger instance;
    if (instance.m_curr_status != LogStatus::SUCCESS)
        instance.init();

    return instance;
}

void
Logger::init()
{
    std::ifstream ifs(m_logfilename, std::ios::binary);
    if (ifs.is_open()) 
    {
        ifs.close();
    }

    m_file.open(m_logfilename, std::ios_base::out | std::ios_base::app);
    if (!m_file.good()) 
    {
        std::cerr << "Unable to initialize the Logger!" << std::endl;
        m_curr_status = LogStatus::FAIL;
    }

}

The issue is the m_file.good() function always returns false. All the member variables are static. The file gets created at the desired location.

static std::ofstream                   m_file;

Aucun commentaire:

Enregistrer un commentaire