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,
31  NUM_UPDATES // must be last
32  };
33 
34  virtual void log(LogLevel level, std::string_view message) = 0;
35  virtual void update(UpdateType type, std::string_view name,
36  std::string_view value) = 0;
37 
38  // convenience methods (shortcuts for log())
39  void printInfo (std::string_view message);
40  void printWarning (std::string_view message);
41  void printError (std::string_view message);
42  void printProgress(std::string_view message);
43 
44  // These overloads are (only) needed for efficiency, because otherwise
45  // the templated overload below is a better match than the 'string_view'
46  // overload above (and we don't want to construct a temp string).
47  void printInfo(const char* message) {
48  printInfo(std::string_view(message));
49  }
50  void printWarning(const char* message) {
51  printWarning(std::string_view(message));
52  }
53  void printError(const char* message) {
54  printError(std::string_view(message));
55  }
56  void printProgress(const char* message) {
57  printProgress(std::string_view(message));
58  }
59 
60  template<typename... Args>
61  void printInfo(Args&& ...args) {
62  printInfo(std::string_view(strCat(std::forward<Args>(args)...)));
63  }
64  template<typename... Args>
65  void printWarning(Args&& ...args) {
66  printWarning(std::string_view(strCat(std::forward<Args>(args)...)));
67  }
68  template<typename... Args>
69  void printError(Args&& ...args) {
70  printError(std::string_view(strCat(std::forward<Args>(args)...)));
71  }
72  template<typename... Args>
73  void printProgress(Args&& ...args) {
74  printProgress(std::string_view(strCat(std::forward<Args>(args)...)));
75  }
76 
77  // string representations of the LogLevel and UpdateType enums
79  static constexpr const char* const levelStr [NUM_LEVELS] = {
80  "info", "warning", "error", "progress"
81  };
82  return levelStr;
83  }
85  static constexpr const char* const updateStr[NUM_UPDATES] = {
86  "led", "setting", "setting-info", "hardware", "plug",
87  "media", "status", "extension", "sounddevice", "connector"
88  };
89  return updateStr;
90  }
91 
92 protected:
93  CliComm() = default;
94  ~CliComm() = default;
95 };
96 
97 } // namespace openmsx
98 
99 #endif
void printProgress(const char *message)
Definition: CliComm.hh:56
void printError(Args &&...args)
Definition: CliComm.hh:69
virtual void update(UpdateType type, std::string_view name, std::string_view value)=0
void printError(const char *message)
Definition: CliComm.hh:53
void printWarning(Args &&...args)
Definition: CliComm.hh:65
void printInfo(Args &&...args)
Definition: CliComm.hh:61
void printInfo(const char *message)
Definition: CliComm.hh:47
void printProgress(Args &&...args)
Definition: CliComm.hh:73
Definition: span.hh:34
~CliComm()=default
CliComm()=default
void printProgress(std::string_view message)
Definition: CliComm.cc:20
void printError(std::string_view message)
Definition: CliComm.cc:15
void printInfo(std::string_view message)
Definition: CliComm.cc:5
virtual void log(LogLevel level, std::string_view message)=0
void printWarning(const char *message)
Definition: CliComm.hh:50
static span< const char *const > getLevelStrings()
Definition: CliComm.hh:78
static span< const char *const > getUpdateStrings()
Definition: CliComm.hh:84
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
void printWarning(std::string_view message)
Definition: CliComm.cc:10
std::string strCat(Ts &&...ts)
Definition: strCat.hh:573