Open3D (C++ API)
Console.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <Eigen/Core>
30 #include <string>
31 #include <vector>
32 
33 #define FMT_HEADER_ONLY 1
34 #define FMT_STRING_ALIAS 1
35 #include <fmt/format.h>
36 #include <fmt/printf.h>
37 #include <fmt/ranges.h>
38 
39 #define DEFAULT_IO_BUFFER_SIZE 1024
40 
41 namespace open3d {
42 namespace utility {
43 
44 enum class VerbosityLevel {
49  Error = 0,
55  Warning = 1,
58  Info = 2,
61  Debug = 3,
62 };
63 
64 class Logger {
65 public:
66  enum class TextColor {
67  Black = 0,
68  Red = 1,
69  Green = 2,
70  Yellow = 3,
71  Blue = 4,
72  Magenta = 5,
73  Cyan = 6,
74  White = 7
75  };
76 
77  Logger() : verbosity_level_(VerbosityLevel::Info) {}
78  Logger(Logger const &) = delete;
79  void operator=(Logger const &) = delete;
80 
81  static Logger &i() {
82  static Logger instance;
83  return instance;
84  }
85 
86  void VError[[noreturn]](const char *format, fmt::format_args args) const {
87  std::string err_msg = fmt::vformat(format, args);
88  err_msg = fmt::format("[Open3D ERROR] {}", err_msg);
89  err_msg = ColorString(err_msg, TextColor::Red, 1);
90  throw std::runtime_error(err_msg);
91  }
92 
93  void VWarning(const char *format, fmt::format_args args) const {
94  if (verbosity_level_ >= VerbosityLevel::Warning) {
95  std::string err_msg = fmt::vformat(format, args);
96  err_msg = fmt::format("[Open3D WARNING] {}", err_msg);
97  err_msg = ColorString(err_msg, TextColor::Yellow, 1);
98  print_fcn_(err_msg);
99  }
100  }
101 
102  void VInfo(const char *format, fmt::format_args args) const {
103  if (verbosity_level_ >= VerbosityLevel::Info) {
104  std::string err_msg = fmt::vformat(format, args);
105  err_msg = fmt::format("[Open3D INFO] {}", err_msg);
106  print_fcn_(err_msg);
107  }
108  }
109 
110  void VDebug(const char *format, fmt::format_args args) const {
111  if (verbosity_level_ >= VerbosityLevel::Debug) {
112  std::string err_msg = fmt::vformat(format, args);
113  err_msg = fmt::format("[Open3D DEBUG] {}", err_msg);
114  print_fcn_(err_msg);
115  }
116  }
117 
118  template <typename... Args>
119  void Error[[noreturn]](const char *format, const Args &... args) const {
120  VError(format, fmt::make_format_args(args...));
121  }
122 
123  template <typename... Args>
124  void Warning(const char *format, const Args &... args) const {
125  VWarning(format, fmt::make_format_args(args...));
126  }
127 
128  template <typename... Args>
129  void Info(const char *format, const Args &... args) const {
130  VInfo(format, fmt::make_format_args(args...));
131  }
132 
133  template <typename... Args>
134  void Debug(const char *format, const Args &... args) const {
135  VDebug(format, fmt::make_format_args(args...));
136  }
137 
138 protected:
144  void ChangeConsoleColor(TextColor text_color, int highlight_text) const;
145  void ResetConsoleColor() const;
147  std::string ColorString(const std::string &text,
148  TextColor text_color,
149  int highlight_text) const;
150 
151 public:
153  std::function<void(const std::string &)> print_fcn_ =
154  [](const std::string &msg) {
155  fmt::print(msg);
156  fmt::print("\n");
157  };
158 };
159 
164 inline void SetVerbosityLevel(VerbosityLevel level) {
165  Logger::i().verbosity_level_ = level;
166 }
167 
170  return Logger::i().verbosity_level_;
171 }
172 
173 template <typename... Args>
174 inline void LogError[[noreturn]](const char *format, const Args &... args) {
175  Logger::i().VError(format, fmt::make_format_args(args...));
176 }
177 
178 template <typename... Args>
179 inline void LogWarning(const char *format, const Args &... args) {
180  Logger::i().VWarning(format, fmt::make_format_args(args...));
181 }
182 
183 template <typename... Args>
184 inline void LogInfo(const char *format, const Args &... args) {
185  Logger::i().VInfo(format, fmt::make_format_args(args...));
186 }
187 
188 template <typename... Args>
189 inline void LogDebug(const char *format, const Args &... args) {
190  Logger::i().VDebug(format, fmt::make_format_args(args...));
191 }
192 
194 public:
195  VerbosityContextManager(VerbosityLevel level) : level_(level) {}
196 
197  void enter() {
198  level_backup_ = Logger::i().verbosity_level_;
199  Logger::i().verbosity_level_ = level_;
200  }
201 
202  void exit() { Logger::i().verbosity_level_ = level_backup_; }
203 
204 private:
205  VerbosityLevel level_;
206  VerbosityLevel level_backup_;
207 };
208 
210 public:
211  ConsoleProgressBar(size_t expected_count,
212  const std::string &progress_info,
213  bool active = false) {
214  reset(expected_count, progress_info, active);
215  }
216 
217  void reset(size_t expected_count,
218  const std::string &progress_info,
219  bool active) {
220  expected_count_ = expected_count;
221  current_count_ = static_cast<size_t>(-1); // Guaranteed to wraparound
222  progress_info_ = progress_info;
223  progress_pixel_ = 0;
224  active_ = active;
225  operator++();
226  }
227 
229  current_count_++;
230  if (!active_) {
231  return *this;
232  }
233  if (current_count_ >= expected_count_) {
234  fmt::print("{}[{}] 100%\n", progress_info_,
235  std::string(resolution_, '='));
236  } else {
237  size_t new_progress_pixel =
238  int(current_count_ * resolution_ / expected_count_);
239  if (new_progress_pixel > progress_pixel_) {
240  progress_pixel_ = new_progress_pixel;
241  int percent = int(current_count_ * 100 / expected_count_);
242  fmt::print("{}[{}>{}] {:d}%\r", progress_info_,
243  std::string(progress_pixel_, '='),
244  std::string(resolution_ - 1 - progress_pixel_, ' '),
245  percent);
246  fflush(stdout);
247  }
248  }
249  return *this;
250  }
251 
252 private:
253  const size_t resolution_ = 40;
254  size_t expected_count_;
255  size_t current_count_;
256  std::string progress_info_;
257  size_t progress_pixel_;
258  bool active_;
259 };
260 
261 std::string GetCurrentTimeStamp();
262 
263 std::string GetProgramOptionAsString(int argc,
264  char **argv,
265  const std::string &option,
266  const std::string &default_value = "");
267 
268 int GetProgramOptionAsInt(int argc,
269  char **argv,
270  const std::string &option,
271  const int default_value = 0);
272 
273 double GetProgramOptionAsDouble(int argc,
274  char **argv,
275  const std::string &option,
276  const double default_value = 0.0);
277 
278 Eigen::VectorXd GetProgramOptionAsEigenVectorXd(
279  int argc,
280  char **argv,
281  const std::string &option,
282  const Eigen::VectorXd default_value = Eigen::VectorXd::Zero(0));
283 
284 bool ProgramOptionExists(int argc, char **argv, const std::string &option);
285 
286 bool ProgramOptionExistsAny(int argc,
287  char **argv,
288  const std::vector<std::string> &options);
289 
290 } // namespace utility
291 } // namespace open3d
void VError(const char *format, fmt::format_args args) const
Definition: Console.h:86
std::string GetProgramOptionAsString(int argc, char **argv, const std::string &option, const std::string &default_value)
Definition: Console.cpp:104
void Info(const char *format, const Args &... args) const
Definition: Console.h:129
Args({1<< 9, 1<< 11})
VerbosityContextManager(VerbosityLevel level)
Definition: Console.h:195
void exit()
Definition: Console.h:202
double GetProgramOptionAsDouble(int argc, char **argv, const std::string &option, const double default_value)
Definition: Console.cpp:137
ConsoleProgressBar & operator++()
Definition: Console.h:228
Logger()
Definition: Console.h:77
void LogError(const char *format, const Args &... args)
Definition: Console.h:174
void LogWarning(const char *format, const Args &... args)
Definition: Console.h:179
void Debug(const char *format, const Args &... args) const
Definition: Console.h:134
bool ProgramOptionExistsAny(int argc, char **argv, const std::vector< std::string > &options)
Definition: Console.cpp:189
void VWarning(const char *format, fmt::format_args args) const
Definition: Console.h:93
TextColor
Definition: Console.h:66
void VInfo(const char *format, fmt::format_args args) const
Definition: Console.h:102
void LogDebug(const char *format, const Args &... args)
Definition: Console.h:189
std::string GetCurrentTimeStamp()
Definition: Console.cpp:99
VerbosityLevel GetVerbosityLevel()
Get global verbosity level of Open3D.
Definition: Console.h:169
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c int
Definition: K4aPlugin.cpp:475
int GetProgramOptionAsInt(int argc, char **argv, const std::string &option, const int default_value)
Definition: Console.cpp:116
VerbosityLevel
Definition: Console.h:44
void enter()
Definition: Console.h:197
void SetVerbosityLevel(VerbosityLevel level)
Definition: Console.h:164
Definition: Open3DViewer.h:29
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c k4a_image_t image_handle uint8_t image_handle image_handle image_handle image_handle image_handle timestamp_usec white_balance image_handle k4a_device_configuration_t config device_handle char size_t serial_number_size bool int32_t int32_t int32_t int32_t default_value
Definition: K4aPlugin.cpp:644
void Warning(const char *format, const Args &... args) const
Definition: Console.h:124
ConsoleProgressBar(size_t expected_count, const std::string &progress_info, bool active=false)
Definition: Console.h:211
filament::Texture::InternalFormat format
Definition: FilamentResourceManager.cpp:155
static Logger & i()
Definition: Console.h:81
void VDebug(const char *format, fmt::format_args args) const
Definition: Console.h:110
void reset(size_t expected_count, const std::string &progress_info, bool active)
Definition: Console.h:217
Eigen::VectorXd GetProgramOptionAsEigenVectorXd(int argc, char **argv, const std::string &option, const Eigen::VectorXd default_value)
Definition: Console.cpp:156
Definition: Console.h:209
VerbosityLevel verbosity_level_
Definition: Console.h:152
Definition: Console.h:64
bool ProgramOptionExists(int argc, char **argv, const std::string &option)
Definition: Console.cpp:185
void LogInfo(const char *format, const Args &... args)
Definition: Console.h:184