Code Snippets
What's That?
I am using it actively
I planning to use/learn it
I don't plan to lean/use it
5th option
Home :: Programming :: Code Snippets :: Cpp :: Using a Logfile

[ Using a Logfile ]

Using a logfile is really easy especially if you use the SLogFile class form the code section. Download it and copy the SLogfile.h header file in your project's directory and add the compiled library to your project or the CPP-file if your are not using VC++.

In the project in which you want use a logfile, declare an instance of SLogFile. Don't forget to include the header file!

//Don't forget this;)
#include "SLogFile.h"
//Global logfile instance
SLogFile logfile;

The next thing to do is to initialize the logfile. Do this in the part of your program where other things of your app are setup. As example I take the function InitApp():

void InitApp()

Now your logfile is inited and ready to use. The first parameter of the member function open(..) specifies the file which the text should be written to. The second parameter tells SLogFile whether the specified logfile should be emptied or not if it allready exists.

Now place calls to SLogFile in your code where you want to know something about the status of your application. A good place is the App's setup function or critical parts of your code (like the drawing function). How to do that:

void DrawApp()
   logfile << "Beginning draw function.";
   logfile << "Draw function finished";

The operator << of SLogFile writes the specified text to the logfile. You could also use SLogFile::writeLine(char* buffer). If you want to write values of variables to the logfile, use something like this:

void DrawApp()
   int xPos, yPos;
   //Drawing and other Updates
   //Writing the values of xPos and yPos
   // to the logfile
   char buffer[256];
   sprintf(buffer,"xPos: %d yPos: %d", xPos,yPos);

That's all. If you have problems, mail me. I'll help you out. Using a logfile is never a bad idea. It can help you finding bugs in your programs. Happy Coding!

 Last edited 2002-12-07 18:28:37 by André Stein - printable version
» copyright by andré stein
» using stCM v1.0
» steinsoft.net revision 5.0