Game Development Community

Better Cosole Debug Output

by Brett Fattori · 09/27/2005 (3:28 pm) · 1 comments

NOTE: I did this separate (and without knowing about) this resource. The one key difference is that in a release build, this code will not output anything to the console. Sorry Xavier, didn't even see your resource until I had finished posting this one!!


These few changes will add a couple of new function calls to the engine and script that you can use to print debug messages to the console only in debug mode. In a release build, the debugging output will not be displayed. Additionally, with this change, the color of the debug text will be blue (in the console) so it will stand out against everything else.

This was modified from the 1.3 codebase. Please back-up before making these changes!

In file: console.h
At or around line: 41
Change this:
enum Level
   {
      Normal = 0,
      Warning,
      Error,
      NUM_CLASS
   } mLevel;
To:
enum Level
   {
      Normal = 0,
      Warning,
      Error,
      Debug
      NUM_CLASS
   } mLevel;


At or around line: 447
After this:
void errorf(const char *_format, ...);
Add:
/// @note The console window colors debug text as BLUE.
   /// @param _format   A stdlib printf style formatted out put string
   /// @param ...       Variables to be written
   void debugf(const char *_format, ...);


In file: console.cc
At or around line: 487
After this:
void errorf(const char* fmt,...)
{
   va_list argptr;
   va_start(argptr, fmt);
   _printf(ConsoleLogEntry::Error, ConsoleLogEntry::General, fmt, argptr);
   va_end(argptr);
}
Add:
void debugf(const char* fmt,...)
{
#ifdef TORQUE_DEBUG

   va_list argptr;
   va_start(argptr, fmt);
   _printf(ConsoleLogEntry::Debug, ConsoleLogEntry::General, fmt, argptr);
   va_end(argptr);

#endif
}


In file: consoleFunctions.cc
At or around line: 636
After this:
ConsoleFunctionGroupBegin( Output, "Functions to output to the console." );
Add:
// Only echo in a debug build
ConsoleFunction(echodebug, void, 2, 0, "echodebug(text [, ... ])")
{
#ifdef TORQUE_DEBUG

   U32 len = 0;
   S32 x;
   for(x = 1; x < argc; x++)
      len += dStrlen(argv[x]);

   char *ret = Con::getReturnBuffer(len + 1);
   ret[0] = 0;
   for(x = 1; x < argc; x++)
      dStrcat(ret, argv[x]);

   Con::debugf("%s", ret);
   ret[0] = 0;

#endif
}


In file: guiConsole.cc
At or around line: 96
After this:
case ConsoleLogEntry::Error:    dglSetBitmapModulation(mProfile->mFontColorNA); break;
Add:
case ConsoleLogEntry::Debug:    dglSetBitmapModulation(mProfile->mFontColorSEL); break;


In file: common/ui/defaultProfiles.cs
At or around line: 384
Change this:
if(!isObject(GuiConsoleProfile)) new GuiControlProfile ("GuiConsoleProfile")
{
   fontType = ($platform $= "macos") ? "Courier New" : "Lucida Console";
   fontSize = ($platform $= "macos") ? 14 : 12;
   fontColor = "0 0 0";
   fontColorHL = "130 130 130";
   fontColorNA = "255 0 0";
   fontColors[6] = "50 50 50";
   fontColors[7] = "50 50 0";  
   fontColors[8] = "0 0 50"; 
   fontColors[9] = "0 50 0";   
};
To:
if(!isObject(GuiConsoleProfile)) new GuiControlProfile ("GuiConsoleProfile")
{
   fontType = ($platform $= "macos") ? "Courier New" : "Lucida Console";
   fontSize = ($platform $= "macos") ? 14 : 12;
   fontColor = "0 0 0";
   fontColorHL = "130 130 130";
   fontColorNA = "255 0 0";
   fontColorSEL = "0 0 255";
   fontColors[6] = "50 50 50";
   fontColors[7] = "50 50 0";  
   fontColors[8] = "0 0 50"; 
   fontColors[9] = "0 50 0";   
};

Now anywhere in the engine, you can use something like:
Con::debugf("Player position: %g %g %g", player.getPosition().x, player.getPosition().y, player.getPosition().z);

And in script you can do something like this:
echodebug("Dump of player position: " @ %playerPosX SPC %playerPosY SPC %playerPosZ);

#1
09/29/2005 (8:02 pm)
Hehehe, no worries Brett ;)

Displaying them only in Debug is a good idea, I never though of that, good job!