[FR] Logger::writeToLog outputDebugString flag


#1

Is it possible to have a boolean flag added to Logger so that when currentLogger == nullptr that it doesn’t call outputDebugString ?

I would like to be able to enable/disable all logging using a FileLogger… but if I set the currentLogger to nullptr it still sends the logging to stdio even in a Release build

void Logger::writeToLog (const String& message, bool bSendToDebug /* = true */)
{
    if (currentLogger != nullptr)
       currentLogger->logMessage (message);
    else
        if (bSendToDebug)
            outputDebugString (message);
}

(or make bSendToDebug a static variable)

Unfortunately there’s no simple way to subclass FileLogger to achieve this change.

Thanks,

Rail


#2

You can subclass FileLogger and add an “enable/disable” switch to your logMessage method:

void FileLogger::logMessage (const String& message)
{
    if (loggingEnabled)
    {
        const ScopedLock sl (logLock);
        DBG (message);
        FileOutputStream out (logFile, 256);
        out << message << newLine;
    }
}

This is also a nice place to add any custom decoration to your log messages, like a date and time.


#3

Or use this minimal one:

(too bad that the forum loses the formatting when quoting…)


#4

I’ve added the addition of logging levels to our backlog.


#5

I had already tried:

void CFileLogger::logMessage (const String& message)
{
    if (m_bLoggingEnabled)
        {
        FileLogger::logMessage (message);
        }
}

which didn’t help… Unless I never set logging to nullptr (except at shutdown).

Rail


#6

Why doesn’t the above help? Do you need to actually set the logger to nullptr? If so, why?


#7

I have a project which has scattered calls to

Logger::writeToLog (message);

and to stop logging I set the logger to nullptr (but obviously that still kept logging going to stderr).

So I tried the above but writeToLog() with the currentLogger == nullptr still sends the output to stderr.

I realize that if I change my original code and don’t set the logger to nullptr and just change my flag instead:

m_pLogger->setEnabled (m_bLoggingEnabled);

that it’ll work as I want. I just have to set the logger to nullptr at shutdown of course.

Cheers,

Rail


#8

Perhaps simply change the comments for setCurrentLogger();

/** Sets the current logging class to use.

    Note that the object passed in will not be owned or deleted by the logger, so
    the caller must make sure that it is not deleted while still being used.
    A null pointer can be passed-in to disable any logging.
*/

Cheers,

Rail


#9

Yes, that comment’s a bit misleading, I’ll improve it…