Open3D (C++ API)  0.13.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 // NVCC does not support deprecated attribute on Windows prior to v11.
41 #if defined(__CUDACC__) && defined(_MSC_VER) && __CUDACC_VER_MAJOR__ < 11
42 #ifndef FMT_DEPRECATED
43 #define FMT_DEPRECATED
44 #endif
45 #endif
46 #include <fmt/format.h>
47 #include <fmt/printf.h>
48 #include <fmt/ranges.h>
49 
50 #define DEFAULT_IO_BUFFER_SIZE 1024
51 
52 // Compiler-specific function macro.
53 // Ref: https://stackoverflow.com/a/4384825
54 #ifdef _WIN32
55 #define __FN__ __FUNCSIG__
56 #else
57 #define __FN__ __PRETTY_FUNCTION__
58 #endif
59 
60 // Mimic "macro in namespace" by concatenating `utility::` and a macro.
61 // Ref: https://stackoverflow.com/a/11791202
62 //
63 // We avoid using (format, ...) since in this case __VA_ARGS__ can be
64 // empty, and the behavior of pruning trailing comma with ##__VA_ARGS__ is not
65 // officially standard.
66 // Ref: https://stackoverflow.com/a/28074198
67 //
68 // __PRETTY_FUNCTION__ has to be converted, otherwise a bug regarding [noreturn]
69 // will be triggered.
70 // Ref: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94742
71 
72 // LogError throws now a runtime_error with the given error message. This
73 // should be used if there is no point in continuing the given algorithm at
74 // some point and the error is not returned in another way (e.g., via a
75 // bool/int as return value).
76 //
77 // Usage : utility::LogError(format_string, arg0, arg1, ...);
78 // Example: utility::LogError("name: {}, age: {}", "dog", 5);
79 #define LogError(...) \
80  Logger::_LogError(__FILE__, __LINE__, (const char *)__FN__, false, \
81  __VA_ARGS__)
82 // Same as LogError but enforce printing the message in the console.
83 #define LogErrorConsole(...) \
84  Logger::_LogError(__FILE__, __LINE__, (const char *)__FN__, true, \
85  __VA_ARGS__)
86 
87 // LogWarning is used if an error occurs, but the error is also signaled
88 // via a return value (i.e., there is no need to throw an exception). This
89 // warning should further be used, if the algorithms encounters a state
90 // that does not break its continuation, but the output is likely not to be
91 // what the user expected.
92 //
93 // Usage : utility::LogWarning(format_string, arg0, arg1, ...);
94 // Example: utility::LogWarning("name: {}, age: {}", "dog", 5);
95 #define LogWarning(...) \
96  Logger::_LogWarning(__FILE__, __LINE__, (const char *)__FN__, false, \
97  __VA_ARGS__)
98 // Same as LogWarning but enforce printing the message in the console.
99 #define LogWarningConsole(...) \
100  Logger::_LogWarning(__FILE__, __LINE__, (const char *)__FN__, true, \
101  __VA_ARGS__)
102 
103 // LogInfo is used to inform the user with expected output, e.g, pressed a
104 // key in the visualizer prints helping information.
105 //
106 // Usage : utility::LogInfo(format_string, arg0, arg1, ...);
107 // Example: utility::LogInfo("name: {}, age: {}", "dog", 5);
108 #define LogInfo(...) \
109  Logger::_LogInfo(__FILE__, __LINE__, (const char *)__FN__, false, \
110  __VA_ARGS__)
111 // Same as LogInfo but enforce printing the message in the console.
112 #define LogInfoConsole(...) \
113  Logger::_LogInfo(__FILE__, __LINE__, (const char *)__FN__, true, \
114  __VA_ARGS__)
115 
116 // LogDebug is used to print debug/additional information on the state of
117 // the algorithm.
118 //
119 // Usage : utility::LogDebug(format_string, arg0, arg1, ...);
120 // Example: utility::LogDebug("name: {}, age: {}", "dog", 5);
121 #define LogDebug(...) \
122  Logger::_LogDebug(__FILE__, __LINE__, (const char *)__FN__, false, \
123  __VA_ARGS__)
124 // Same as LogDebug but enforce printing the message in the console.
125 #define LogDebugConsole(...) \
126  Logger::_LogDebug(__FILE__, __LINE__, (const char *)__FN__, true, \
127  __VA_ARGS__)
128 
129 namespace open3d {
130 namespace utility {
131 
132 enum class VerbosityLevel {
137  Error = 0,
143  Warning = 1,
146  Info = 2,
149  Debug = 3,
150 };
151 
153 class Logger {
154 public:
155  Logger(Logger const &) = delete;
156  void operator=(Logger const &) = delete;
157 
159  static Logger &GetInstance();
160 
167  void SetPrintFunction(std::function<void(const std::string &)> print_fcn);
168 
170  void ResetPrintFunction();
171 
176  void SetVerbosityLevel(VerbosityLevel verbosity_level);
177 
180 
181  template <typename... Args>
182  static void _LogError [[noreturn]] (const char *file_name,
183  int line_number,
184  const char *function_name,
185  bool force_console_log,
186  const char *format,
187  Args &&... args) {
188  Logger::GetInstance().VError(file_name, line_number, function_name,
189  force_console_log, format,
190  fmt::make_format_args(args...));
191  }
192  template <typename... Args>
193  static void _LogWarning(const char *file_name,
194  int line_number,
195  const char *function_name,
196  bool force_console_log,
197  const char *format,
198  Args &&... args) {
199  Logger::GetInstance().VWarning(file_name, line_number, function_name,
200  force_console_log, format,
201  fmt::make_format_args(args...));
202  }
203  template <typename... Args>
204  static void _LogInfo(const char *file_name,
205  int line_number,
206  const char *function_name,
207  bool force_console_log,
208  const char *format,
209  Args &&... args) {
210  Logger::GetInstance().VInfo(file_name, line_number, function_name,
211  force_console_log, format,
212  fmt::make_format_args(args...));
213  }
214  template <typename... Args>
215  static void _LogDebug(const char *file_name,
216  int line_number,
217  const char *function_name,
218  bool force_console_log,
219  const char *format,
220  Args &&... args) {
221  Logger::GetInstance().VDebug(file_name, line_number, function_name,
222  force_console_log, format,
223  fmt::make_format_args(args...));
224  }
225 
226 private:
227  Logger();
228  void VError [[noreturn]] (const char *file_name,
229  int line_number,
230  const char *function_name,
231  bool force_console_log,
232  const char *format,
233  fmt::format_args args) const;
234  void VWarning(const char *file_name,
235  int line_number,
236  const char *function_name,
237  bool force_console_log,
238  const char *format,
239  fmt::format_args args) const;
240  void VInfo(const char *file_name,
241  int line_number,
242  const char *function_name,
243  bool force_console_log,
244  const char *format,
245  fmt::format_args args) const;
246  void VDebug(const char *file_name,
247  int line_number,
248  const char *function_name,
249  bool force_console_log,
250  const char *format,
251  fmt::format_args args) const;
252 
253 private:
254  struct Impl;
255  std::unique_ptr<Impl> impl_;
256 };
257 
263 
266 
268 public:
269  VerbosityContextManager(VerbosityLevel level) : level_(level) {}
270 
271  void Enter() {
272  level_backup_ = Logger::GetInstance().GetVerbosityLevel();
274  }
275 
276  void Exit() { Logger::GetInstance().SetVerbosityLevel(level_backup_); }
277 
278 private:
279  VerbosityLevel level_;
280  VerbosityLevel level_backup_;
281 };
282 
284 public:
285  ConsoleProgressBar(size_t expected_count,
286  const std::string &progress_info,
287  bool active = false);
288 
289  void Reset(size_t expected_count,
290  const std::string &progress_info,
291  bool active);
292 
293  ConsoleProgressBar &operator++();
294 
295  void SetCurrentCount(size_t n);
296 
297 private:
298  const size_t resolution_ = 40;
299  size_t expected_count_;
300  size_t current_count_;
301  std::string progress_info_;
302  size_t progress_pixel_;
303  bool active_;
304 };
305 
306 std::string GetCurrentTimeStamp();
307 
308 std::string GetProgramOptionAsString(int argc,
309  char **argv,
310  const std::string &option,
311  const std::string &default_value = "");
312 
313 int GetProgramOptionAsInt(int argc,
314  char **argv,
315  const std::string &option,
316  const int default_value = 0);
317 
318 double GetProgramOptionAsDouble(int argc,
319  char **argv,
320  const std::string &option,
321  const double default_value = 0.0);
322 
323 Eigen::VectorXd GetProgramOptionAsEigenVectorXd(
324  int argc,
325  char **argv,
326  const std::string &option,
327  const Eigen::VectorXd default_value = Eigen::VectorXd::Zero(0));
328 
329 bool ProgramOptionExists(int argc, char **argv, const std::string &option);
330 
331 bool ProgramOptionExistsAny(int argc,
332  char **argv,
333  const std::vector<std::string> &options);
334 
335 } // namespace utility
336 } // namespace open3d
std::string GetProgramOptionAsString(int argc, char **argv, const std::string &option, const std::string &default_value)
Definition: Console.cpp:246
void SetVerbosityLevel(VerbosityLevel verbosity_level)
Definition: Console.cpp:180
static Logger & GetInstance()
Get Logger global singleton instance.
Definition: Console.cpp:98
VerbosityContextManager(VerbosityLevel level)
Definition: Console.h:269
double GetProgramOptionAsDouble(int argc, char **argv, const std::string &option, const double default_value)
Definition: Console.cpp:279
Definition: Console.cpp:63
bool ProgramOptionExistsAny(int argc, char **argv, const std::vector< std::string > &options)
Definition: Console.cpp:331
static void _LogInfo(const char *file_name, int line_number, const char *function_name, bool force_console_log, const char *format, Args &&... args)
Definition: Console.h:204
void Enter()
Definition: Console.h:271
static void _LogDebug(const char *file_name, int line_number, const char *function_name, bool force_console_log, const char *format, Args &&... args)
Definition: Console.h:215
std::string GetCurrentTimeStamp()
Definition: Console.cpp:241
VerbosityLevel GetVerbosityLevel()
Get global verbosity level of Open3D.
Definition: Console.cpp:237
static void _LogWarning(const char *file_name, int line_number, const char *function_name, bool force_console_log, const char *format, Args &&... args)
Definition: Console.h:193
int GetProgramOptionAsInt(int argc, char **argv, const std::string &option, const int default_value)
Definition: Console.cpp:258
VerbosityLevel
Definition: Console.h:132
void SetVerbosityLevel(VerbosityLevel level)
Definition: Console.cpp:233
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
filament::Texture::InternalFormat format
Definition: FilamentResourceManager.cpp:199
void Exit()
Definition: Console.h:276
Eigen::VectorXd GetProgramOptionAsEigenVectorXd(int argc, char **argv, const std::string &option, const Eigen::VectorXd default_value)
Definition: Console.cpp:298
Definition: Console.h:283
Logger class should be used as a global singleton object (GetInstance()).
Definition: Console.h:153
bool ProgramOptionExists(int argc, char **argv, const std::string &option)
Definition: Console.cpp:327
VerbosityLevel GetVerbosityLevel() const
Get global verbosity level of Open3D.
Definition: Console.cpp:184