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

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
May 2, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

.

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.