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
	}
	else
	{
		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;
	logfile.close();

	return 0;
}

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

Log logger;
logger.Write(to_write);

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.

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

commented: Thanks for the help. +3

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.