In writing a logging program for a SDK Engine, I have come across some problems. The logging function works fine when called with test programs, but when I run it in a full test of the SDK Engine, no file is ever created. I added some testing lines to make sure the function is getting called, and it is. The directory was properly created the first time I ran the program, but the file never is. Strange thing is, the file failbit flag is never set, and the is_open() function returns true, but the file does not exist.
Here is the code of the Write function, with the diagnostic lines added to write to standard output:

int Log::Write(std::string to_write) {
/*test*/ printf("log.write called");
	//function to write message to a logfile
	if(stat(strPathname.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode))
		//do nothing; directory is present - continue with log writing
		mkdir(strPathname.c_str(), NULL);
	std::ofstream logfile;
	logfile.open("log.txt", std::fstream::out | std::fstream::app); //append to end of file
/*test*/ if(logfile.fail()){printf("\nlogfile fail\n");}else{}
/*test*/ if(!logfile.is_open()){printf("\nfile is not open\n");}else{}

	logfile << getTime() << " " << to_write << std::endl;

	return 0;

In the application, the class is initialized and the functions are called like this:

Log logger;

Does anyone have any idea why the file is never created? ofstream.open() should create the non-existent file. The file is created in /var/HHPVideoServer (running on Ubuntu Linux), and the application is run with root permissions to allow it to create the directory if needed and use the files.

Edited by iamthesgt: added OS clarification

5 Years
Discussion Span
Last Post by iamthesgt

You forgot to chdir(strPathname.c_str()) The logfile is created, but in the wrong place.

Votes + Comments
Thanks for the help.

Thanks. I had actually just figured this out, but thanks for the answer. I was stuck for awhile.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.