Open3D (C++ API)  0.15.1
Logging.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-2021 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 <functional>
30 #include <string>
31 
32 #ifndef FMT_HEADER_ONLY
33 #define FMT_HEADER_ONLY 1
34 #endif
35 #ifndef FMT_STRING_ALIAS
36 #define FMT_STRING_ALIAS 1
37 #endif
38 // NVCC does not support deprecated attribute on Windows prior to v11.
39 #if defined(__CUDACC__) && defined(_MSC_VER) && __CUDACC_VER_MAJOR__ < 11
40 #ifndef FMT_DEPRECATED
41 #define FMT_DEPRECATED
42 #endif
43 #endif
44 #include <fmt/format.h>
45 #include <fmt/ostream.h>
46 #include <fmt/printf.h>
47 #include <fmt/ranges.h>
48 
49 #define DEFAULT_IO_BUFFER_SIZE 1024
50 
51 #include "open3d/Macro.h"
52 
53 // Mimic "macro in namespace" by concatenating `utility::` and a macro.
54 // Ref: https://stackoverflow.com/a/11791202
55 //
56 // We avoid using (format, ...) since in this case __VA_ARGS__ can be
57 // empty, and the behavior of pruning trailing comma with ##__VA_ARGS__ is not
58 // officially standard.
59 // Ref: https://stackoverflow.com/a/28074198
60 //
61 // __PRETTY_FUNCTION__ has to be converted, otherwise a bug regarding [noreturn]
62 // will be triggered.
63 // Ref: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94742
64 
65 // LogError throws now a runtime_error with the given error message. This
66 // should be used if there is no point in continuing the given algorithm at
67 // some point and the error is not returned in another way (e.g., via a
68 // bool/int as return value).
69 //
70 // Usage : utility::LogError(format_string, arg0, arg1, ...);
71 // Example: utility::LogError("name: {}, age: {}", "dog", 5);
72 #define LogError(...) \
73  Logger::LogError_(__FILE__, __LINE__, \
74  static_cast<const char *>(OPEN3D_FUNCTION), __VA_ARGS__)
75 
76 // LogWarning is used if an error occurs, but the error is also signaled
77 // via a return value (i.e., there is no need to throw an exception). This
78 // warning should further be used, if the algorithms encounters a state
79 // that does not break its continuation, but the output is likely not to be
80 // what the user expected.
81 //
82 // Usage : utility::LogWarning(format_string, arg0, arg1, ...);
83 // Example: utility::LogWarning("name: {}, age: {}", "dog", 5);
84 #define LogWarning(...) \
85  Logger::LogWarning_(__FILE__, __LINE__, \
86  static_cast<const char *>(OPEN3D_FUNCTION), \
87  __VA_ARGS__)
88 
89 // LogInfo is used to inform the user with expected output, e.g, pressed a
90 // key in the visualizer prints helping information.
91 //
92 // Usage : utility::LogInfo(format_string, arg0, arg1, ...);
93 // Example: utility::LogInfo("name: {}, age: {}", "dog", 5);
94 #define LogInfo(...) \
95  Logger::LogInfo_(__FILE__, __LINE__, \
96  static_cast<const char *>(OPEN3D_FUNCTION), __VA_ARGS__)
97 
98 // LogDebug is used to print debug/additional information on the state of
99 // the algorithm.
100 //
101 // Usage : utility::LogDebug(format_string, arg0, arg1, ...);
102 // Example: utility::LogDebug("name: {}, age: {}", "dog", 5);
103 #define LogDebug(...) \
104  Logger::LogDebug_(__FILE__, __LINE__, \
105  static_cast<const char *>(OPEN3D_FUNCTION), __VA_ARGS__)
106 
107 namespace open3d {
108 namespace utility {
109 
110 enum class VerbosityLevel {
115  Error = 0,
121  Warning = 1,
124  Info = 2,
127  Debug = 3,
128 };
129 
131 class Logger {
132 public:
133  Logger(Logger const &) = delete;
134  void operator=(Logger const &) = delete;
135 
137  static Logger &GetInstance();
138 
145  void SetPrintFunction(std::function<void(const std::string &)> print_fcn);
146 
148  void ResetPrintFunction();
149 
151  const std::function<void(const std::string &)> GetPrintFunction();
152 
157  void SetVerbosityLevel(VerbosityLevel verbosity_level);
158 
161 
162  template <typename... Args>
163  static void LogError_ [[noreturn]] (const char *file,
164  int line,
165  const char *function,
166  const char *format,
167  Args &&... args) {
168  if (sizeof...(Args) > 0) {
169  Logger::GetInstance().VError(
170  file, line, function,
171  FormatArgs(format, fmt::make_format_args(args...)));
172  } else {
173  Logger::GetInstance().VError(file, line, function,
174  std::string(format));
175  }
176  }
177  template <typename... Args>
178  static void LogWarning_(const char *file,
179  int line,
180  const char *function,
181  const char *format,
182  Args &&... args) {
185  if (sizeof...(Args) > 0) {
186  Logger::GetInstance().VWarning(
187  file, line, function,
188  FormatArgs(format, fmt::make_format_args(args...)));
189  } else {
190  Logger::GetInstance().VWarning(file, line, function,
191  std::string(format));
192  }
193  }
194  }
195  template <typename... Args>
196  static void LogInfo_(const char *file,
197  int line,
198  const char *function,
199  const char *format,
200  Args &&... args) {
202  if (sizeof...(Args) > 0) {
203  Logger::GetInstance().VInfo(
204  file, line, function,
205  FormatArgs(format, fmt::make_format_args(args...)));
206  } else {
207  Logger::GetInstance().VInfo(file, line, function,
208  std::string(format));
209  }
210  }
211  }
212  template <typename... Args>
213  static void LogDebug_(const char *file,
214  int line,
215  const char *function,
216  const char *format,
217  Args &&... args) {
220  if (sizeof...(Args) > 0) {
221  Logger::GetInstance().VDebug(
222  file, line, function,
223  FormatArgs(format, fmt::make_format_args(args...)));
224  } else {
225  Logger::GetInstance().VDebug(file, line, function,
226  std::string(format));
227  }
228  }
229  }
230 
231 private:
232  Logger();
233  static std::string FormatArgs(const char *format, fmt::format_args args) {
234  std::string err_msg = fmt::vformat(format, args);
235  return err_msg;
236  }
237  void VError [[noreturn]] (const char *file,
238  int line,
239  const char *function,
240  const std::string &message) const;
241  void VWarning(const char *file,
242  int line,
243  const char *function,
244  const std::string &message) const;
245  void VInfo(const char *file,
246  int line,
247  const char *function,
248  const std::string &message) const;
249  void VDebug(const char *file,
250  int line,
251  const char *function,
252  const std::string &message) const;
253 
254 private:
255  struct Impl;
256  std::unique_ptr<Impl> impl_;
257 };
258 
264 
267 
269 public:
270  VerbosityContextManager(VerbosityLevel level) : level_(level) {}
271 
272  void Enter() {
273  level_backup_ = Logger::GetInstance().GetVerbosityLevel();
275  }
276 
277  void Exit() { Logger::GetInstance().SetVerbosityLevel(level_backup_); }
278 
279 private:
280  VerbosityLevel level_;
281  VerbosityLevel level_backup_;
282 };
283 
284 } // namespace utility
285 } // namespace open3d
filament::Texture::InternalFormat format
Definition: FilamentResourceManager.cpp:214
Logger class should be used as a global singleton object (GetInstance()).
Definition: Logging.h:131
Logger(Logger const &)=delete
void operator=(Logger const &)=delete
const std::function< void(const std::string &)> GetPrintFunction()
Get the print function used by the Logger.
Definition: Logging.cpp:131
VerbosityLevel GetVerbosityLevel() const
Get global verbosity level of Open3D.
Definition: Logging.cpp:143
void ResetPrintFunction()
Reset the print function to the default one (print to console).
Definition: Logging.cpp:135
void SetPrintFunction(std::function< void(const std::string &)> print_fcn)
Definition: Logging.cpp:126
static void LogError_(const char *file, int line, const char *function, const char *format, Args &&... args)
Definition: Logging.h:163
static void LogWarning_(const char *file, int line, const char *function, const char *format, Args &&... args)
Definition: Logging.h:178
static void LogDebug_(const char *file, int line, const char *function, const char *format, Args &&... args)
Definition: Logging.h:213
static void LogInfo_(const char *file, int line, const char *function, const char *format, Args &&... args)
Definition: Logging.h:196
void SetVerbosityLevel(VerbosityLevel verbosity_level)
Definition: Logging.cpp:139
static Logger & GetInstance()
Get Logger global singleton instance.
Definition: Logging.cpp:83
void Exit()
Definition: Logging.h:277
VerbosityContextManager(VerbosityLevel level)
Definition: Logging.h:270
void Enter()
Definition: Logging.h:272
const char const char value recording_handle imu_sample void
Definition: K4aPlugin.cpp:269
void SetVerbosityLevel(VerbosityLevel level)
Definition: Logging.cpp:147
VerbosityLevel
Definition: Logging.h:110
VerbosityLevel GetVerbosityLevel()
Get global verbosity level of Open3D.
Definition: Logging.cpp:151
Definition: PinholeCameraIntrinsic.cpp:35