plog icon indicating copy to clipboard operation
plog copied to clipboard

DebugOutputAppender (Support linux to avoid compilation error)

Open erakis opened this issue 9 years ago • 5 comments

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
        }
    };
}

erakis avatar Nov 25 '16 13:11 erakis

I have some concerns about this approach:

  1. There is no concept of debug output on non-Windows platforms
  2. qDebug sends log messages to std::cout on Linux/Mac
  3. std::cerr is 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);

SergiusTheBest avatar Nov 27 '16 15:11 SergiusTheBest

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...

erakis avatar Dec 02 '16 15:12 erakis

I'll check that.

SergiusTheBest avatar Dec 02 '16 16:12 SergiusTheBest

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 avatar Apr 21 '18 08:04 mrts

@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?

SergiusTheBest avatar Aug 16 '18 07:08 SergiusTheBest