Open3D (C++ API)
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 {
45  Off = 0,
46  Fatal = 1,
47  Error = 2,
48  Warning = 3,
49  Info = 4,
50  Debug = 5,
51 };
52 
53 class Logger {
54 public:
55  enum class TextColor {
56  Black = 0,
57  Red = 1,
58  Green = 2,
59  Yellow = 3,
60  Blue = 4,
61  Magenta = 5,
62  Cyan = 6,
63  White = 7
64  };
65 
66  Logger() : verbosity_level_(VerbosityLevel::Info) {}
67  Logger(Logger const &) = delete;
68  void operator=(Logger const &) = delete;
69 
70  static Logger &i() {
71  static Logger instance;
72  return instance;
73  }
74 
79  void ChangeConsoleColor(TextColor text_color, int highlight_text);
80  void ResetConsoleColor();
81 
82  void VFatal(const char *format, fmt::format_args args) {
83  if (verbosity_level_ >= VerbosityLevel::Fatal) {
84  ChangeConsoleColor(TextColor::Red, 1);
85  fmt::print("[Open3D FATAL] ");
86  fmt::vprint(format, args);
87  ResetConsoleColor();
88  exit(-1);
89  }
90  }
91 
92  void VError(const char *format, fmt::format_args args) {
93  if (verbosity_level_ >= VerbosityLevel::Error) {
94  ChangeConsoleColor(TextColor::Red, 1);
95  fmt::print("[Open3D ERROR] ");
96  fmt::vprint(format, args);
97  ResetConsoleColor();
98  }
99  }
100 
101  void VWarning(const char *format, fmt::format_args args) {
102  if (verbosity_level_ >= VerbosityLevel::Warning) {
103  ChangeConsoleColor(TextColor::Yellow, 1);
104  fmt::print("[Open3D WARNING] ");
105  fmt::vprint(format, args);
106  ResetConsoleColor();
107  }
108  }
109 
110  void VInfo(const char *format, fmt::format_args args) {
111  if (verbosity_level_ >= VerbosityLevel::Info) {
112  fmt::print("[Open3D INFO] ");
113  fmt::vprint(format, args);
114  }
115  }
116 
117  void VDebug(const char *format, fmt::format_args args) {
118  if (verbosity_level_ >= VerbosityLevel::Debug) {
119  fmt::print("[Open3D DEBUG] ");
120  fmt::vprint(format, args);
121  }
122  }
123 
124  template <typename... Args>
125  void Fatal(const char *format, const Args &... args) {
126  VFatal(format, fmt::make_format_args(args...));
127  }
128 
129  template <typename... Args>
130  void Error(const char *format, const Args &... args) {
131  VError(format, fmt::make_format_args(args...));
132  }
133 
134  template <typename... Args>
135  void Warning(const char *format, const Args &... args) {
136  VWarning(format, fmt::make_format_args(args...));
137  }
138 
139  template <typename... Args>
140  void Info(const char *format, const Args &... args) {
141  VInfo(format, fmt::make_format_args(args...));
142  }
143 
144  template <typename... Args>
145  void Debug(const char *format, const Args &... args) {
146  VDebug(format, fmt::make_format_args(args...));
147  }
148 
149  template <typename... Args>
150  void Fatalf(const char *format, const Args &... args) {
151  if (verbosity_level_ >= VerbosityLevel::Fatal) {
152  ChangeConsoleColor(TextColor::Red, 1);
153  fmt::print("[Open3D FATAL] ");
154  fmt::printf(format, args...);
155  ResetConsoleColor();
156  exit(-1);
157  }
158  }
159 
160  template <typename... Args>
161  void Errorf(const char *format, const Args &... args) {
162  if (verbosity_level_ >= VerbosityLevel::Error) {
163  ChangeConsoleColor(TextColor::Red, 1);
164  fmt::print("[Open3D ERROR] ");
165  fmt::printf(format, args...);
166  ResetConsoleColor();
167  }
168  }
169 
170  template <typename... Args>
171  void Warningf(const char *format, const Args &... args) {
172  if (verbosity_level_ >= VerbosityLevel::Warning) {
173  ChangeConsoleColor(TextColor::Yellow, 1);
174  fmt::print("[Open3D WARNING] ");
175  fmt::printf(format, args...);
176  ResetConsoleColor();
177  }
178  }
179 
180  template <typename... Args>
181  void Infof(const char *format, const Args &... args) {
182  if (verbosity_level_ >= VerbosityLevel::Info) {
183  fmt::print("[Open3D INFO] ");
184  fmt::printf(format, args...);
185  }
186  }
187 
188  template <typename... Args>
189  void Debugf(const char *format, const Args &... args) {
190  if (verbosity_level_ >= VerbosityLevel::Debug) {
191  fmt::print("[Open3D DEBUG] ");
192  fmt::printf(format, args...);
193  }
194  }
195 
196 public:
198 };
199 
200 inline void SetVerbosityLevel(VerbosityLevel level) {
201  Logger::i().verbosity_level_ = level;
202 }
203 
205  return Logger::i().verbosity_level_;
206 }
207 
208 template <typename... Args>
209 inline void LogFatal(const char *format, const Args &... args) {
210  Logger::i().VFatal(format, fmt::make_format_args(args...));
211 }
212 
213 template <typename... Args>
214 inline void LogError(const char *format, const Args &... args) {
215  Logger::i().VError(format, fmt::make_format_args(args...));
216 }
217 
218 template <typename... Args>
219 inline void LogWarning(const char *format, const Args &... args) {
220  Logger::i().VWarning(format, fmt::make_format_args(args...));
221 }
222 
223 template <typename... Args>
224 inline void LogInfo(const char *format, const Args &... args) {
225  Logger::i().VInfo(format, fmt::make_format_args(args...));
226 }
227 
228 template <typename... Args>
229 inline void LogDebug(const char *format, const Args &... args) {
230  Logger::i().VDebug(format, fmt::make_format_args(args...));
231 }
232 
233 template <typename... Args>
234 inline void LogFatalf(const char *format, const Args &... args) {
235  Logger::i().Fatalf(format, args...);
236 }
237 
238 template <typename... Args>
239 inline void LogErrorf(const char *format, const Args &... args) {
240  Logger::i().Errorf(format, args...);
241 }
242 
243 template <typename... Args>
244 inline void LogWarningf(const char *format, const Args &... args) {
245  Logger::i().Warningf(format, args...);
246 }
247 
248 template <typename... Args>
249 inline void LogInfof(const char *format, const Args &... args) {
250  Logger::i().Infof(format, args...);
251 }
252 
253 template <typename... Args>
254 inline void LogDebugf(const char *format, const Args &... args) {
255  Logger::i().Debugf(format, args...);
256 }
257 
259 public:
260  ConsoleProgressBar(size_t expected_count,
261  const std::string &progress_info,
262  bool active = false) {
263  reset(expected_count, progress_info, active);
264  }
265 
266  void reset(size_t expected_count,
267  const std::string &progress_info,
268  bool active) {
269  expected_count_ = expected_count;
270  current_count_ = -1;
271  progress_info_ = progress_info;
272  progress_pixel_ = 0;
273  active_ = active;
274  operator++();
275  }
276 
278  current_count_++;
279  if (!active_) {
280  return *this;
281  }
282  if (current_count_ >= expected_count_) {
283  fmt::print("{}[{}] 100%\n", progress_info_,
284  std::string(resolution_, '='));
285  } else {
286  size_t new_progress_pixel =
287  int(current_count_ * resolution_ / expected_count_);
288  if (new_progress_pixel > progress_pixel_) {
289  progress_pixel_ = new_progress_pixel;
290  int percent = int(current_count_ * 100 / expected_count_);
291  fmt::print("{}[{}>{}] {:d}%\r", progress_info_,
292  std::string(progress_pixel_, '='),
293  std::string(resolution_ - 1 - progress_pixel_, ' '),
294  percent);
295  fflush(stdout);
296  }
297  }
298  return *this;
299  }
300 
301 private:
302  const size_t resolution_ = 40;
303  size_t expected_count_;
304  size_t current_count_;
305  std::string progress_info_;
306  size_t progress_pixel_;
307  bool active_;
308 };
309 
310 std::string GetCurrentTimeStamp();
311 
312 std::string GetProgramOptionAsString(int argc,
313  char **argv,
314  const std::string &option,
315  const std::string &default_value = "");
316 
317 int GetProgramOptionAsInt(int argc,
318  char **argv,
319  const std::string &option,
320  const int default_value = 0);
321 
322 double GetProgramOptionAsDouble(int argc,
323  char **argv,
324  const std::string &option,
325  const double default_value = 0.0);
326 
327 Eigen::VectorXd GetProgramOptionAsEigenVectorXd(
328  int argc,
329  char **argv,
330  const std::string &option,
331  const Eigen::VectorXd default_value = Eigen::VectorXd::Zero(0));
332 
333 bool ProgramOptionExists(int argc, char **argv, const std::string &option);
334 
335 bool ProgramOptionExistsAny(int argc,
336  char **argv,
337  const std::vector<std::string> &options);
338 } // namespace utility
339 } // namespace open3d
void Debugf(const char *format, const Args &... args)
Definition: Console.h:189
void Fatal(const char *format, const Args &... args)
Definition: Console.h:125
std::string GetProgramOptionAsString(int argc, char **argv, const std::string &option, const std::string &default_value)
Definition: Console.cpp:87
void VError(const char *format, fmt::format_args args)
Definition: Console.h:92
void LogDebugf(const char *format, const Args &... args)
Definition: Console.h:254
void Errorf(const char *format, const Args &... args)
Definition: Console.h:161
void Warningf(const char *format, const Args &... args)
Definition: Console.h:171
double GetProgramOptionAsDouble(int argc, char **argv, const std::string &option, const double default_value)
Definition: Console.cpp:120
void LogInfof(const char *format, const Args &... args)
Definition: Console.h:249
ConsoleProgressBar & operator++()
Definition: Console.h:277
Logger()
Definition: Console.h:66
void LogError(const char *format, const Args &... args)
Definition: Console.h:214
void Info(const char *format, const Args &... args)
Definition: Console.h:140
void Infof(const char *format, const Args &... args)
Definition: Console.h:181
void VDebug(const char *format, fmt::format_args args)
Definition: Console.h:117
void LogWarning(const char *format, const Args &... args)
Definition: Console.h:219
bool ProgramOptionExistsAny(int argc, char **argv, const std::vector< std::string > &options)
Definition: Console.cpp:172
TextColor
Definition: Console.h:55
void LogFatal(const char *format, const Args &... args)
Definition: Console.h:209
void LogDebug(const char *format, const Args &... args)
Definition: Console.h:229
void VInfo(const char *format, fmt::format_args args)
Definition: Console.h:110
void VFatal(const char *format, fmt::format_args args)
Definition: Console.h:82
std::string GetCurrentTimeStamp()
Definition: Console.cpp:82
VerbosityLevel GetVerbosityLevel()
Definition: Console.h:204
void Warning(const char *format, const Args &... args)
Definition: Console.h:135
void Error(const char *format, const Args &... args)
Definition: Console.h:130
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
void LogErrorf(const char *format, const Args &... args)
Definition: Console.h:239
int GetProgramOptionAsInt(int argc, char **argv, const std::string &option, const int default_value)
Definition: Console.cpp:99
VerbosityLevel
Definition: Console.h:44
void LogFatalf(const char *format, const Args &... args)
Definition: Console.h:234
void SetVerbosityLevel(VerbosityLevel level)
Definition: Console.h:200
Definition: PinholeCameraIntrinsic.cpp:34
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:475
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
ConsoleProgressBar(size_t expected_count, const std::string &progress_info, bool active=false)
Definition: Console.h:260
static Logger & i()
Definition: Console.h:70
void Fatalf(const char *format, const Args &... args)
Definition: Console.h:150
void reset(size_t expected_count, const std::string &progress_info, bool active)
Definition: Console.h:266
void LogWarningf(const char *format, const Args &... args)
Definition: Console.h:244
Eigen::VectorXd GetProgramOptionAsEigenVectorXd(int argc, char **argv, const std::string &option, const Eigen::VectorXd default_value)
Definition: Console.cpp:139
Definition: Console.h:258
VerbosityLevel verbosity_level_
Definition: Console.h:197
Definition: Console.h:53
bool ProgramOptionExists(int argc, char **argv, const std::string &option)
Definition: Console.cpp:168
void LogInfo(const char *format, const Args &... args)
Definition: Console.h:224
void Debug(const char *format, const Args &... args)
Definition: Console.h:145
void VWarning(const char *format, fmt::format_args args)
Definition: Console.h:101