glob2-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [glob2-devel] Error handling and reporting


From: simon schuler
Subject: Re: [glob2-devel] Error handling and reporting
Date: Tue, 04 Oct 2005 15:28:21 +0200
User-agent: Mail/News 1.4 (X11/20050914)

Andrew Sayers wrote:
Assertions and standard error messages are extremely useful as far as
they go, but have a lot of limitations:

* Assertions don't provide any information about why a test failed.
  For example, I would like "assert(myValue <= maxValue)" to tell me the
  value of myValue when it fails.
if you want this info, consider a macro like this:
#define assert_message(a, ...) if (a) {fprintf (stderr, __VA_ARGS__); \
                                                               abort(); }

or if you like a function: (a bit less ugly)

void assert_message (bool a, char *formatstring, ...)
{
   va_list p;
   va_start (p, formatstring);
   if (!a)
       vprintf (formatstring, p);
   va_end (p);
}
and you can customize your error messages.

* They assume players look at the standard error.  Since Glob2 is still
  in the alpha stage, assertions etc. are (rightly) left in the code
  people play.  Unless the program is run from the command-line, errors
  and assertions are simply ignored - and when the game fails, players
  are treated to the very jarring experience of the game just vanishing.
with linux you can easily redirect stderr to a logfile:

   freopen ("logfile", "w", stderr);

But I don't know if this is portable...
* configurable on a per-class basis.  Each class should be able to
  configure debugging levels independently.  Classes should be able to
  have several debugging "profiles" - for example, one profile for
  impossible errors, another for ordinary complaints to the user.

I have to admit that I have no idea about such logging/error reporting mechanisms and I don't know if it s really useful. For me assert, fprintf... has always been enough. The only implementation I can imagine is having a base debug class which implements
everything you suggest. Then you can just derive every class from this one.

a quick example:
class debug_class
{
   protected:
      virtual bool is_verbose () {return true};

      virtual void log_message (char *formatstring, ...);
      virtual void error_message (char *formatstring, ...);
virtual void assert_message (bool condition, char *formatstring, ...);
      virtual void Assert (bool condition);
};

and log_message for example like this:

debug_class::void log_message (char *formatstring, ...)
{
   if ( !is_verbose () )
      return;
   va_list p;
   va_start (p, formatstring);
   vfprintf (logfile, formatstring, p);
   va_end (p);
}

every derived class can set its own log level by setting:

virtual bool derived_class::is_verbose () {return false};
or
virtual bool derived_class::is_verbose () {return true};
or just take the default value from the debug_class

I think something like this this would be easy to implement, and could do most of what you want. You could also easily adapt all the logging functions and loglevels it for
every class. but I doubt that this is really useful


simon




reply via email to

[Prev in Thread] Current Thread [Next in Thread]