Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.14.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
154  void SetVerbosityLevel(VerbosityLevel verbosity_level);
155 
158 
159  template <typename... Args>
160  static void LogError_ [[noreturn]] (const char *file,
161  int line,
162  const char *function,
163  const char *format,
164  Args &&... args) {
165  if (sizeof...(Args) > 0) {
166  Logger::GetInstance().VError(
167  file, line, function,
168  FormatArgs(format, fmt::make_format_args(args...)));
169  } else {
170  Logger::GetInstance().VError(file, line, function,
171  std::string(format));
172  }
173  }
174  template <typename... Args>
175  static void LogWarning_(const char *file,
176  int line,
177  const char *function,
178  const char *format,
179  Args &&... args) {
180  if (sizeof...(Args) > 0) {
181  Logger::GetInstance().VWarning(
182  file, line, function,
183  FormatArgs(format, fmt::make_format_args(args...)));
184  } else {
185  Logger::GetInstance().VWarning(file, line, function,
186  std::string(format));
187  }
188  }
189  template <typename... Args>
190  static void LogInfo_(const char *file,
191  int line,
192  const char *function,
193  const char *format,
194  Args &&... args) {
195  if (sizeof...(Args) > 0) {
196  Logger::GetInstance().VInfo(
197  file, line, function,
198  FormatArgs(format, fmt::make_format_args(args...)));
199  } else {
200  Logger::GetInstance().VInfo(file, line, function,
201  std::string(format));
202  }
203  }
204  template <typename... Args>
205  static void LogDebug_(const char *file,
206  int line,
207  const char *function,
208  const char *format,
209  Args &&... args) {
210  if (sizeof...(Args) > 0) {
211  Logger::GetInstance().VDebug(
212  file, line, function,
213  FormatArgs(format, fmt::make_format_args(args...)));
214  } else {
215  Logger::GetInstance().VDebug(file, line, function,
216  std::string(format));
217  }
218  }
219 
220 private:
221  Logger();
222  static std::string FormatArgs(const char *format, fmt::format_args args) {
223  std::string err_msg = fmt::vformat(format, args);
224  return err_msg;
225  }
226  void VError [[noreturn]] (const char *file,
227  int line,
228  const char *function,
229  const std::string &message) const;
230  void VWarning(const char *file,
231  int line,
232  const char *function,
233  const std::string &message) const;
234  void VInfo(const char *file,
235  int line,
236  const char *function,
237  const std::string &message) const;
238  void VDebug(const char *file,
239  int line,
240  const char *function,
241  const std::string &message) const;
242 
243 private:
244  struct Impl;
245  std::unique_ptr<Impl> impl_;
246 };
247 
253 
256 
258 public:
259  VerbosityContextManager(VerbosityLevel level) : level_(level) {}
260 
261  void Enter() {
262  level_backup_ = Logger::GetInstance().GetVerbosityLevel();
264  }
265 
266  void Exit() { Logger::GetInstance().SetVerbosityLevel(level_backup_); }
267 
268 private:
269  VerbosityLevel level_;
270  VerbosityLevel level_backup_;
271 };
272 
273 } // namespace utility
274 } // namespace open3d
void SetVerbosityLevel(VerbosityLevel verbosity_level)
Definition: Logging.cpp:141
static Logger & GetInstance()
Get Logger global singleton instance.
Definition: Logging.cpp:83
VerbosityContextManager(VerbosityLevel level)
Definition: Logging.h:259
Definition: Logging.cpp:48
static void LogWarning_(const char *file, int line, const char *function, const char *format, Args &&... args)
Definition: Logging.h:175
void Enter()
Definition: Logging.h:261
static void LogDebug_(const char *file, int line, const char *function, const char *format, Args &&... args)
Definition: Logging.h:205
VerbosityLevel GetVerbosityLevel()
Get global verbosity level of Open3D.
Definition: Logging.cpp:153
static void LogInfo_(const char *file, int line, const char *function, const char *format, Args &&... args)
Definition: Logging.h:190
VerbosityLevel
Definition: Logging.h:110
void SetVerbosityLevel(VerbosityLevel level)
Definition: Logging.cpp:149
Definition: PinholeCameraIntrinsic.cpp:35
filament::Texture::InternalFormat format
Definition: FilamentResourceManager.cpp:214
void Exit()
Definition: Logging.h:266
Logger class should be used as a global singleton object (GetInstance()).
Definition: Logging.h:131
VerbosityLevel GetVerbosityLevel() const
Get global verbosity level of Open3D.
Definition: Logging.cpp:145