Be Friendly
Search This Site for Tutorials About...

How To Create a Log File (For Reporting Program Errors using C or C++)

Just for fun: How to Make French Toast

It is desired to have some type of an output system when developing a complex application. In Windows-based applications the minimal requirement to output a message is to display a message box with the MessageBox(); command. However, what I want to illustrate in this tutorial is a simple output system which logs messages to a file.

Many times you will need to log some important information to a file. For example, it is convenient to output the results of initialization commands in your application into a specific log file. That way it will be easy to see at which point the program quit (if it did) when an error occurs, without mindlessly trying to figure that out by yourself. So what we're going to do here is create a function and define it as

            Log(char *message);

As you guessed, message is the parameter that will be outputted into a file stream. The system log filename is defined by LOGFILE. Every time Log(char *message); is called, the string "message" is appended to the filename defined as LOGFILE. We will call Log(...); a lot in the OpenGL base code presented in OpenGL tutorials. Additionally to Log(...); I wrote LogErr(...); Whereas Log(...); merely writes information to a file for logging purposes, when you can LogErr(...); the message is written, however the program additionally quits. This functionality is useful when you want to log a message in case of an error to avoid further execution of the code.

There's really not much to explain. The code below should be trivial to understand. First, take a look at the header log.h:

//log.h - the header file which defines Log(); and LogErr();

#define LOGFILE	"gl.log"     // all Log(); messages will be appended to this file
extern bool LogCreated;      // keeps track whether the log file is created or not
void Log (char *message);    // logs a message to LOGFILE
void LogErr (char *message); // logs a message; execution is interrupted

And here is the log.cpp:

// log.cpp;

#include <stdlib.h>
#include <stdio.h>
#include "system.h"        // SysShutdown();
#include "log.h"

bool LogCreated = false;

void Log (char *message)
	FILE *file;

	if (!LogCreated) {
		file = fopen(LOGFILE, "w");
		LogCreated = true;
		file = fopen(LOGFILE, "a");

	if (file == NULL) {
		if (LogCreated)
			LogCreated = false;
		fputs(message, file);

	if (file)

void LogErr (char *message)

Really, simple as that. If you're currently reading OpenGL or DirectInput tutorials or any other advanced tutorials on this site, you will see how Log(char *message) and LogErr(char *message) are used. Another thing to note is that in log.cpp I include the mysterious "system.h". You see, normally in my programs the main "program-shutdown" function is defined in "sysetm.h" and since LogErr() needs some way of knowing where that function is located to properly shutdown an application after logging a message to a log file, that definition is required. Now, if you're going to use this code in your own program, be sure to delete that line or connect it with your own "system shutdown" function by including your own header where such a function is located or in the many possible other ways (add an external function definition in the log.h header file?).

The source code contains log.h and log.cpp, Have fun.

Did this article help you learn something new?

I enjoy writing for the Internet audiences because my thoughts are instantly published to the entire world. My work consists of writing educational articles about making websites to help people learn. If you enjoyed reading this article, or learned something new, then I have succeeded.

If the content on this website somehow helped you to learn something new, please let others know about it by sharing it on your website or on your Facebook page with your friends. In addition you can help by making a donation or bookmarking this site.