Archived

This topic is now archived and is closed to further replies.

Log Disabling

This topic is 4956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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));

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
here''s how i do it.

#ifdef NDEBUG
static void log(const char *Format, ...) { }
#else
extern void log(const char *Format, ...);
#endif

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites