Utility Class for logging errors to a file

This is the code for CLogFile, a utility class for logging errors to a disk file. The constructor of the file takes the log file name as the argument. The main function is LOGERROR(TCHAR* formatString, ...), which takes variable number of arguments. So this function can be called with format specifications.

In addition this class has overloaded operator << for most of the common data types. There is also a utility function PrintCurTime, which puts the current date and time to the log file. In the end the destructor of the class will close the file and do the cleanups. The examples shown illustrates the use of this class.


CLogFile lf ("C:\\Test.log");
lf.LOGERROR(_T("The string is %s, the integer is %d and the double is %s"), szStr, nInt, dDouble);
lf << "Error no : " << e.Error << " and the description is " << e.ErrorMessage();
lf.PrintCurTime(); 

 

Download source - 2 KB



Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Today's "average" business in general is ever more reliant on technology and the Internet. Mobility is the most often cited business trend that has transformed the way many of us work and communicate. From an IT security perspective, this means that protection methods and tools from even a few years ago are rapidly becoming "unfit for purpose." This guide provides crucial facts to assist you in building a robust business case, meeting the demands of your business, and protecting against threats now and in the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds