Skip to main content
gamedev.net
🔒 Locked

Log Disabling

Started by Anima May 18, 2004 at 1:45 PM 7 replies 450+ views
Original Post
Anima
Anima
I''m trying to find the best way of disabling a log file without having to remove all of the Log adding functions. So far I''ve got a simple check in the Log''s Add function, e.g:
void CLog::Add (const char *strText, ...)
{
     if (!m_File)
          return;

     ...
}
 
I know it''s not much, but I don''t like the fact that even disabling the log will require this bit of processing. I''ve considered using macros which would allow the function call to be ignored completely, but as far as I can see it''s impossible to use these with ellipsis. Anyone got any good suggestions?
SiCrane
SiCrane
What compiler are you using? Some pre-processors do support vararg macros. There''s also the extra parenthesis macro approach, which goes like:
#ifdef DEBUG
#define LOG(x) CLog::Add x
#else
#define LOG(X) (void)(0)
#endif

LOG(("string", 1));
Pipo DeClown
Pipo DeClown
class CLog
{
..
bool mEnabled;
}

void CLog::EnableLog( bool Enable = true )
{
mEnabled = Enable;
}

void CLog::Add(..)
{
if( !mEnabled )
return;
..
}
Am I not getting your question, at all? Because I have the idea..


--
You're Welcome,
Rick Wong
- Google | Google for GameDev.net


[edited by - Pipo DeClown on May 18, 2004 3:27:50 PM]
Etnu
Etnu
#ifdef _DEBUG
---------------------------Hello, and Welcome to some arbitrary temporal location in the space-time continuum.
doynax
doynax
here''s how i do it.

#ifdef NDEBUG
static void log(const char *Format, ...) { }
#else
extern void log(const char *Format, ...);
#endif
Anima
Anima
Thanks for your help! If any of you didn''t understand before then I apoligise - I am just concerned about the function call still being made even when the log is disabled. But I will look into the macros idea somemore to try and get them to work with variable arguments / ellipsis. To whomever asked - I''m using Visual Studio 6.0
rypyr
rypyr
shameless plug: Logging Control API

Check out the API docs (main page) for code samples that address your problem. Basically I solved this by using streams and a pre-processor macro.

Example code would be:

In MyLog.h:

#include "LoggingControl.h"

extern bool bLoggingInitialized;
extern HLOGNODE theLog;

bool initLogging();

#if(DEBUG)
#define Log(_x_) if(bLoggingInitialized || initLogging()) LOG_LINE(theLog, _x_)
#else
#define Log(_x_)
#endif



In MyLog.cpp:

#include "MyLog.h"

bool bLoggingInitialized = false;
HLOGNODE theLog = 0;

bool initLogging() {
theLog = CreateLogNode();
return AddFileSinkToLogNode(theLog, "mylog.txt");
}


Elsewhere in your code, use it like this:

Log("Position = " << x << ", " << y << ", " << z);

Anything that can get sent to an ostream will be logged.

Of course you can make the Log() macro more expressive by adding timestamping, __FILE__, __LINE__, etc.

Regards,
Jeff


[ CodeDread ]
Anima
Anima
I have been trying to allow formatting in my log file, but I''ve never been able to use a stringstream like the way you have in your Log documents:

LOG_LINE(hLoggy, "The value of hLoggy is " << hLoggy << " and its address is " << &hLoggy);

I presume the second parameter is a stringstream, anyway. Can someone tell me how to do this?
rypyr
rypyr
The first parameter in my LOG_LINE macro is a handle to a Log Node. The second parameter is a "blob" of text that gets thrown into an ostream.

The macro is defined as something like this:


#define LOG_LINE(hLog, _x_) LockLogNode(hLog); \
GetLogNodeStream(hLog) << _x_ << endl; \
ReleaseAndFlushLogNode(hLog);


The syntax looks a little odd at first, but I find it extremely convenient. And best thing is that since it''s a macro it can be completely compiled out in a release build.

Regards,
Jeff


[ CodeDread ]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.