# Ofstream outputting crazy results

This topic is 4321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey all, Having a few issues with ofstream; I read up on previous posts (including the singleton one here), but I don't think that's the problem (although I have been known to be wrong a lot [grin]). I've got a logger system implemented. The code follows:
#define LOG(text)		g_Log.output(text);

#define FN(text)		LOG(text);

#define LOG_EXIT		g_Log.output("Exiting log..."); g_Log.destroy();

class avLogger {
public:
const char*			g_filename;
ofstream			fout;

avLogger(GLvoid) {}
avLogger(const char* filename) {g_filename = filename; setup();}
~avLogger(GLvoid) {}

GLvoid setup(GLvoid);
GLvoid destroy(GLvoid);

GLvoid output(const char* text);
};

static avLogger g_Log("log.txt");


setup opens the ofstream and checks for failure, destroy closes the ofstream, and output is composed of fout << text. The macros LOG(text) and FN(text) are used to access the statically declared g_Log, and are the only methods used to access the logger throughout the program. Here's the problem. The output in the logfile misses the first few FN and LOG calls in the program, then outputs some FN calls, then outputs the next LOG call except with the first 7 or 8 characters chopped off, then seems okay from there. In addition, when I exit the program, I get a "quit unexpectedly" error, which I'm almost positive is related to this (doesn't do it when I chop out all the LOG/FN calls). I appreciate any help, and please be as harsh as you want...if I'm wrong about the previous post not applying, tear me a new one :P. Thanks a lot, Aviosity

##### Share on other sites
I'm not a big fan of singletons, but the instantiation-outside-of-a-constructor idea might be what you need. My "manager" classes each have a bool Init() method, and I call them in a specified order inside the general application.Init() method.

Aside from that, it's hard to know what the problem is without your definition code. Might I suggest a ofstream::flush() call after each write?

##### Share on other sites
The bodies of the setup, destroy and output functions might be helpful. Also, is there some reason you need to call destroy manually instead of in the destructor? And why do you need FN when it does the same thing as LOG? Not that these SHOULD be the problem but simplifying the design might help you track down the source of the error.