Skip to main content
GameDev.net gamedev.net
🔒 Locked

How do *you* log errors?

Started by Fresh Jan 10, 2001 at 2:58 PM 7 replies 650+ views
Original Post
Fresh
Fresh
Until recently, I was using a simple error handling class which fopened a file when my application started, fprinted to it during runtime, and fclosed it upon exit. Only inexplicably, sometimes upon exit the file would appear to be empty. The rest of the program seems to run fine. What could cause that? Another problem with doing things that way, was if something stupid happened during the program, the whole thing would crash, and windoze would end the program and the destructor of the error handler wouldn''t be called, ie. the file would not be fclosed. So what I am doing now is clearing the file upon startup, and then append opening and closing the file before and after every call to the error handler. This seems to have solved the problem I stated above. However - just how stupid is? I''m not in the habit of calling the logger/errorhandler every frame, just on things like bitmap loading and directdraw errors etc., so is this that stupid? It at least always preserves the contents of the file. How do you people log errors? r. "The mere thought hadn''t even begun to speculate about the slightest possibility of traversing the eternal wasteland that is my mind..."
TwoFlower
TwoFlower
Yep, that''s fine. Stick it on an option switch though so you can turn it off. And you can also have different levels of output so you can decide how much stuff to output.
MicahJon
MicahJon
It's not a that bad a solution to the problem, but there is a better one.

    
FILE* MyFile = fopen("MyFile","wt");
//stuff

fprintf(MyFile,"I'm here!\n");
fflush(MyFile);

//Stuff that causes program to crash



When you call fprintf (or fwrite) it doesn't get written to the file, it actually gets stored in a buffer in RAM until the buffer gets full. If your program crashes before the buffer is full, you lose all of the data that was in the buffer. The fflush forces the buffer to be written out whether it's full or not, and ensures that your file always has up-to-date contents, without having to open and re-open the file.

Micah

Edited by - MicahJon on January 10, 2001 5:42:36 PM
Moe
Moe
I will ocasionally throw in a MessageBox() windows function so that an OK message boxs pops up at a certain time in my program. It halts the rest of the program. It takes ~1 line of code to do so it is quick.

"Never pet a burning dog." - Warcraft II
Shannon Barber
Shannon Barber
MsgBox''s can suck because they''re modal...

It''s a bad idea to only open & close once, its a better idea to flush, it''s the best idea to open & close after each write.

If you''re debugging, it implies some instability problems (hopefully no BSODs ), and if your program crashes hard it can leave that file hanging open. Now you have to reboot to use the file again.(logging out might do the trick on NT).
If you open/write/close, it can only get stuck open if the write itself fails.

I suppose a robust program would do some clean up and close all the open files in the event of an exception...

Magmai Kai Holmlor
- The disgruntled & disillusioned
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
MicahJon
MicahJon
Magmai,

I've never had a file stuck open under Win32, no matter how hard my app crashed (well, I must say I've never checked after my app takes down Windows itself ). I just assumed it couldn't happen anymore under Windows. Of course, that makes the wrongful assumption that everyone writes code for Windows.

Micah

Edited by - MicahJon on January 11, 2001 2:08:31 PM
Dev6969
Dev6969
Yo Fresh, I use an appended file that gets killed everytime the app is started... basically after every major device initialization i output to the file to say that the device was initialized. But the biggest use i''ve found is to output every step in loading scripts, as i''m still in the process of developing my scripting engine. In addition to all that, durring runtime my app outputs to the file a PolyCount, ObjectCount, and FPS... So i can see just how much my engine will be able to handle.

i have a sub like this in my vb app:

Public ToDebugFile (Output as string)
If Debugging = True Then
Open "C:\Windows\Desktop\Debug.txt" for append as #1
Print #1, Output
Close #1
End if
End Sub

and i won''t bother with a C++ equivalent as i''m sure C++ ppl can understand that.

-Dev
Run_The_Shadows
Run_The_Shadows
i dunno if i''ve got the tags right, but......

ofstream logfile("log.txt");

inline void LogMsg(char Msg)
{
logfile << msg << endl;
}





-Ryan "Run_The_Shadows"
-Run_The_Shadows@excite.com
"Doubt Everything. Find your own light." -Dying words of Gautama
NickB
NickB
Hi,

Personally I tend to have this code in a seperate file (logging.cpp) in my project (and just copy it into everything I write).

  
#include <stdarg.h>
#include <stdio.h>

void applog(char *szformat, ...)
{
va_list list;
char szout[256];
FILE *fp;

if(logging) { // only write the log if required

va_start(list, szformat);
vsprintf(szout, szformat, list);
// if there isn''t a new line character at the end, append one to the output string

if(szout[strlen(szout)-1]!=''\n'') strcat(szout,"\n");
fp=fopen("log.txt","at+");
fwrite(szout, strlen(szout), 1, fp);
fclose(fp);
}
}


It''s pretty easy to use as well, the text formating is exactly like ''printf'' eg. applog("Variable1 = %d", Variable1);

NickB

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.