OXYGENHUB
◆ main openmsx / src / events / CliComm.hh
3.7 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 1
#ifndef CLICOMM_HH
2
#define CLICOMM_HH
3
4
#include "stl.hh"
5
#include "strCat.hh"
6
7
#include <cstdint>
8
#include <span>
9
#include <string_view>
10
#include <utility>
11
12
namespace openmsx {
13
14
class CliComm
15
{
16
public:
17
	enum class LogLevel : uint8_t {
18
		INFO,
19
		WARNING,
20
		LOGLEVEL_ERROR, // ERROR may give preprocessor name clashes
21
		PROGRESS,
22
		NUM // must be last
23
	};
24
	enum class UpdateType : uint8_t {
25
		LED,
26
		SETTING,
27
		SETTING_INFO,
28
		HARDWARE,
29
		PLUG,
30
		MEDIA,
31
		STATUS,
32
		EXTENSION,
33
		SOUND_DEVICE,
34
		CONNECTOR,
35
		DEBUG_UPDT,
36
		NUM // must be last
37
	};
38
39
	/** Log a message with a certain priority level.
40
	  * The 'fraction' parameter is only meaningful for 'level=PROGRESS'.
41
	  * See printProgress() for details.
42
	  */
43
	virtual void log(LogLevel level, std::string_view message, float fraction = 0.0f) = 0;
44
	virtual void update(UpdateType type, std::string_view name,
45
	                    std::string_view value) = 0;
46
	/** Same as update(), but checks that the value for type-name is the
47
	    same as in the previous call. If so do nothing. */
48
	virtual void updateFiltered(UpdateType type, std::string_view name,
49
	                            std::string_view value) = 0;
50
51
	// convenience methods (shortcuts for log())
52
	void printInfo    (std::string_view message);
53
	void printWarning (std::string_view message);
54
	void printError   (std::string_view message);
55
	// 'fraction' should be between 0.0 and 1.0, with these exceptions:
56
	//   a negative value (currently -1.0 is used) means an unknown progress fraction
57
	//   any value > 1.0 is clipped to 1.0
58
	// Progress messages should be send periodically (aim for a few per second).
59
	// The last message in such a sequence MUST always have 'fraction >= // 1.0',
60
	// because this signals the action is done and e.g. removes the progress bar.
61
	void printProgress(std::string_view message, float fraction);
62
63
	// These overloads are (only) needed for efficiency, because otherwise
64
	// the templated overload below is a better match than the 'string_view'
65
	// overload above (and we don't want to construct a temp string).
66
	void printInfo(const char* message) {
67
		printInfo(std::string_view(message));
68
	}
69
	void printWarning(const char* message) {
70
		printWarning(std::string_view(message));
71
	}
72
	void printError(const char* message) {
73
		printError(std::string_view(message));
74
	}
75
	void printProgress(const char* message, float fraction) {
76
		printProgress(std::string_view(message), fraction);
77
	}
78
79
	template<typename... Args>
80
	void printInfo(Args&& ...args) {
81
		auto tmp = tmpStrCat(std::forward<Args>(args)...);
82
		printInfo(std::string_view(tmp));
83
	}
84
	template<typename... Args>
85
	void printWarning(Args&& ...args) {
86
		auto tmp = tmpStrCat(std::forward<Args>(args)...);
87
		printWarning(std::string_view(tmp));
88
	}
89
	template<typename... Args>
90
	void printError(Args&& ...args) {
91
		auto tmp = tmpStrCat(std::forward<Args>(args)...);
92
		printError(std::string_view(tmp));
93
	}
94
95
	// string representations of the LogLevel and UpdateType enums
96
	[[nodiscard]] static auto getLevelStrings() {
97
		static constexpr array_with_enum_index<LogLevel, std::string_view> levelStr = {
98
			"info", "warning", "error", "progress"
99
		};
100
		return std::span{levelStr};
101
	}
102
	[[nodiscard]] static auto getUpdateStrings() {
103
		static constexpr array_with_enum_index<UpdateType, std::string_view> updateStr = {
104
			"led", "setting", "setting-info", "hardware", "plug",
105
			"media", "status", "extension", "sounddevice", "connector",
106
			"debug"
107
		};
108
		return std::span{updateStr};
109
	}
110
111
protected:
112
	CliComm() = default;
113
	~CliComm() = default;
114
};
115
116
[[nodiscard]] inline auto toString(CliComm::LogLevel type)
117
{
118
	return CliComm::getLevelStrings()[std::to_underlying(type)];
119
}
120
121
[[nodiscard]] inline auto toString(CliComm::UpdateType type)
122
{
123
	return CliComm::getUpdateStrings()[std::to_underlying(type)];
124
}
125
126
} // namespace openmsx
127
128
#endif