1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#pragma once
#include <plog/Appenders/IAppender.h>
#include <plog/Util.h>
#include <plog/WinApi.h>
#include <iostream>

namespace plog
{
    enum OutputStream
    {
        streamStdOut,
        streamStdErr
    };

    template<class Formatter>
    class PLOG_LINKAGE_HIDDEN ConsoleAppender : public IAppender
    {
    public:
#ifdef _WIN32
#   ifdef _MSC_VER
#       pragma warning(suppress: 26812) //  Prefer 'enum class' over 'enum'
#   endif
        ConsoleAppender(OutputStream outStream = streamStdOut)
            : m_isatty(!!_isatty(_fileno(outStream == streamStdOut ? stdout : stderr)))
            , m_outputStream(outStream == streamStdOut ? std::cout : std::cerr)
            , m_outputHandle()
        {
            if (m_isatty)
            {
                m_outputHandle = GetStdHandle(outStream == streamStdOut ? stdHandle::kOutput : stdHandle::kErrorOutput);
            }
        }
#else
        ConsoleAppender(OutputStream outStream = streamStdOut)
            : m_isatty(!!isatty(fileno(outStream == streamStdOut ? stdout : stderr)))
            , m_outputStream(outStream == streamStdOut ? std::cout : std::cerr)
        {}
#endif

        virtual void write(const Record& record) PLOG_OVERRIDE<--- Function in derived class
        {
            util::nstring str = Formatter::format(record);
            util::MutexLock lock(m_mutex);

            writestr(str);
        }

    protected:
        void writestr(const util::nstring& str)
        {
#ifdef _WIN32
            if (m_isatty)
            {
                const std::wstring& wstr = util::toWide(str);
                WriteConsoleW(m_outputHandle, wstr.c_str(), static_cast<DWORD>(wstr.size()), NULL, NULL);
            }
            else
            {
#   if PLOG_CHAR_IS_UTF8
                m_outputStream << str << std::flush;
#   else
                m_outputStream << util::toNarrow(str, codePage::kActive) << std::flush;
#   endif
            }
#else
            m_outputStream << str << std::flush;
#endif
        }

    private:
#ifdef __BORLANDC__
        static int _isatty(int fd) { return ::isatty(fd); }
#endif

    protected:
        util::Mutex m_mutex;
        const bool  m_isatty;
        std::ostream& m_outputStream;
#ifdef _WIN32
        HANDLE      m_outputHandle;
#endif
    };
}