Original Post
I've been away from C++ for a while, but I'm experiencing a problem I can't really explain: The above code is a fairly simple logging class I wrote in a few minutes. The problems I am experiencing revolve entirely around the m_LogFileName member. All I'm trying to do is to set it's value in the constructor. The log file isn't getting created, however, so I poked around a bit with the debugger to determine my problem. At REFERENCE LINE 1, the debugger tells me that m_LogFileName is a "". When it reaches REFERENCE Line 2, it has the same value as the "message" variable passed to the WriteLine method. The instance I'm using is a global variable declared as: Log logger; and used as: catch (exception e) { logger.WriteLog("General Exception", e.what()); } catch (...) { logger.WriteLog("General Exception", ""); } As you can see, this is some pretty simple stuff. Again, though, I've been hanging in C# land for a while, so perhaps I'm taking something for granted. Can anyone see what is wrong with this code? Thanks.
// log.h
#ifndef LOG_H
#define LOG_H
#include<iostream>
#include<fstream>
#include<string>
#include<ctime>
using namespace std;
class Log
{
public:
Log();
void WriteLog(const string& message);
void WriteLog(const string& header, const string& message);
private:
string m_LogFileName;
};
#endif
// log.cpp
#include "log.h"
Log::Log()
{
char date[9];
_strdate_s(date);
for(int i = 0; i < 9; i++)
if(date == '/')
date = '-';
m_LogFileName = "Log.txt";
// REFERENCE LINE 1
// m_LogFileName.append(date);
// m_LogFileName.append(".txt");
}
void Log::WriteLog(const string& message)
{
WriteLog("", message);
}
void Log::WriteLog(const string& header, const string& message)
{
char date[9];
char time[9];
_strdate_s(date);
_strtime_s(time);
fstream logFile;
logFile.open(m_LogFileName.c_str());
// REFERENCE LINE 2
if(logFile.is_open())
{
logFile << "=========================================================================" << endl;
logFile << date << " - " << time << endl;
if(header != "")
logFile << header << endl;
logFile << message << endl;
logFile << "=========================================================================" << endl;
}
logFile.close();