- Online Community Forums » Programming Questions » Error mesages

This thread is locked; no one can reply to it. rss feed Print
Error mesages
Member #12,583
February 2011

When is it appropriate to print an error message should something fail? For example:

1if(!al_init()) 2 { 3 fprintf(stderr, "failed to initialize allegro!"); 4 return -1; 5 }

I have been doing it for almost everything that needs to be initialized, installed, loaded, or created.

Trent Gamblin
Member #261
April 2000

Everywhere something critical could fail. If it's recoverable or something that happens often, you might not want to enable debug printing there unless a DEBUG flag is set or something. The more error messages you have though the less time it takes to pinpoint bugs.

Tobias Dammers
Member #2,604
August 2002

Rule of thumb: If your code can recover completely from the error, remain silent. Otherwise, fail early and loudly.

If it's not an error, but you would like to check the state of something somewhere, you should make the logging configurable, especially if it happens in a time-critical section of your code. Either set a global logging level (yes, this is one of the cases where globals are excusable, although wrapping it in a class is a good idea), or set up some defines to set the reporting level at compile time. Both work; the global variable approach allows you to change the reporting level without recompiling, but the define approach will remove even the tiny bit of code that is needed to skip the logging.

Me make music: Triofobie
"We need Tobias and his awesome trombone, too." - Johan Halmén

Go to: