Open3D (C++ API)  0.12.0
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 <iostream>
31 #include <string>
32 #include <vector>
33 
34 #ifndef FMT_HEADER_ONLY
35 #define FMT_HEADER_ONLY 1
36 #endif
37 #ifndef FMT_STRING_ALIAS
38 #define FMT_STRING_ALIAS 1
39 #endif
40 #include <fmt/format.h>
41 #include <fmt/printf.h>
42 #include <fmt/ranges.h>
43 
44 #define DEFAULT_IO_BUFFER_SIZE 1024
45 
46 namespace open3d {
47 namespace utility {
48 
49 enum class VerbosityLevel {
54  Error = 0,
60  Warning = 1,
63  Info = 2,
66  Debug = 3,
67 };
68 
69 class Logger {
70 public:
71  enum class TextColor {
72  Black = 0,
73  Red = 1,
74  Green = 2,
75  Yellow = 3,
76  Blue = 4,
77  Magenta = 5,
78  Cyan = 6,
79  White = 7
80  };
81 
82  Logger() : verbosity_level_(VerbosityLevel::Info) {}
83  Logger(Logger const &) = delete;
84  void operator=(Logger const &) = delete;
85 
86  static Logger &i() {
87  static Logger instance;
88  return instance;
89  }
90 
91  void VError [[noreturn]] (const char *format, fmt::format_args args) const {
92  std::string err_msg = fmt::vformat(format, args);
93  err_msg = fmt::format("[Open3D ERROR] {}", err_msg);
94  err_msg = ColorString(err_msg, TextColor::Red, 1);
95  throw std::runtime_error(err_msg);
96  }
97 
98  void VWarning(const char *format, fmt::format_args args) const {
99  if (verbosity_level_ >= VerbosityLevel::Warning) {
100  std::string err_msg = fmt::vformat(format, args);
101  err_msg = fmt::format("[Open3D WARNING] {}", err_msg);
102  err_msg = ColorString(err_msg, TextColor::Yellow, 1);
103  print_fcn_(err_msg);
104  }
105  }
106 
107  void VInfo(const char *format, fmt::format_args args) const {
108  if (verbosity_level_ >= VerbosityLevel::Info) {
109  std::string err_msg = fmt::vformat(format, args);
110  err_msg = fmt::format("[Open3D INFO] {}", err_msg);
111  print_fcn_(err_msg);
112  }
113  }
114 
115  void VDebug(const char *format, fmt::format_args args) const {
116  if (verbosity_level_ >= VerbosityLevel::Debug) {
117  std::string err_msg = fmt::vformat(format, args);
118  err_msg = fmt::format("[Open3D DEBUG] {}", err_msg);
119  print_fcn_(err_msg);
120  }
121  }
122 
123  template <typename... Args>
124  void Error [[noreturn]] (const char *format, const Args &... args) const {
125  VError(format, fmt::make_format_args(args...));
126  }
127 
128  template <typename... Args>
129  void Warning(const char *format, const Args &... args) const {
130  VWarning(format, fmt::make_format_args(args...));
131  }
132 
133  template <typename... Args>
134  void Info(const char *format, const Args &... args) const {
135  VInfo(format, fmt::make_format_args(args...));
136  }
137 
138  template <typename... Args>
139  void Debug(const char *format, const Args &... args) const {
140  VDebug(format, fmt::make_format_args(args...));
141  }
142 
143 protected:
149  void ChangeConsoleColor(TextColor text_color, int highlight_text) const;
150  void ResetConsoleColor() const;
152  std::string ColorString(const std::string &text,
153  TextColor text_color,
154  int highlight_text) const;
155 
156 public:
158  std::function<void(const std::string &)> print_fcn_ =
159  [](const std::string &msg) { std::cout << msg << std::endl; };
160 };
161 
166 inline void SetVerbosityLevel(VerbosityLevel level) {
167  Logger::i().verbosity_level_ = level;
168 }
169 
172  return Logger::i().verbosity_level_;
173 }
174 
175 template <typename... Args>
176 inline void LogError [[noreturn]] (const char *format, const Args &... args) {
177  Logger::i().VError(format, fmt::make_format_args(args...));
178 }
179 
180 template <typename... Args>
181 inline void LogWarning(const char *format, const Args &... args) {
182  Logger::i().VWarning(format, fmt::make_format_args(args...));
183 }
184 
185 template <typename... Args>
186 inline void LogInfo(const char *format, const Args &... args) {
187  Logger::i().VInfo(format, fmt::make_format_args(args...));
188 }
189 
190 template <typename... Args>
191 inline void LogDebug(const char *format, const Args &... args) {
192  Logger::i().VDebug(format, fmt::make_format_args(args...));
193 }
194 
196 public:
197  VerbosityContextManager(VerbosityLevel level) : level_(level) {}
198 
199  void enter() {
200  level_backup_ = Logger::i().verbosity_level_;
201  Logger::i().verbosity_level_ = level_;
202  }
203 
204  void exit() { Logger::i().verbosity_level_ = level_backup_; }
205 
206 private:
207  VerbosityLevel level_;
208  VerbosityLevel level_backup_;
209 };
210 
212 public:
213  ConsoleProgressBar(size_t expected_count,
214  const std::string &progress_info,
215  bool active = false) {
216  reset(expected_count, progress_info, active);
217  }
218 
219  void reset(size_t expected_count,
220  const std::string &progress_info,
221  bool active) {
222  expected_count_ = expected_count;
223  current_count_ = static_cast<size_t>(-1); // Guaranteed to wraparound
224  progress_info_ = progress_info;
225  progress_pixel_ = 0;
226  active_ = active;
227  operator++();
228  }
229 
231  current_count_++;
232  if (!active_) {
233  return *this;
234  }
235  if (current_count_ >= expected_count_) {
236  fmt::print("{}[{}] 100%\n", progress_info_,
237  std::string(resolution_, '='));
238  } else {
239  size_t new_progress_pixel =
240  int(current_count_ * resolution_ / expected_count_);
241  if (new_progress_pixel > progress_pixel_) {
242  progress_pixel_ = new_progress_pixel;
243  int percent = int(current_count_ * 100 / expected_count_);
244  fmt::print("{}[{}>{}] {:d}%\r", progress_info_,
245  std::string(progress_pixel_, '='),
246  std::string(resolution_ - 1 - progress_pixel_, ' '),
247  percent);
248  fflush(stdout);
249  }
250  }
251  return *this;
252  }
253 
254 private:
255  const size_t resolution_ = 40;
256  size_t expected_count_;
257  size_t current_count_;
258  std::string progress_info_;
259  size_t progress_pixel_;
260  bool active_;
261 };
262 
263 std::string GetCurrentTimeStamp();
264 
265 std::string GetProgramOptionAsString(int argc,
266  char **argv,
267  const std::string &option,
268  const std::string &default_value = "");
269 
270 int GetProgramOptionAsInt(int argc,
271  char **argv,
272  const std::string &option,
273  const int default_value = 0);
274 
275 double GetProgramOptionAsDouble(int argc,
276  char **argv,
277  const std::string &option,
278  const double default_value = 0.0);
279 
280 Eigen::VectorXd GetProgramOptionAsEigenVectorXd(
281  int argc,
282  char **argv,
283  const std::string &option,
284  const Eigen::VectorXd default_value = Eigen::VectorXd::Zero(0));
285 
286 bool ProgramOptionExists(int argc, char **argv, const std::string &option);
287 
288 bool ProgramOptionExistsAny(int argc,
289  char **argv,
290  const std::vector<std::string> &options);
291 
292 } // namespace utility
293 } // namespace open3d
void VError(const char *format, fmt::format_args args) const
Definition: Console.h:91
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:134
VerbosityContextManager(VerbosityLevel level)
Definition: Console.h:197
void exit()
Definition: Console.h:204
double GetProgramOptionAsDouble(int argc, char **argv, const std::string &option, const double default_value)
Definition: Console.cpp:137
ConsoleProgressBar & operator++()
Definition: Console.h:230
Logger()
Definition: Console.h:82
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
void LogWarning(const char *format, const Args &... args)
Definition: Console.h:181
void Debug(const char *format, const Args &... args) const
Definition: Console.h:139
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:98
TextColor
Definition: Console.h:71
void VInfo(const char *format, fmt::format_args args) const
Definition: Console.h:107
void LogDebug(const char *format, const Args &... args)
Definition: Console.h:191
std::string GetCurrentTimeStamp()
Definition: Console.cpp:99
VerbosityLevel GetVerbosityLevel()
Get global verbosity level of Open3D.
Definition: Console.h:171
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:479
int GetProgramOptionAsInt(int argc, char **argv, const std::string &option, const int default_value)
Definition: Console.cpp:116
VerbosityLevel
Definition: Console.h:49
void enter()
Definition: Console.h:199
void SetVerbosityLevel(VerbosityLevel level)
Definition: Console.h:166
Definition: PinholeCameraIntrinsic.cpp:35
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:648
void Warning(const char *format, const Args &... args) const
Definition: Console.h:129
ConsoleProgressBar(size_t expected_count, const std::string &progress_info, bool active=false)
Definition: Console.h:213
filament::Texture::InternalFormat format
Definition: FilamentResourceManager.cpp:191
static Logger & i()
Definition: Console.h:86
void VDebug(const char *format, fmt::format_args args) const
Definition: Console.h:115
void reset(size_t expected_count, const std::string &progress_info, bool active)
Definition: Console.h:219
Eigen::VectorXd GetProgramOptionAsEigenVectorXd(int argc, char **argv, const std::string &option, const Eigen::VectorXd default_value)
Definition: Console.cpp:156
Definition: Console.h:211
VerbosityLevel verbosity_level_
Definition: Console.h:157
Definition: Console.h:69
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:186