openMSX
CliComm.hh
Go to the documentation of this file.
1 #ifndef CLICOMM_HH
2 #define CLICOMM_HH
3 
4 #include "span.hh"
5 #include "strCat.hh"
6 #include <string_view>
7 
8 namespace openmsx {
9 
10 class CliComm
11 {
12 public:
13  enum LogLevel {
16  LOGLEVEL_ERROR, // ERROR may give preprocessor name clashes
18  NUM_LEVELS // must be last
19  };
20  enum UpdateType {
21  LED,
32  NUM_UPDATES // must be last
33  };
34 
35  virtual void log(LogLevel level, std::string_view message) = 0;
36  virtual void update(UpdateType type, std::string_view name,
37  std::string_view value) = 0;
40  virtual void updateFiltered(UpdateType type, std::string_view name,
41  std::string_view value) = 0;
42 
43  // convenience methods (shortcuts for log())
44  void printInfo (std::string_view message);
45  void printWarning (std::string_view message);
46  void printError (std::string_view message);
47  void printProgress(std::string_view message);
48 
49  // These overloads are (only) needed for efficiency, because otherwise
50  // the templated overload below is a better match than the 'string_view'
51  // overload above (and we don't want to construct a temp string).
52  void printInfo(const char* message) {
53  printInfo(std::string_view(message));
54  }
55  void printWarning(const char* message) {
56  printWarning(std::string_view(message));
57  }
58  void printError(const char* message) {
59  printError(std::string_view(message));
60  }
61  void printProgress(const char* message) {
62  printProgress(std::string_view(message));
63  }
64 
65  template<typename... Args>
66  void printInfo(Args&& ...args) {
67  auto tmp = tmpStrCat(std::forward<Args>(args)...);
68  printInfo(std::string_view(tmp));
69  }
70  template<typename... Args>
71  void printWarning(Args&& ...args) {
72  auto tmp = tmpStrCat(std::forward<Args>(args)...);
73  printWarning(std::string_view(tmp));
74  }
75  template<typename... Args>
76  void printError(Args&& ...args) {
77  auto tmp = tmpStrCat(std::forward<Args>(args)...);
78  printError(std::string_view(tmp));
79  }
80  template<typename... Args>
81  void printProgress(Args&& ...args) {
82  auto tmp = tmpStrCat(std::forward<Args>(args)...);
83  printProgress(std::string_view(tmp));
84  }
85 
86  // string representations of the LogLevel and UpdateType enums
87  [[nodiscard]] static span<const char* const> getLevelStrings() {
88  static constexpr const char* const levelStr [NUM_LEVELS] = {
89  "info", "warning", "error", "progress"
90  };
91  return levelStr;
92  }
93  [[nodiscard]] static span<const char* const> getUpdateStrings() {
94  static constexpr const char* const updateStr[NUM_UPDATES] = {
95  "led", "setting", "setting-info", "hardware", "plug",
96  "media", "status", "extension", "sounddevice", "connector",
97  "debug"
98  };
99  return updateStr;
100  }
101 
102 protected:
103  CliComm() = default;
104  ~CliComm() = default;
105 };
106 
107 } // namespace openmsx
108 
109 #endif
void printProgress(const char *message)
Definition: CliComm.hh:61
void printProgress(std::string_view message)
Definition: CliComm.cc:20
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.
CliComm()=default
void printError(const char *message)
Definition: CliComm.hh:58
static span< const char *const > getLevelStrings()
Definition: CliComm.hh:87
virtual void update(UpdateType type, std::string_view name, std::string_view value)=0
static span< const char *const > getUpdateStrings()
Definition: CliComm.hh:93
~CliComm()=default
void printInfo(Args &&...args)
Definition: CliComm.hh:66
void printError(std::string_view message)
Definition: CliComm.cc:15
void printWarning(Args &&...args)
Definition: CliComm.hh:71
void printProgress(Args &&...args)
Definition: CliComm.hh:81
void printWarning(std::string_view message)
Definition: CliComm.cc:10
void printError(Args &&...args)
Definition: CliComm.hh:76
void printWarning(const char *message)
Definition: CliComm.hh:55
void printInfo(const char *message)
Definition: CliComm.hh:52
virtual void log(LogLevel level, std::string_view message)=0
Definition: span.hh:126
This file implemented 3 utility functions:
Definition: Autofire.cc:9
TemporaryString tmpStrCat(Ts &&... ts)
Definition: strCat.hh:659