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