Game Development Reference
In-Depth Information
querying the m_tags map, and sending it to those places. LogMgr::SetDisplay-
Flags() finds the tag in the m_tags map and updates the display flags. If there is
no tag in the map, it creates one. Logger::SetDisplayFlags() is just a wrapper
forthisfunction.
LogMgr::AddErrorMessenger() is called whenever a new ErrorMessenger
object is created. It simply adds the ErrorMessenger object to the m_error
Messengers list. The LogMgr::Error() function is called from the Error
Messenger::Show() function to display the appropriate dialog box. The return
value of this function is used by ErrorMessenger::Show() to update the
m_enabled flag, which determines whether or not the dialog is displayed next time.
The final three functions are private helpers.
This logging system is pretty neat, but it
s not very
easy to use. The code listing I showed you previously for using the ErrorMessen-
ger class is a great example. Something like that really should be a single line of
code. Second, and perhaps more importantly, there
'
s missing two key things. First, it
'
'
s no easy way to get rid of these
errors and logs in release mode. Fortunately, there
s a simple solution that will solve
both of these issues. All you need to do is create a few macros to wrap the public
interface of the logging system. These macros can encapsulate the coding overhead
of using the logging system and can be completely compiled out in release mode.
Macros are a double-edged sword; they are typically harder to understand and debug
since the compiler can
'
t step into them. They can cause unforeseen problems as well.
The compiler literally takes the macro call and replaces it with the macro text. For
example, consider the following code:
'
#define MULT(x, y) x * y
int value = MULT(5 + 5, 10);
What would you expect value to be? It may not be what you think; in the code
above, value will be 55, not 100. Since MULT() is a macro that replaces the call
with the macro text, it ends up expanding to this:
int value =5+5*10;
If MULT() were a function, it would behave as expected and return 100 because the
parameters are evaluated before being pushed onto the stack. This is just one example
of how a macro can bite you.
Here is the final version of the GCC_ERROR() macro:
#ifndef NDEBUG
#define GCC_ERROR(str) \
do \
{\
Search WWH ::




Custom Search