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 
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 {
96  fmt::print("[Open3D WARNING] ");
97  fmt::vprint(format, args);
98  fmt::print("\n");
100  }
101  }
102 
103  void VInfo(const char *format, fmt::format_args args) const {
105  fmt::print("[Open3D INFO] ");
106  fmt::vprint(format, args);
107  fmt::print("\n");
108  }
109  }
110 
111  void VDebug(const char *format, fmt::format_args args) const {
113  fmt::print("[Open3D DEBUG] ");
114  fmt::vprint(format, args);
115  fmt::print("\n");
116  }
117  }
118 
119  template <typename... Args>
120  void Error[[noreturn]](const char *format, const Args &... args) const {
121  VError(format, fmt::make_format_args(args...));
122  }
123 
124  template <typename... Args>
125  void Warning(const char *format, const Args &... args) const {
126  VWarning(format, fmt::make_format_args(args...));
127  }
128 
129  template <typename... Args>
130  void Info(const char *format, const Args &... args) const {
131  VInfo(format, fmt::make_format_args(args...));
132  }
133 
134  template <typename... Args>
135  void Debug(const char *format, const Args &... args) const {
136  VDebug(format, fmt::make_format_args(args...));
137  }
138 
139  template <typename... Args>
140  void Errorf[[noreturn]](const char *format, const Args &... args) const {
141  std::string err_msg = fmt::sprintf(format, args...);
142  err_msg = fmt::format("[Open3D Error] {}", err_msg);
143  err_msg = ColorString(err_msg, TextColor::Red, 1);
144  throw std::runtime_error(err_msg);
145  }
146 
147  template <typename... Args>
148  void Warningf(const char *format, const Args &... args) const {
151  fmt::print("[Open3D WARNING] ");
152  fmt::printf(format, args...);
154  fmt::print("\n");
155  }
156  }
157 
158  template <typename... Args>
159  void Infof(const char *format, const Args &... args) const {
161  fmt::print("[Open3D INFO] ");
162  fmt::printf(format, args...);
163  fmt::print("\n");
164  }
165  }
166 
167  template <typename... Args>
168  void Debugf(const char *format, const Args &... args) const {
170  fmt::print("[Open3D DEBUG] ");
171  fmt::printf(format, args...);
172  fmt::print("\n");
173  }
174  }
175 
176 protected:
181  void ChangeConsoleColor(TextColor text_color, int highlight_text) const;
182  void ResetConsoleColor() const;
184  std::string ColorString(const std::string &text,
185  TextColor text_color,
186  int highlight_text) const;
187 
188 public:
190 };
191 
192 inline void SetVerbosityLevel(VerbosityLevel level) {
193  Logger::i().verbosity_level_ = level;
194 }
195 
197  return Logger::i().verbosity_level_;
198 }
199 
200 template <typename... Args>
201 inline void LogError[[noreturn]](const char *format, const Args &... args) {
202  Logger::i().VError(format, fmt::make_format_args(args...));
203 }
204 
205 template <typename... Args>
206 inline void LogWarning(const char *format, const Args &... args) {
207  Logger::i().VWarning(format, fmt::make_format_args(args...));
208 }
209 
210 template <typename... Args>
211 inline void LogInfo(const char *format, const Args &... args) {
212  Logger::i().VInfo(format, fmt::make_format_args(args...));
213 }
214 
215 template <typename... Args>
216 inline void LogDebug(const char *format, const Args &... args) {
217  Logger::i().VDebug(format, fmt::make_format_args(args...));
218 }
219 
220 template <typename... Args>
221 inline void LogErrorf[[noreturn]](const char *format, const Args &... args) {
222  Logger::i().Errorf(format, args...);
223 }
224 
225 template <typename... Args>
226 inline void LogWarningf(const char *format, const Args &... args) {
227  Logger::i().Warningf(format, args...);
228 }
229 
230 template <typename... Args>
231 inline void LogInfof(const char *format, const Args &... args) {
232  Logger::i().Infof(format, args...);
233 }
234 
235 template <typename... Args>
236 inline void LogDebugf(const char *format, const Args &... args) {
237  Logger::i().Debugf(format, args...);
238 }
239 
241 public:
242  ConsoleProgressBar(size_t expected_count,
243  const std::string &progress_info,
244  bool active = false) {
245  reset(expected_count, progress_info, active);
246  }
247 
248  void reset(size_t expected_count,
249  const std::string &progress_info,
250  bool active) {
251  expected_count_ = expected_count;
252  current_count_ = -1;
253  progress_info_ = progress_info;
254  progress_pixel_ = 0;
255  active_ = active;
256  operator++();
257  }
258 
260  current_count_++;
261  if (!active_) {
262  return *this;
263  }
264  if (current_count_ >= expected_count_) {
265  fmt::print("{}[{}] 100%\n", progress_info_,
266  std::string(resolution_, '='));
267  } else {
268  size_t new_progress_pixel =
269  int(current_count_ * resolution_ / expected_count_);
270  if (new_progress_pixel > progress_pixel_) {
271  progress_pixel_ = new_progress_pixel;
272  int percent = int(current_count_ * 100 / expected_count_);
273  fmt::print("{}[{}>{}] {:d}%\r", progress_info_,
274  std::string(progress_pixel_, '='),
275  std::string(resolution_ - 1 - progress_pixel_, ' '),
276  percent);
277  fflush(stdout);
278  }
279  }
280  return *this;
281  }
282 
283 private:
284  const size_t resolution_ = 40;
285  size_t expected_count_;
286  size_t current_count_;
287  std::string progress_info_;
288  size_t progress_pixel_;
289  bool active_;
290 };
291 
292 std::string GetCurrentTimeStamp();
293 
294 std::string GetProgramOptionAsString(int argc,
295  char **argv,
296  const std::string &option,
297  const std::string &default_value = "");
298 
299 int GetProgramOptionAsInt(int argc,
300  char **argv,
301  const std::string &option,
302  const int default_value = 0);
303 
304 double GetProgramOptionAsDouble(int argc,
305  char **argv,
306  const std::string &option,
307  const double default_value = 0.0);
308 
309 Eigen::VectorXd GetProgramOptionAsEigenVectorXd(
310  int argc,
311  char **argv,
312  const std::string &option,
313  const Eigen::VectorXd default_value = Eigen::VectorXd::Zero(0));
314 
315 bool ProgramOptionExists(int argc, char **argv, const std::string &option);
316 
317 bool ProgramOptionExistsAny(int argc,
318  char **argv,
319  const std::vector<std::string> &options);
320 
321 } // namespace utility
322 } // namespace open3d
open3d::utility::Logger::ChangeConsoleColor
void ChangeConsoleColor(TextColor text_color, int highlight_text) const
Definition: Console.cpp:53
open3d::utility::ConsoleProgressBar
Definition: Console.h:240
open3d::utility::Logger::TextColor::Black
@ Black
open3d::utility::ProgramOptionExists
bool ProgramOptionExists(int argc, char **argv, const std::string &option)
Definition: Console.cpp:185
open3d::utility::LogErrorf
void LogErrorf(const char *format, const Args &... args)
Definition: Console.h:221
open3d::utility::Logger::Warning
void Warning(const char *format, const Args &... args) const
Definition: Console.h:125
open3d::utility::Logger::ColorString
std::string ColorString(const std::string &text, TextColor text_color, int highlight_text) const
Colorize and reset the color of a string, does not work on Windows.
Definition: Console.cpp:84
open3d::utility::Logger::Debugf
void Debugf(const char *format, const Args &... args) const
Definition: Console.h:168
open3d::io::k4a_plugin::int
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
open3d::utility::GetVerbosityLevel
VerbosityLevel GetVerbosityLevel()
Definition: Console.h:196
open3d::utility::Logger::verbosity_level_
VerbosityLevel verbosity_level_
Definition: Console.h:189
open3d::utility::LogDebug
void LogDebug(const char *format, const Args &... args)
Definition: Console.h:216
open3d::utility::Logger::i
static Logger & i()
Definition: Console.h:81
open3d::utility::LogWarningf
void LogWarningf(const char *format, const Args &... args)
Definition: Console.h:226
open3d::utility::Logger::VDebug
void VDebug(const char *format, fmt::format_args args) const
Definition: Console.h:111
open3d::utility::VerbosityLevel::Info
@ Info
open3d::utility::GetProgramOptionAsString
std::string GetProgramOptionAsString(int argc, char **argv, const std::string &option, const std::string &default_value)
Definition: Console.cpp:104
open3d::utility::ConsoleProgressBar::operator++
ConsoleProgressBar & operator++()
Definition: Console.h:259
open3d::utility::VerbosityLevel::Warning
@ Warning
open3d::utility::Logger::TextColor::Green
@ Green
open3d::utility::Logger::VError
void VError(const char *format, fmt::format_args args) const
Definition: Console.h:86
open3d::utility::VerbosityLevel::Debug
@ Debug
open3d::utility::Logger::operator=
void operator=(Logger const &)=delete
open3d::utility::Logger::Errorf
void Errorf(const char *format, const Args &... args) const
Definition: Console.h:140
open3d::utility::GetCurrentTimeStamp
std::string GetCurrentTimeStamp()
Definition: Console.cpp:99
open3d::io::k4a_plugin::default_value
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:658
open3d::utility::ProgramOptionExistsAny
bool ProgramOptionExistsAny(int argc, char **argv, const std::vector< std::string > &options)
Definition: Console.cpp:189
open3d::utility::Logger::TextColor::White
@ White
open3d::utility::Logger::Debug
void Debug(const char *format, const Args &... args) const
Definition: Console.h:135
open3d::utility::Logger::ResetConsoleColor
void ResetConsoleColor() const
Definition: Console.cpp:74
open3d::utility::SetVerbosityLevel
void SetVerbosityLevel(VerbosityLevel level)
Definition: Console.h:192
open3d::utility::Logger::Info
void Info(const char *format, const Args &... args) const
Definition: Console.h:130
open3d::utility::Logger::VInfo
void VInfo(const char *format, fmt::format_args args) const
Definition: Console.h:103
open3d::utility::Logger::Logger
Logger()
Definition: Console.h:77
open3d::utility::Logger::Logger
Logger(Logger const &)=delete
open3d::utility::VerbosityLevel::Error
@ Error
open3d::utility::Logger::Infof
void Infof(const char *format, const Args &... args) const
Definition: Console.h:159
open3d::utility::GetProgramOptionAsDouble
double GetProgramOptionAsDouble(int argc, char **argv, const std::string &option, const double default_value)
Definition: Console.cpp:137
open3d::utility::Logger::TextColor::Yellow
@ Yellow
open3d::utility::GetProgramOptionAsEigenVectorXd
Eigen::VectorXd GetProgramOptionAsEigenVectorXd(int argc, char **argv, const std::string &option, const Eigen::VectorXd default_value)
Definition: Console.cpp:156
open3d::utility::LogInfof
void LogInfof(const char *format, const Args &... args)
Definition: Console.h:231
open3d::utility::Logger::TextColor::Cyan
@ Cyan
open3d::io::k4a_plugin::format
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 format
Definition: K4aPlugin.cpp:478
open3d::utility::VerbosityLevel
VerbosityLevel
Definition: Console.h:44
open3d::utility::Logger::Error
void Error(const char *format, const Args &... args) const
Definition: Console.h:120
open3d::utility::Logger::TextColor::Red
@ Red
open3d::utility::LogInfo
void LogInfo(const char *format, const Args &... args)
Definition: Console.h:211
open3d
Definition: PinholeCameraIntrinsic.cpp:34
open3d::utility::LogDebugf
void LogDebugf(const char *format, const Args &... args)
Definition: Console.h:236
open3d::utility::ConsoleProgressBar::reset
void reset(size_t expected_count, const std::string &progress_info, bool active)
Definition: Console.h:248
open3d::utility::Logger::Warningf
void Warningf(const char *format, const Args &... args) const
Definition: Console.h:148
open3d::utility::LogError
void LogError(const char *format, const Args &... args)
Definition: Console.h:201
open3d::utility::ConsoleProgressBar::ConsoleProgressBar
ConsoleProgressBar(size_t expected_count, const std::string &progress_info, bool active=false)
Definition: Console.h:242
open3d::utility::GetProgramOptionAsInt
int GetProgramOptionAsInt(int argc, char **argv, const std::string &option, const int default_value)
Definition: Console.cpp:116
open3d::utility::Logger::TextColor
TextColor
Definition: Console.h:66
open3d::utility::Logger::VWarning
void VWarning(const char *format, fmt::format_args args) const
Definition: Console.h:93
open3d::utility::Logger::TextColor::Magenta
@ Magenta
open3d::utility::Logger::TextColor::Blue
@ Blue
open3d::utility::Logger
Definition: Console.h:64
open3d::utility::LogWarning
void LogWarning(const char *format, const Args &... args)
Definition: Console.h:206