#### Archived

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

# best way to log error / successes ?

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

## Recommended Posts

high, im working on a side scroller and am wondering, now since i am attempting a bigger project, what would be the proper way to handle errors? i mean, as in, after i load in an image, i want to log that the image has been loaded successfully, or if there is a problem it will log that there was errors. i know OF fprintf and the other print one that prints to console (these are C functions right?) , but is this the best way? also, for some reason fprintf doesnt output any files for me. i thought it would write the text to a file, but does it just display it in the console. i only learned how to do this from an SDL book i have, so im not too clear on how it works. it looks something like fprintf(stdout,"SDL initialized properly!\n"); also it writes to stderr if there is errors. but i dont see any stdout or stderr file in my folder. what would be the best way to write logs to a file? the only file i/o i work with is using fstreams, should i just use fstreams to log all this stuff? keep in mind im using C++. (.net compiler if it matters). also, about errors in general, what is the best way to go about handling them? do you make your functions boolean, and if theres an error, you return false? so it would look like: if(Init_Images()) { print to file "loaded succesfully" } else { print to file "loading failed. bailing out" return -1; } should i do all my functions like this? (well the ones that are appropriate for this of course like image loading functions and such) or, should i just do all error handling inside the function itself? thanks for any help in advance Donkey Punch Productions.net [edited by - graveyard filla on April 15, 2004 1:38:01 AM]

##### Share on other sites
stdout and stderr are system defined as the console. If you want to send something to a file you will need to open a file and then pass the filehandle to fprintf.

FILE *log;

fopen(log,"log.txt","wt");
fprintf(log,"My error\n");
fclose(log);

##### Share on other sites
so should i jsut use fstreams then? because i know how to use fstreams, plus isnt it " a c++ thing" ??

or does the FILE stuff have an advantage? is it better for this situration? should i just learn it or stick to what i know? fstreams have been kind to me so far

##### Share on other sites
I am working on a tutorial based on a better tutorial.
You might want to take a look at the logfile stuff.

Read the disclaimer ... however the logfile part is pretty much the same as in a GOOD tutorial.

Breaktris Tutorial

##### Share on other sites
thanks for the link, but id prefer to not follow any tutorials. i like to learn things as i code . so can anyone confirm if i should just use fstreams to log everything?

Donkey Punch Productions.net

##### Share on other sites
you want to learn while coding? then why do you don''t make the choise by yourself and find out and learn while coding if its a good choise, or not?

If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net

##### Share on other sites
yeah well, the thing is, if i were to code "blindly" and write a whole Logging class, and then it turns out this is compeltely wrong, well that would suck. programming is way to complicated and complex for me to take COMPLETE shots in the dark. i mean, the least i could do is ask for a simple "yes, that should be fine.", or "no, dont do that." i mean, its not like im asking for help with logic. taking shots in the dark with logic is fine, but with syntax? thats just dumb, you dont really get much out of learning what to NOT do with syntax (what do you gain? memorization? i can gain that from this post...)... of course, learning what to NOT do with logic is completely different. making mistakes with logic will teach you the ins and outs of how a program should flow (at least it has with me).

Donkey Punch Productions.net

##### Share on other sites
For my projects, I have a vector of strings. On program destruction, I write the strings to a log file. I also have an option to write "on the fly" in case I need to log events but get an access violation or a system hang or the like (something that stops normal program destruction).

Stay Clausal,

Red Sodium

##### Share on other sites
I use a singleton Log class that I use to log everything. I call it like this: Log::Get().Write(DEBUG, "debug message"). It allso has members like: AllLogsToStdOut(), ErrorsToStdOut(), and so on...

##### Share on other sites
fstreams are a good choice when logging errors, so if you don''t have code already using FILE pointers, go with C++ fstreams.

The big problem with writing a log to file is speed. If you write to the log every time you load an image, your game will load noticable slower. There are three popluar ways to get around this problem

1) create a string object, and write info into that. When the string reaches a certain size, write the string to a file, clear the string in memory, and start the process over. The only problem is there will be a noticable hickup in the game every time you write your string to file.

2) Only write errors to file. This method is the most stright foreward, but sometimes you need more information that just what went wrong.

3) Use C++ precompiler directives to designate certain parts of your code as DEBUG.

Ex.

// Put this line somewhere in main.h (or any other header that is linked with the whole project)#define _DEBUG_             // comment this line before release------------------------------------------------// everywhere you want to write to the log#ifdef _DEBUG_log << Message;#endif