Tree branch
Another tree branch
OpenGL Tutorials
Spider web
Facebook Twitter Google Plus
How to create a log file (for reporting program errors) in C or C++

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. We are simply opening a file for writing on the given path in the filesystem. The "w" flag indicates that if file doesn't exist, it will be written. If it exists, it will be overwritten from scratch.

First, let's 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; } else file = fopen(LOGFILE, "a"); if (file == NULL) { if (LogCreated) LogCreated = false; return; } else { fputs(message, file); fclose(file); } if (file) fclose(file); } void LogErr (char *message) { Log(message); Log("\n"); SysShutdown(); }

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.

© 2014 OpenGL Tutorials.

About | Privacy | Contact

Built with Tornado PHP FrameworkTornado PHP Framework with Wooden PHP Template template | Web design by Web Design by Greg.