plog
plog copied to clipboard
DebugOutputAppender (Support linux to avoid compilation error)
What do you think of :
#pragma once
namespace plog
{
template<class Formatter>
class DebugOutputAppender : public IAppender
{
public:
virtual void write(const Record& record)
{
#if defined(_WIN32) && defined(_MSC_VER) // Win32 and Visual Studio
::OutputDebugStringW(Formatter::format(record).c_str());
#elif defined(QT_VERSION) // Linux on Qt
qDebug() << record;
#else // Anything else...
std::cerr << record;
#endif
}
};
}
I have some concerns about this approach:
- There is no concept of debug output on non-Windows platforms
qDebugsends log messages tostd::couton Linux/Macstd::cerris for errors
I think a better way will be to explicitly specify desired output for debug messages:
static plog::RollingFileAppender<plog::CsvFormatter> fileAppender("log.csv");
#ifdef _WIN32
static plog::DebugOutputAppender<plog::TxtFormatter> debugAppender;
#else
static plog::ConsoleAppender<plog::TxtFormatter> debugAppender;
#endif
plog::init(plog::debug, &fileAppender).addAppender(&debugAppender);
I'm totally agree with you Sergius :)
However, AFAIK on Linux with QtCreator the plog::ConsoleAppender does not output to the QT console so it is a bit annoying...
I'll check that.
Why not simply add QtDebugOutputAppender that sends messages to qDebug() to assure they appear in Qt console?
Building on the example above:
static plog::QtDebugOutputAppender<plog::TxtFormatter> qtDebugAppender();
plog::init(plog::debug, &qtDebugAppender);
I can send a pull request if this looks good to you.
@mrts I'm not a Qt-guy (at least currently) so I cannot say how useful it would be. Also I see that there are qInfo, qWarning, qFatal. Do you use them?