Boring But Important: logfiles

A forum to store posts deemed exceptionally wise and useful
Post Reply
Nick_Japan
Posts: 98
Joined: Mon Dec 13, 2004 11:47 am
Location: Japan

Boring But Important: logfiles

Post by Nick_Japan »

It looks like I'm going for the award for the Most Unexciting Irrlicht Related Tutorial Ever award here. If you're not using a logfile for your debugging output, then I'd really recommend you start. If nothing else, the massive advantage they have over simple printf() calls is that you can output as much as you like without it getting lost up the top of the output window.

I've written a very simple error log system, simply a class that writes things to a file for you. As you can see in the header, the class is already declared when the file is included, so you don't need to initialise it or anything. To use it, do this:

Code: Select all

    // in your code somewhere
    if(somethingGoesWrong == true)
    {
       logfile.writeEntry("something went wrong - my ostrich is %d metres tall", ostrich.getLength());
    }
Download here

And yes, it does the cool formatted text thing like printf(). Go Me.

Only one thing, in order to get at it every source file needs to #include "CErrorLog.h" - I use a "Global.h" which has Irrlicht includes and everything, but if you don't then make sure you include this everywhere.

Here's the header, just so you can see what's going on:

Code: Select all

#ifndef __CERRORLOG_H
#define __CERRORLOG_H

#include <stdarg.h>

class CErrorLog
{
public:
    CErrorLog();
    CErrorLog(char *logfilename);
    ~CErrorLog();
    void writeEntry(char *entry, ...);
    void close();
private:
    FILE *logfile;
};

//static CErrorLog *logfile;

static CErrorLog logfile;

#endif
Guest

Post by Guest »

I found a fairly elegant solution to this same problem. I implemented an IEventReceiver that dumps EET_LOG_TEXT_EVENT messages to the log file.

If you want printf like logging, you can modify the ILogger interface to support that. So now for logging I can just use

Code: Select all

Device->getLogger()->log(ELL_INFORMATION, "failed to read file '%s', filename);
and I get a nice message to my output file.

Travis
DaChief
Posts: 45
Joined: Tue Nov 01, 2005 9:02 am
Location: Switzerland

Post by DaChief »

I wrote a little Tutorial which shows, how you can use the IrrLicht Logger also for LogFiles. Look at this:
http://irrlicht.sourceforge.net/phpBB2/ ... php?t=9955

I use this solution for the LogFile-Problem, because the IrrLicht output is also logged such as the createDevice for Device-Init or the "Loaded Texture", "Loaded Mesh" etc

But your example is interesting, because you can add some Variables to the Log-Text... maybe you should combine these solutions, so your Class is using the IrrLicht logger, and the ILogger logs also to Files just like "Guest" said before...


nice logging....

DaChief
IrrLicht v0.14
Audiere Sounds-API v1.9.3
Current Project: KoulesXD
Post Reply