Logging & Filters … etc
I think that is extremely beneficial for every desktop application having an flexible yet powerful logging system.
This can “log” to a file, to a console (internal or not) or both. Anyway, the most important thing are the different log levels, and filters.
Let’s say that we have Text, Info, Warning, Error … each represents an level. Then we can setup a filter to “show” only the Warnings, or the Errors supressing all the other messages. This is extremly useful if you have bunch messages showing up in your log file / console.
Now let’s see some code
#define SAFE_ARRAY_DELETE(x) if(x) {delete [] x; x=NULL;}
#define INRANGE(x,min,max) ( (x >= min) && (x < max) )
namespace Log
{
const int Text = 0;
const int Info = 1;
const int Warning = 2;
const int Error = 3;
// used only by the filtering
const int Any = 4;
};
static int g_logFilter = Log::Any;
void setConsoleLogFilter( const int logFilter )
{
if( INRANGE(logFilter,0,5) )
{
g_logFilter = logFilter;
return;
}
g_logFilter = Log::Any;
}
void consoleLog( const int logLevel, const char *msg, ... )
{
va_list ap;
// is filtering turned on?
if( g_logFilter != Log::Any && g_logFilter != logLevel )
{
return;
}
char *text = new char[ 2048 ];
assert( text );
memset(text,0,sizeof(text));
va_start(ap, msg);
vsprintf(text, msg, ap);
va_end(ap);
switch( logLevel )
{
case Log::Info:
printf( "INFO: %s\n", text );
break;
case Log::Warning:
printf( "WARNING: %s\n", text );
break;
case Log::Error:
printf( "ERROR: %s\n", text );
break;
default:
printf( "%s\n", text );
break;
}
SAFE_ARRAY_DELETE( text );
}
This is very very straightforward … it’s C++, but it can be adapted to C, by replacing the memory managment stuff … new with malloc, delete [] with free
and moving out the constants into an enum
It works out great for me, and I use something very very similar to this on a day to day basis
Happy coding!
2 Comments »
Leave a comment
About
Blog moved to Szabster.net …
Formal Introduction: 100% Computer Science Geek, made from 100% pure and recyclable electrons.
Current Occupation: Software Engineer and a lot more
I speak fluently the following languages: Hungarian, Romanian, English, French, C/C++/C#, Delphi, HTML, CSS, JavaScript, PHP, MySQL, Python, and a lot more.
I am known as: Icebreaker, coder_master or Lone Wolf (Wolverine).
I can be also found on IRC: DynastyNet (network) as coderguy.
Favorite Quote: I’m never wrong. I thought I was wrong once but I was mistaken. - Jeff Mayer
My Favorite Linux Distribution is: Ubuntu
Place where I would like to relax: On the beach …
… and with who? : With a million naked girls.
My Personal Geek Code:
—–BEGIN GEEK CODE BLOCK—–
Version: 3.1.2
GCS/TW/IT dpu s: a? C++(++++) UBL
P+ L+(++) E— W+++ N+ o+
K- w++>+++ !O M V- PS+(+++) PE++
Y PGP>+ t+@ 5? X+ R>+++ !tv
b++(+++) DI? D+>+++$ G e h! r– y++
——END GEEK CODE BLOCK——
-
Archives
- May 2008 (15)
- April 2008 (12)
- March 2008 (7)
- February 2008 (11)
- January 2008 (3)
- December 2007 (2)
- November 2007 (8)
- October 2007 (6)
- August 2007 (1)
- July 2007 (2)
- May 2007 (1)
- April 2007 (6)
-
Categories
- 4k 64k
- Blogging
- box2d
- Chick
- Coding
- demo
- directx
- documents
- Download
- easter
- egg
- flash
- framework
- Free
- Friends
- Game
- Game Engine
- Gamer
- Games
- Geek
- humor
- Internet
- intro
- Linux
- mac
- macosx
- music
- Nerd
- Open Source
- OpenGL
- Operating System
- physics
- Poetry
- Programming
- rc
- Real Life
- resources
- Scene
- sdl
- skype
- Social Networking
- software
- sp1
- texture
- Toughts on things
- Ubuntu
- Uncategorized
- Vista
- web
- winamp
- Windows
- word
-
RSS
Entries RSS
Comments RSS




Looks good, however you should either allocate the buffer once, or should use a global or smthn, since this way you can have a quite big memory / performance hit because of the memory subsystem.
J.
J(ancsi)??? … This was changed a long long before, perhaps I should update the post, but this blog is becoming deprecated
A global buffer is not a good idea when it comes to multi-threading …. a static local buffer is the best solution …