File: CConsoleHandler.h

package info (click to toggle)
vcmi 0.99%2Bdfsg%2Bgit20190113.f06c8a87-2
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 11,136 kB
  • sloc: cpp: 142,615; sh: 315; objc: 248; makefile: 32; ansic: 28; python: 13
file content (94 lines) | stat: -rw-r--r-- 2,607 bytes parent folder | download | duplicates (2)
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
84
85
86
87
88
89
90
91
92
93
94
/*
 * CConsoleHandler.h, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */
#pragma once

namespace EConsoleTextColor
{
/** The color enum is used for colored text console output. */
enum EConsoleTextColor
{
    DEFAULT = -1,
    GREEN,
    RED,
    MAGENTA,
    YELLOW,
    WHITE,
    GRAY,
    TEAL = -2
};
}

/// Class which wraps the native console. It can print text based on
/// the chosen color
class DLL_LINKAGE CConsoleHandler
{
public:
    CConsoleHandler();
    ~CConsoleHandler();
    void start(); //starts listening thread

    template<typename T> void print(const T &data, bool addNewLine = false, EConsoleTextColor::EConsoleTextColor color = EConsoleTextColor::DEFAULT, bool printToStdErr = false)
	{
        TLockGuard _(smx);
#ifndef VCMI_WINDOWS
		// with love from ffmpeg - library is trying to print some warnings from separate thread
		// this results in broken console on Linux. Lock stdout to print all our data at once
		flockfile(stdout);
#endif
        if(color != EConsoleTextColor::DEFAULT) setColor(color);
        if(printToStdErr)
        {
            std::cerr << data;
            if(addNewLine)
            {
                std::cerr << std::endl;
            }
            else
            {
                std::cerr << std::flush;
            }
        }
        else
        {
            std::cout << data;
            if(addNewLine)
            {
                std::cout << std::endl;
            }
            else
            {
                std::cout << std::flush;
            }
        }

        if(color != EConsoleTextColor::DEFAULT) setColor(EConsoleTextColor::DEFAULT);
#ifndef VCMI_WINDOWS
		funlockfile(stdout);
#endif
	}

    std::function<void(const std::string &)> *cb; //function to be called when message is received

private:
    int run();

    void end(); //kills listening thread

    void setColor(EConsoleTextColor::EConsoleTextColor color); //sets color of text appropriate for given logging level

    /// FIXME: Implement CConsoleHandler as singleton, move some logic into CLogConsoleTarget, etc... needs to be disussed:)
    /// Without static, application will crash complaining about mutex deleted. In short: CConsoleHandler gets deleted before
    /// the logging system.
    static boost::mutex smx;

    boost::thread * thread;
};

extern DLL_LINKAGE CConsoleHandler * console;