VTK  9.3.0
vtkLogger.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
141 #ifndef vtkLogger_h
142 #define vtkLogger_h
143 
144 #include "vtkObjectBase.h"
145 #include "vtkSetGet.h" // needed for macros
146 
147 #include <string> // needed for std::string
148 
149 #if defined(_MSC_VER)
150 #include <sal.h> // Needed for _In_z_ etc annotations
151 #endif
152 
153 // this is copied from `loguru.hpp`
154 #if defined(__clang__) || defined(__GNUC__)
155 // Helper macro for declaring functions as having similar signature to printf.
156 // This allows the compiler to catch format errors at compile-time.
157 #define VTK_PRINTF_LIKE(fmtarg, firstvararg) \
158  __attribute__((__format__(__printf__, fmtarg, firstvararg)))
159 #define VTK_FORMAT_STRING_TYPE const char*
160 #elif defined(_MSC_VER)
161 #define VTK_PRINTF_LIKE(fmtarg, firstvararg)
162 #define VTK_FORMAT_STRING_TYPE _In_z_ _Printf_format_string_ const char*
163 #else
164 #define VTK_PRINTF_LIKE(fmtarg, firstvararg)
165 #define VTK_FORMAT_STRING_TYPE const char*
166 #endif
167 
168 VTK_ABI_NAMESPACE_BEGIN
169 class VTKCOMMONCORE_EXPORT vtkLogger : public vtkObjectBase
170 {
171 public:
173  void PrintSelf(ostream& os, vtkIndent indent) override;
174 
176  {
177  // Used to mark an invalid verbosity. Do not log to this level.
178  VERBOSITY_INVALID = -10, // Never do LOG_F(INVALID)
179 
180  // You may use VERBOSITY_OFF on g_stderr_verbosity, but for nothing else!
181  VERBOSITY_OFF = -9, // Never do LOG_F(OFF)
182 
183  VERBOSITY_ERROR = -2,
184  VERBOSITY_WARNING = -1,
185 
186  // Normal messages. By default written to stderr.
187  VERBOSITY_INFO = 0,
188 
189  // Same as VERBOSITY_INFO in every way.
190  VERBOSITY_0 = 0,
191 
192  // Verbosity levels 1-9 are generally not written to stderr, but are written to file.
193  VERBOSITY_1 = +1,
194  VERBOSITY_2 = +2,
195  VERBOSITY_3 = +3,
196  VERBOSITY_4 = +4,
197  VERBOSITY_5 = +5,
198  VERBOSITY_6 = +6,
199  VERBOSITY_7 = +7,
200  VERBOSITY_8 = +8,
201  VERBOSITY_9 = +9,
202 
203  // trace level, same as VERBOSITY_9
204  VERBOSITY_TRACE = +9,
205 
206  // Don not use higher verbosity levels, as that will make grepping log files harder.
207  VERBOSITY_MAX = +9,
208  };
209 
247  static void Init(int& argc, char* argv[], const char* verbosity_flag = "-v");
248  static void Init();
258 
266 
271  enum FileMode
272  {
274  APPEND
275  };
276 
283  static void LogToFile(const char* path, FileMode filemode, Verbosity verbosity);
284 
288  static void EndLogToFile(const char* path);
289 
291 
294  static void SetThreadName(const std::string& name);
297 
302 
307  struct Message
308  {
309  // You would generally print a Message by just concatenating the buffers without spacing.
310  // Optionally, ignore preamble and indentation.
311  Verbosity verbosity; // Already part of preamble
312  const char* filename; // Already part of preamble
313  unsigned line; // Already part of preamble
314  const char* preamble; // Date, time, uptime, thread, file:line, verbosity.
315  const char* indentation; // Just a bunch of spacing.
316  const char* prefix; // Assertion failure info goes here (or "").
317  const char* message; // User message goes here.
318  };
319 
321 
324  using LogHandlerCallbackT = void (*)(void* user_data, const Message& message);
325  using CloseHandlerCallbackT = void (*)(void* user_data);
326  using FlushHandlerCallbackT = void (*)(void* user_data);
328 
338  static void AddCallback(const char* id, LogHandlerCallbackT callback, void* user_data,
339  Verbosity verbosity, CloseHandlerCallbackT on_close = nullptr,
340  FlushHandlerCallbackT on_flush = nullptr);
341 
346  static bool RemoveCallback(const char* id);
347 
351  static bool IsEnabled();
352 
359 
367 
374  static Verbosity ConvertToVerbosity(const char* text);
375 
377 
382  static void Log(
383  Verbosity verbosity, VTK_FILEPATH const char* fname, unsigned int lineno, const char* txt);
384  static void StartScope(
385  Verbosity verbosity, const char* id, VTK_FILEPATH const char* fname, unsigned int lineno);
386  static void EndScope(const char* id);
387 #if !defined(__WRAP__)
388  static void LogF(Verbosity verbosity, VTK_FILEPATH const char* fname, unsigned int lineno,
389  VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(4, 5);
390  static void StartScopeF(Verbosity verbosity, const char* id, VTK_FILEPATH const char* fname,
391  unsigned int lineno, VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(5, 6);
392 
393  class VTKCOMMONCORE_EXPORT LogScopeRAII
394  {
395  public:
397  LogScopeRAII(vtkLogger::Verbosity verbosity, const char* fname, unsigned int lineno,
398  VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(5, 6);
400 #if defined(_MSC_VER) && _MSC_VER > 1800
401  // see loguru.hpp for the reason why this is needed on MSVC
402  LogScopeRAII(LogScopeRAII&& other)
403  : Internals(other.Internals)
404  {
405  other.Internals = nullptr;
406  }
407 #else
409 #endif
410 
411  private:
412  LogScopeRAII(const LogScopeRAII&) = delete;
413  void operator=(const LogScopeRAII&) = delete;
414  class LSInternals;
415  LSInternals* Internals;
416  };
417 #endif
419 
426 
427 protected:
429  ~vtkLogger() override;
430 
431 private:
432  vtkLogger(const vtkLogger&) = delete;
433  void operator=(const vtkLogger&) = delete;
434  static vtkLogger::Verbosity InternalVerbosityLevel;
435 };
436 
438 
452 #define vtkVLogF(level, ...) \
453  ((level) > vtkLogger::GetCurrentVerbosityCutoff()) \
454  ? (void)0 \
455  : vtkLogger::LogF(level, __FILE__, __LINE__, __VA_ARGS__)
456 #define vtkLogF(verbosity_name, ...) vtkVLogF(vtkLogger::VERBOSITY_##verbosity_name, __VA_ARGS__)
457 #define vtkVLog(level, x) \
458  if ((level) <= vtkLogger::GetCurrentVerbosityCutoff()) \
459  { \
460  vtkOStrStreamWrapper::EndlType const endl; \
461  vtkOStrStreamWrapper::UseEndl(endl); \
462  vtkOStrStreamWrapper vtkmsg; \
463  vtkmsg << "" x; \
464  vtkLogger::Log(level, __FILE__, __LINE__, vtkmsg.str()); \
465  vtkmsg.rdbuf()->freeze(0); \
466  }
467 #define vtkLog(verbosity_name, x) vtkVLog(vtkLogger::VERBOSITY_##verbosity_name, x)
469 
471 
483 #define vtkVLogIfF(level, cond, ...) \
484  ((level) > vtkLogger::GetCurrentVerbosityCutoff() || (cond) == false) \
485  ? (void)0 \
486  : vtkLogger::LogF(level, __FILE__, __LINE__, __VA_ARGS__)
487 
488 #define vtkLogIfF(verbosity_name, cond, ...) \
489  vtkVLogIfF(vtkLogger::VERBOSITY_##verbosity_name, cond, __VA_ARGS__)
490 
491 #define vtkVLogIf(level, cond, x) \
492  if ((level) <= vtkLogger::GetCurrentVerbosityCutoff() && (cond)) \
493  { \
494  vtkOStrStreamWrapper::EndlType endl; \
495  vtkOStrStreamWrapper::UseEndl(endl); \
496  vtkOStrStreamWrapper vtkmsg; \
497  vtkmsg << "" x; \
498  vtkLogger::Log(level, __FILE__, __LINE__, vtkmsg.str()); \
499  vtkmsg.rdbuf()->freeze(0); \
500  }
501 #define vtkLogIf(verbosity_name, cond, x) vtkVLogIf(vtkLogger::VERBOSITY_##verbosity_name, cond, x)
503 
504 #define VTKLOG_CONCAT_IMPL(s1, s2) s1##s2
505 #define VTKLOG_CONCAT(s1, s2) VTKLOG_CONCAT_IMPL(s1, s2)
506 #define VTKLOG_ANONYMOUS_VARIABLE(x) VTKLOG_CONCAT(x, __LINE__)
507 
508 #define vtkVLogScopeF(level, ...) \
509  auto VTKLOG_ANONYMOUS_VARIABLE(msg_context) = ((level) > vtkLogger::GetCurrentVerbosityCutoff()) \
510  ? vtkLogger::LogScopeRAII() \
511  : vtkLogger::LogScopeRAII(level, __FILE__, __LINE__, __VA_ARGS__)
512 
513 #define vtkLogScopeF(verbosity_name, ...) \
514  vtkVLogScopeF(vtkLogger::VERBOSITY_##verbosity_name, __VA_ARGS__)
515 
516 #define vtkLogScopeFunction(verbosity_name) vtkLogScopeF(verbosity_name, "%s", __func__)
517 #define vtkVLogScopeFunction(level) vtkVLogScopeF(level, "%s", __func__)
518 
520 
524 #define vtkLogStartScope(verbosity_name, id) \
525  vtkLogger::StartScope(vtkLogger::VERBOSITY_##verbosity_name, id, __FILE__, __LINE__)
526 #define vtkLogEndScope(id) vtkLogger::EndScope(id)
527 
528 #define vtkLogStartScopeF(verbosity_name, id, ...) \
529  vtkLogger::StartScopeF(vtkLogger::VERBOSITY_##verbosity_name, id, __FILE__, __LINE__, __VA_ARGS__)
530 
531 #define vtkVLogStartScope(level, id) vtkLogger::StartScope(level, id, __FILE__, __LINE__)
532 #define vtkVLogStartScopeF(level, id, ...) \
533  vtkLogger::StartScopeF(level, id, __FILE__, __LINE__, __VA_ARGS__)
535 
541 #define vtkLogIdentifier(vtkobject) vtkLogger::GetIdentifier(vtkobject).c_str()
542 
543 VTK_ABI_NAMESPACE_END
544 #endif
a simple class to control print indentation
Definition: vtkIndent.h:29
LogScopeRAII(vtkLogger::Verbosity verbosity, const char *fname, unsigned int lineno, VTK_FORMAT_STRING_TYPE format,...) VTK_PRINTF_LIKE(5
LogScopeRAII(LogScopeRAII &&)=default
logging framework for use in VTK and in applications based on VTK
Definition: vtkLogger.h:170
static bool EnableUnsafeSignalHandler
Flag to enable/disable the logging frameworks printing of a stack trace when catching signals,...
Definition: vtkLogger.h:425
static Verbosity ConvertToVerbosity(int value)
Convenience function to convert an integer to matching verbosity level.
void(*)(void *user_data) CloseHandlerCallbackT
Callback handle types.
Definition: vtkLogger.h:325
void(*)(void *user_data, const Message &message) LogHandlerCallbackT
Callback handle types.
Definition: vtkLogger.h:324
static bool RemoveCallback(const char *id)
Remove a callback using the id specified.
static std::string GetIdentifier(vtkObjectBase *obj)
Returns a printable string for a vtkObjectBase instance.
static void EndScope(const char *id)
vtkBaseTypeMacro(vtkLogger, vtkObjectBase)
static std::string GetThreadName()
Get/Set the name to identify the current thread in the log output.
static void SetInternalVerbosityLevel(Verbosity level)
Set internal messages verbosity level.
void(*)(void *user_data) FlushHandlerCallbackT
Callback handle types.
Definition: vtkLogger.h:326
static void AddCallback(const char *id, LogHandlerCallbackT callback, void *user_data, Verbosity verbosity, CloseHandlerCallbackT on_close=nullptr, FlushHandlerCallbackT on_flush=nullptr)
Add a callback to call on each log message with a verbosity less or equal to the given one.
static bool IsEnabled()
Returns true if VTK is built with logging support enabled.
static void SetThreadName(const std::string &name)
Get/Set the name to identify the current thread in the log output.
static Verbosity ConvertToVerbosity(const char *text)
Convenience function to convert a string to matching verbosity level.
static void Init()
Initializes logging.
static void EndLogToFile(const char *path)
Stop logging to a file at the given path.
static void Init(int &argc, char *argv[], const char *verbosity_flag="-v")
Initializes logging.
static void Log(Verbosity verbosity, VTK_FILEPATH const char *fname, unsigned int lineno, const char *txt)
static void SetStderrVerbosity(Verbosity level)
Set the verbosity level for the output logged to stderr.
FileMode
Support log file modes: TRUNCATE truncates the file clearing any existing contents while APPEND appen...
Definition: vtkLogger.h:272
static void LogF(Verbosity verbosity, VTK_FILEPATH const char *fname, unsigned int lineno, VTK_FORMAT_STRING_TYPE format,...) VTK_PRINTF_LIKE(4
static Verbosity GetCurrentVerbosityCutoff()
Returns the maximum verbosity of all log outputs.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
static void StartScope(Verbosity verbosity, const char *id, VTK_FILEPATH const char *fname, unsigned int lineno)
~vtkLogger() override
static void LogToFile(const char *path, FileMode filemode, Verbosity verbosity)
Enable logging to a file at the given path.
abstract base class for most VTK objects
Definition: vtkObjectBase.h:63
void operator=(const vtkObjectBase &)
@ level
Definition: vtkX3D.h:395
@ value
Definition: vtkX3D.h:220
@ name
Definition: vtkX3D.h:219
@ string
Definition: vtkX3D.h:490
The message structure that is passed to custom callbacks registered using vtkLogger::AddCallback.
Definition: vtkLogger.h:308
const char * filename
Definition: vtkLogger.h:312
const char * preamble
Definition: vtkLogger.h:314
Verbosity verbosity
Definition: vtkLogger.h:311
const char * message
Definition: vtkLogger.h:317
const char * indentation
Definition: vtkLogger.h:315
const char * prefix
Definition: vtkLogger.h:316
#define VTK_FORMAT_STRING_TYPE
Definition: vtkLogger.h:165
#define VTK_PRINTF_LIKE(fmtarg, firstvararg)
Definition: vtkLogger.h:164
#define VTK_FILEPATH