[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

fflush not writing to file

Posted on 2009-05-19
4
Medium Priority
?
219 Views
Last Modified: 2012-05-07
Hello all,
I have a C++ class that I am using to act as a real time logger for a generic server application.  When the WriteLogEntry function is called it writes the log entry to the file stream.  After writing it out, I call fflush to move it into the file on the disk, however the data is not being written to the disk until fclose is called.  

My understanding of fflush was that it flushed the stream to the end device.  Is this a correct understanding?  If so what is going wrong here?  If not, how to I get the data written to disk without having to call fclose?  I need to be able to use the log file as a diagnostic tool without shutting down the server to get the data to the log.

Thanks much.
**************************
ServerLog.h
**************************
#ifndef SERVER_LOG_H
#define SERVER_LOG_H
 
#include <stdio.h>
#include <boost/algorithm/string.hpp>
 
using namespace std;
using namespace boost;
 
class ServerLog
{
public:
	static ServerLog* Instance();
	void WriteLogEntry(string entryData);
	~ServerLog(void);
 
protected:
	ServerLog(void);
 
private:
	static ServerLog* instance;
	static FILE* serverLogFile;
};
 
#endif /* SERVER_LOG_H */
 
**************************
ServerLog.cpp
**************************
#include "ServerLog.h"
 
FILE* ServerLog::serverLogFile = 0;
ServerLog* ServerLog::instance = 0;
 
ServerLog* ServerLog::Instance()
{
	if( instance == 0 )
	{
		instance = new ServerLog();
	}
	return instance;
}
 
ServerLog::ServerLog(void)
{
	serverLogFile = fopen("lcs_system.log", "a");
	int result = fflush(serverLogFile);
}
 
ServerLog::~ServerLog(void)
{
	fclose(serverLogFile);
}
 
void ServerLog::WriteLogEntry(string entryData)
{
	// todo: check write - throw exception if not correct
	int bytesWritten = fwrite(entryData.c_str(), sizeof(char), entryData.length(), serverLogFile);
}

Open in new window

0
Comment
Question by:edc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 2000 total points
ID: 24426916
You have to call fflush after every write, not just when you open the file. Everytime you call it the write buffer will be flushed. In your code example you only flush when you open the file, what about in WriteLogEnrty()?
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24426925
0
 
LVL 1

Author Comment

by:edc
ID: 24426937
I don't freakin' believe I did that.  Thanks evilrix for pointing that out.  I'm going to go feel stupid for a few days :o).
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24426949
>> I don't freakin' believe I did that.
Sometime it just needs anotehr pair of eye -- I do things like that all the time :)

>> Thanks evilrix for pointing that out.
Very welcome
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question