I am trying to get my client which works with Linux and Windows to work with Mac. I have a log class so I can see what is going on and catch errors but my logfile isn’t even outputting. The logfile is declared globally so it should at least output the logfile header regardless. I am using the terminal version C++ with Xcode.
Here is my log code:
log.h
#ifndef LOG_H
#define LOG_H
#include <fstream>
using namespace std;
class Log
{
public:
// Constructor / Destructor
Log();
~Log();
// Class functions
void writeNewline();
void writeError(char * text,...);
void writeSuccess(char * text,...);
private:
ofstream logfile;
};
#endif
log.cpp
#include <ctime>
#include <stdarg.h>
#include "log.h"
const int BUFFER_SIZE = 1024;
using namespace std;
Log::Log()
{
// Create a log file for output
logfile.open ("lanternlog.txt", ios::out);
// Grab the current system time
time_t t = time(0);
struct tm * now = localtime( & t );
// TODO: Format the time correctly
// Insert the time and date at the top
logfile << "<---> Logfile Initialized on " << now->tm_mon + 1 << "-" <<
now->tm_mday << "-" << now->tm_year + 1900 << " at " << now->tm_hour <<
":" << now->tm_min << ":" << now->tm_sec << endl;
}
// Destructor
Log::~Log()
{
// Close the logfile
logfile.close();
}
void Log::writeError(char * text,...)
{
// Grab the variables and insert them
va_list ap;
va_start(ap, text);
char buff[BUFFER_SIZE];
vsnprintf(buff, sizeof(buff), text, ap);
// Output to the log
logfile << "<-!-> " << buff << endl;
}
void Log::writeSuccess(char * text,...)
{
// Grab the variables and insert them
va_list ap;
va_start(ap, text);
char buff[BUFFER_SIZE];
vsnprintf(buff, sizeof(buff), text, ap);
// Output to the log
logfile << "<---> " << buff << endl;
}
void Log::writeNewline()
{
// Create a new line in the logfile
logfile << endl;
}
When the application shuts down, and I have dropped a breakpoint, the logfile should have already output something. There is also a warning with all of my log commands. For instance:
errorLog.writeSuccess("Fatal Error: Unable to initialize Lantern!");
yields: Conversion from string literal to ‘char *’ is deprecated
Still, the main initialization of the logfile does not use this method and should output the file.
First question was solved! Check below for other error:
Edit: It seems I have gotten a tiny bit further. The logfile is created, but is created in the harddrive/users/ folder. How would I have it simply output to the Xcode project folder like with Visual Studio.
I think you can deal with the
by changing the methods from ones which take
char *parameters:to ones which take
const char *parametersThe compiler should be worried about passing string literals as parameters to functions which could try to change them.
Is the logfile being created?
I’d try removing everything (using #if 0 … #endif) from the constructor except for a dumb
to reduce the number of ways it can break.