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