• ### Popular Now

• 13
• 16
• 19
• 27
• 9

#### Archived

This topic is now archived and is closed to further replies.

# Best way of logging errors?

This topic is 5695 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

For my 4 Elements contest entry, I was logging my errors to a file using the simple class that I had created: In the header file:

//----------------------------------------//

// logfile.h - header for error logging

//

//----------------------------------------//

//ifndef it so it doesn''t get included more than once

#ifndef _LOGFILE_H_
#define _LOGFILE_H_

//includ the necessary files for the input/output

#include <iostream.h>		//input/output stuff
#include <fstream.h>		//more i/o stuff

//return types for the scripting

const int LOGFILE_OK		= 0;

//the logfile class - creates a file and logs all the information on

//how the program behaves to it

class LOGFILE
{
public:
~LOGFILE();
ofstream outfile;

int CreateLogFile(const char* FileName);
int LogtoFile(char Outputstring[256]);
void CloseLogFile();

};

#endif //for _LOGFILE_H_


In the cpp file:

//----------------------------------------//

// logfile.cpp - main file for error logging

//

//----------------------------------------//

//do the usual...

#include "logfile.h"

//--------------------------------------------------//

// Name: ~LOGFILE()

// Desc: destructor that closes the logfile

//--------------------------------------------------//

LOGFILE::~LOGFILE()
{
//close the file

outfile.close();

}

//--------------------------------------------------//

// Name: CreateLogFile()

// Desc: function that creates a file for logging

//--------------------------------------------------//

int LOGFILE::CreateLogFile(const char* FileName)
{
//create the file and test if it was made ok

outfile.open(FileName);

if(!outfile)
{
}

//return ok

return LOGFILE_OK;
};

//--------------------------------------------------//

// Name: LogtoFile()

// Desc: function that puts a string to the file

//--------------------------------------------------//

int LOGFILE::LogtoFile(char OutputString[256])
{
//output it to the file and flush it so it doesn''t

//get lost if the program messes up

outfile<<OutputString;
outfile.flush();

//return ok

return LOGFILE_OK;
}

//--------------------------------------------------//

// Name: CloseLogFile()

// Desc: closes down the logfile, if it hasn''t been already.

// Note: If this function doesn''t get called, the destructor will handle things

//--------------------------------------------------//

void LOGFILE::CloseLogFile()
{
//close the file

outfile.close();

}

Now to post something in the logfile, I would do something like:

logfile.LogtoFile("D3D - Unable to find a supported backbuffer format \n");

This is all find and dandy, and it works just fine, but it doesn''t seem like a very good way of logging errors. It seems to take quite a bit just to say one little thing to the logfile. Not only that, but my output strings are limited to 256 characters (I could increase that, but it just seems inefficient to me). Is there a better way to log errors to a file? How do you other people do it? Moe''s site

##### Share on other sites
You use a logging class, like you have done. There really isn''t a transparent way to log events in your program. This way is the most flexable, as it allows you to change the format of your log output by only changing a few functions, and it''s portable across multiple projects if you design it right - consistency there.

##### Share on other sites
Ok, it seems to have messed up somehow and everything is green... If you can''t read it, try highlighting it by selecting it.

http 500 x1

Moe''s site

##### Share on other sites
take a look at my CLog class (note that i also have a CLogManager, but u don't really need that). i posted src to it somewhere, i will look and edit this post when i find it. so don't lose hope, i'm here to help!

just gotta post something (it kept timing out, gonna try again now) quickly...

ps. there are at least three advantages in my CLog class over urs: no limit on message-to-be-logged length; it accepts custom number of params; and log will be fine even if ur program blue-screens (therefore outfile.close() will never get called), no data will be lost (cuz it might just log things to the buffer, instead of the actual file on ur hd).
ps2. great, now even this keeps timing out! i hate these forums! >:-( it's impossible to post anything! gives me error 500.

---
shurcool
wwdev

[edited by - shurcool on August 13, 2002 5:59:03 PM]

##### Share on other sites
Thanks for the replies everyone. I guess I will stick with the sort of system that I have, but I will definitly change a few things. Thanks for the feedback so far!

Moe''s site

##### Share on other sites
My error log class:

  // log.hofstream &getErrorLog ();// log.cppofstream &getErrorLog (){  static ofstream theLog ("ErrLog.txt");  return theLog;}

Don''t really see why you''d need more than that.

##### Share on other sites
If you want to make your logfile class a little easier to use in program (especially passing around the logfile object) You can either make everything in it static, or move to a non-class based approach, which can still be just as modular. The downside to this method is you can then only have one logfile, instead of the possibilities of multiples.

The real improvement you can do however is with your LogToFile function. You can make this function similar to C''s printf functions, which will make posting messages easier since you won''t have to build the strings before passing them in. Basically what I''m saying here is that you can add the ability to pass more information than just a static string without having to build the cstring by hand.

Finally my last suggestion is that you get rid of them max array size of 256. You will probably never try to print a line longer than that but it costs you nothing to make it an open size. So just use:
char* outputstring
char outputstring[256]

##### Share on other sites
did u even take a look at my CLog class? i dunno, maybe you just missed it because i edited my post, not made another one. =/

i just wanted to reply to what AP said above me. it''s all good using char*, but that''s just C-style. move on, learn the benefits of C++. it''s not "C, just with classes". my point? use std::string instead of usual-C-null-terminated-char-sequences.

---
shurcool
wwdev

##### Share on other sites
Certain cases, I would love to close and re-append my file for every line of logs to reduce lost cluster... because of the flushing problems don''t mix well with reboot. .... but that slows thing down.

##### Share on other sites
quote:
Original post by DerekSaw
Certain cases, I would love to close and re-append my file for every line of logs to reduce lost cluster... because of the flushing problems don''t mix well with reboot. .... but that slows thing down.

flush does exactly that.

---
shurcool
wwdev