A Text File Logging system for use in VC++ projects

.

Environment: This code was compiled and tested with Visual C++ 6.0

In many applications it is necessary to have a logging system which logs text into a file.The following class provides such a logging system which is very easy to use.

The features include

1. Logging of text directly / using string table ids

2. Tracking of log file size (displays warning messages without affecting further logging)

3. automatically provides date and time with the log message

4. ablility to clear the log file etc..

How does the output text file look like ?

format is Date    Time    Logged text

For Example:

01/17/99 17:02:46 Here goes ur text

How do I integrate it with my existing code or how do I use it?

Just add the two files into ur project and in the appclass or ur project create an object like this

In the header (.h) file


#include "log.h"

class CSomeApp : public CWinApp
{
public:
 CSomeApp();
 CLog m_Log;

 ...
};

In the implementation (.cpp) file

CSomeApp::CSomeApp():m_Log("log file name") // see log
constructor for more details
{
 // TODO: add construction code here
}

and where ever you want to log u can use code like this...


extern CSomeApp theApp;
theApp.m_Log.LogMessage("this is what is logged");    // see

LogMessage for more details

or you can use these macros


// macro to log a string directly

#ifndef LOGS
 #define LOGS(sz) ((CSomeApp*)AfxGetApp())->m_Log.LogMessage(sz)
#endif

// macro to log  a string using a  string table id
#ifndef LOGN
 #define LOGN(a) ((CSomeApp*)AfxGetApp())->m_Log.LogMessage(a)
#endif

Download source - 3 KB



Comments

  • This code will fail if Program crashes in middle.

    Posted by Legacy on 02/03/1999 12:00am

    Originally posted by: Shashik

    This object will fail to close the log file, and thus all the details that were entered in the log file will be lost, if the application crashes in between. I think it would work if you open file in append mode and log the message, and as it is close the file. Just let me know whether I'm right.
    Thank you.

    Reply
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 …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds