Tuesday, February 4, 2014

Classes for Logging messages to a file and Truncate that file at routine intervals

Code at: 

I often used to think that what a simple logging interface would look like. So I went ahead and wrote couple of classes that would do just that. 

The classes are simple. First class LogService has two constructors that set the logFileName. This class also has a function logMessageToFile that writes the provided string to the logFile.

void logMessageToFile(QString);

Now the second class LogTruncateService has similar constructors. But this class implements a QThread. So we have run function as well.

LogTruncateService(QString );
void run();

The run function contains an infinite while loop which keeps checking the time stamps stored in logFile and resets the file after certain no of days are past. Here I have set that time to 10 days.

The limitation is that we need to provide log messages in given format. Else this parser fails. But that is feasible when we are only logging critical activities, errors etc. And that's what is most suitable area for use of this service.  

No comments:

Post a Comment