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
t0m
January 3, 2017, 1:16pm
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.
1 Like
daniel
January 3, 2017, 1:26pm
3
Or use this minimal one:
…it only logs to the console, if no logger was set. So what I was about to do (just was not important enough), was to write:
class SwallowLogger : public Logger
{
void logMessage (const String &message) override {}
};
Logger::setCurrentLogger (new SwallowLogger());
But actually I would wish for an additional int or bitmask to add a log level to the writeToLog() call:
void Logger::writeToLog (const String& message, int level=0)
{
if (currentLogger && level >= currentLogger->getLogLevel…
(too bad that the forum loses the formatting when quoting…)
t0m
January 3, 2017, 3:22pm
4
I’ve added the addition of logging levels to our backlog.
2 Likes
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
t0m
January 3, 2017, 6:51pm
6
Why doesn’t the above help? Do you need to actually set the logger to nullptr? If so, why?
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
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
jules
January 4, 2017, 9:01am
9
Yes, that comment’s a bit misleading, I’ll improve it…