VTK  9.2.6
vtkLogger.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkLogger.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
153 #ifndef vtkLogger_h
154 #define vtkLogger_h
155 
156 #include "vtkObjectBase.h"
157 #include "vtkSetGet.h" // needed for macros
158 
159 #include <string> // needed for std::string
160 
161 #if defined(_MSC_VER)
162 #include <sal.h> // Needed for _In_z_ etc annotations
163 #endif
164 
165 // this is copied from `loguru.hpp`
166 #if defined(__clang__) || defined(__GNUC__)
167 // Helper macro for declaring functions as having similar signature to printf.
168 // This allows the compiler to catch format errors at compile-time.
169 #define VTK_PRINTF_LIKE(fmtarg, firstvararg) \
170  __attribute__((__format__(__printf__, fmtarg, firstvararg)))
171 #define VTK_FORMAT_STRING_TYPE const char*
172 #elif defined(_MSC_VER)
173 #define VTK_PRINTF_LIKE(fmtarg, firstvararg)
174 #define VTK_FORMAT_STRING_TYPE _In_z_ _Printf_format_string_ const char*
175 #else
176 #define VTK_PRINTF_LIKE(fmtarg, firstvararg)
177 #define VTK_FORMAT_STRING_TYPE const char*
178 #endif
179 
180 class VTKCOMMONCORE_EXPORT vtkLogger : public vtkObjectBase
181 {
182 public:
183  vtkBaseTypeMacro(vtkLogger, vtkObjectBase);
184  void PrintSelf(ostream& os, vtkIndent indent) override;
185 
187  {
188  // Used to mark an invalid verbosity. Do not log to this level.
189  VERBOSITY_INVALID = -10, // Never do LOG_F(INVALID)
190 
191  // You may use VERBOSITY_OFF on g_stderr_verbosity, but for nothing else!
192  VERBOSITY_OFF = -9, // Never do LOG_F(OFF)
193 
194  VERBOSITY_ERROR = -2,
195  VERBOSITY_WARNING = -1,
196 
197  // Normal messages. By default written to stderr.
198  VERBOSITY_INFO = 0,
199 
200  // Same as VERBOSITY_INFO in every way.
201  VERBOSITY_0 = 0,
202 
203  // Verbosity levels 1-9 are generally not written to stderr, but are written to file.
204  VERBOSITY_1 = +1,
205  VERBOSITY_2 = +2,
206  VERBOSITY_3 = +3,
207  VERBOSITY_4 = +4,
208  VERBOSITY_5 = +5,
209  VERBOSITY_6 = +6,
210  VERBOSITY_7 = +7,
211  VERBOSITY_8 = +8,
212  VERBOSITY_9 = +9,
213 
214  // trace level, same as VERBOSITY_9
215  VERBOSITY_TRACE = +9,
216 
217  // Don not use higher verbosity levels, as that will make grepping log files harder.
218  VERBOSITY_MAX = +9,
219  };
220 
258  static void Init(int& argc, char* argv[], const char* verbosity_flag = "-v");
259  static void Init();
268  static void SetStderrVerbosity(Verbosity level);
269 
276  static void SetInternalVerbosityLevel(Verbosity level);
277 
282  enum FileMode
283  {
285  APPEND
286  };
287 
294  static void LogToFile(const char* path, FileMode filemode, Verbosity verbosity);
295 
299  static void EndLogToFile(const char* path);
300 
302 
305  static void SetThreadName(const std::string& name);
306  static std::string GetThreadName();
308 
312  static std::string GetIdentifier(vtkObjectBase* obj);
313 
318  struct Message
319  {
320  // You would generally print a Message by just concatenating the buffers without spacing.
321  // Optionally, ignore preamble and indentation.
322  Verbosity verbosity; // Already part of preamble
323  const char* filename; // Already part of preamble
324  unsigned line; // Already part of preamble
325  const char* preamble; // Date, time, uptime, thread, file:line, verbosity.
326  const char* indentation; // Just a bunch of spacing.
327  const char* prefix; // Assertion failure info goes here (or "").
328  const char* message; // User message goes here.
329  };
330 
332 
335  using LogHandlerCallbackT = void (*)(void* user_data, const Message& message);
336  using CloseHandlerCallbackT = void (*)(void* user_data);
337  using FlushHandlerCallbackT = void (*)(void* user_data);
339 
349  static void AddCallback(const char* id, LogHandlerCallbackT callback, void* user_data,
350  Verbosity verbosity, CloseHandlerCallbackT on_close = nullptr,
351  FlushHandlerCallbackT on_flush = nullptr);
352 
357  static bool RemoveCallback(const char* id);
358 
362  static bool IsEnabled();
363 
369  static Verbosity GetCurrentVerbosityCutoff();
370 
377  static Verbosity ConvertToVerbosity(int value);
378 
385  static Verbosity ConvertToVerbosity(const char* text);
386 
388 
393  static void Log(
394  Verbosity verbosity, VTK_FILEPATH const char* fname, unsigned int lineno, const char* txt);
395  static void StartScope(
396  Verbosity verbosity, const char* id, VTK_FILEPATH const char* fname, unsigned int lineno);
397  static void EndScope(const char* id);
398 #if !defined(__WRAP__)
399  static void LogF(Verbosity verbosity, VTK_FILEPATH const char* fname, unsigned int lineno,
400  VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(4, 5);
401  static void StartScopeF(Verbosity verbosity, const char* id, VTK_FILEPATH const char* fname,
402  unsigned int lineno, VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(5, 6);
403 
404  class VTKCOMMONCORE_EXPORT LogScopeRAII
405  {
406  public:
407  LogScopeRAII();
408  LogScopeRAII(vtkLogger::Verbosity verbosity, const char* fname, unsigned int lineno,
409  VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(5, 6);
410  ~LogScopeRAII();
411 #if defined(_MSC_VER) && _MSC_VER > 1800
412  // see loguru.hpp for the reason why this is needed on MSVC
413  LogScopeRAII(LogScopeRAII&& other)
414  : Internals(other.Internals)
415  {
416  other.Internals = nullptr;
417  }
418 #else
419  LogScopeRAII(LogScopeRAII&&) = default;
420 #endif
421 
422  private:
423  LogScopeRAII(const LogScopeRAII&) = delete;
424  void operator=(const LogScopeRAII&) = delete;
425  class LSInternals;
426  LSInternals* Internals;
427  };
428 #endif
429 
437 
438 protected:
439  vtkLogger();
440  ~vtkLogger() override;
441 
442 private:
443  vtkLogger(const vtkLogger&) = delete;
444  void operator=(const vtkLogger&) = delete;
445  static vtkLogger::Verbosity InternalVerbosityLevel;
446 };
447 
449 
463 #define vtkVLogF(level, ...) \
464  ((level) > vtkLogger::GetCurrentVerbosityCutoff()) \
465  ? (void)0 \
466  : vtkLogger::LogF(level, __FILE__, __LINE__, __VA_ARGS__)
467 #define vtkLogF(verbosity_name, ...) vtkVLogF(vtkLogger::VERBOSITY_##verbosity_name, __VA_ARGS__)
468 #define vtkVLog(level, x) \
469  if ((level) <= vtkLogger::GetCurrentVerbosityCutoff()) \
470  { \
471  vtkOStrStreamWrapper::EndlType endl; \
472  vtkOStrStreamWrapper::UseEndl(endl); \
473  vtkOStrStreamWrapper vtkmsg; \
474  vtkmsg << "" x; \
475  vtkLogger::Log(level, __FILE__, __LINE__, vtkmsg.str()); \
476  vtkmsg.rdbuf()->freeze(0); \
477  }
478 #define vtkLog(verbosity_name, x) vtkVLog(vtkLogger::VERBOSITY_##verbosity_name, x)
479 
482 
494 #define vtkVLogIfF(level, cond, ...) \
495  ((level) > vtkLogger::GetCurrentVerbosityCutoff() || (cond) == false) \
496  ? (void)0 \
497  : vtkLogger::LogF(level, __FILE__, __LINE__, __VA_ARGS__)
498 
499 #define vtkLogIfF(verbosity_name, cond, ...) \
500  vtkVLogIfF(vtkLogger::VERBOSITY_##verbosity_name, cond, __VA_ARGS__)
501 
502 #define vtkVLogIf(level, cond, x) \
503  if ((level) <= vtkLogger::GetCurrentVerbosityCutoff() && (cond)) \
504  { \
505  vtkOStrStreamWrapper::EndlType endl; \
506  vtkOStrStreamWrapper::UseEndl(endl); \
507  vtkOStrStreamWrapper vtkmsg; \
508  vtkmsg << "" x; \
509  vtkLogger::Log(level, __FILE__, __LINE__, vtkmsg.str()); \
510  vtkmsg.rdbuf()->freeze(0); \
511  }
512 #define vtkLogIf(verbosity_name, cond, x) vtkVLogIf(vtkLogger::VERBOSITY_##verbosity_name, cond, x)
513 
515 #define VTKLOG_CONCAT_IMPL(s1, s2) s1##s2
516 #define VTKLOG_CONCAT(s1, s2) VTKLOG_CONCAT_IMPL(s1, s2)
517 #define VTKLOG_ANONYMOUS_VARIABLE(x) VTKLOG_CONCAT(x, __LINE__)
518 
519 #define vtkVLogScopeF(level, ...) \
520  auto VTKLOG_ANONYMOUS_VARIABLE(msg_context) = ((level) > vtkLogger::GetCurrentVerbosityCutoff()) \
521  ? vtkLogger::LogScopeRAII() \
522  : vtkLogger::LogScopeRAII(level, __FILE__, __LINE__, __VA_ARGS__)
523 
524 #define vtkLogScopeF(verbosity_name, ...) \
525  vtkVLogScopeF(vtkLogger::VERBOSITY_##verbosity_name, __VA_ARGS__)
526 
527 #define vtkLogScopeFunction(verbosity_name) vtkLogScopeF(verbosity_name, "%s", __func__)
528 #define vtkVLogScopeFunction(level) vtkVLogScopeF(level, "%s", __func__)
529 
531 
535 #define vtkLogStartScope(verbosity_name, id) \
536  vtkLogger::StartScope(vtkLogger::VERBOSITY_##verbosity_name, id, __FILE__, __LINE__)
537 #define vtkLogEndScope(id) vtkLogger::EndScope(id)
538 
539 #define vtkLogStartScopeF(verbosity_name, id, ...) \
540  vtkLogger::StartScopeF(vtkLogger::VERBOSITY_##verbosity_name, id, __FILE__, __LINE__, __VA_ARGS__)
541 
542 #define vtkVLogStartScope(level, id) vtkLogger::StartScope(level, id, __FILE__, __LINE__)
543 #define vtkVLogStartScopeF(level, id, ...) \
544  vtkLogger::StartScopeF(level, id, __FILE__, __LINE__, __VA_ARGS__)
545 
552 #define vtkLogIdentifier(vtkobject) vtkLogger::GetIdentifier(vtkobject).c_str()
553 
554 #endif
Verbosity verbosity
Definition: vtkLogger.h:322
The message structure that is passed to custom callbacks registered using vtkLogger::AddCallback.
Definition: vtkLogger.h:318
const char * indentation
Definition: vtkLogger.h:326
const char * filename
Definition: vtkLogger.h:323
void(*)(void *user_data) CloseHandlerCallbackT
Callback handle types.
Definition: vtkLogger.h:336
virtual void PrintSelf(ostream &os, vtkIndent indent)
Methods invoked by print to print information about the object including superclasses.
void(*)(void *user_data) FlushHandlerCallbackT
Callback handle types.
Definition: vtkLogger.h:337
logging framework for use in VTK and in applications based on VTK
Definition: vtkLogger.h:180
a simple class to control print indentation
Definition: vtkIndent.h:39
const char * preamble
Definition: vtkLogger.h:325
#define VTK_PRINTF_LIKE(fmtarg, firstvararg)
Definition: vtkLogger.h:176
abstract base class for most VTK objects
Definition: vtkObjectBase.h:73
const char * message
Definition: vtkLogger.h:328
void(*)(void *user_data, const Message &message) LogHandlerCallbackT
Callback handle types.
Definition: vtkLogger.h:335
const char * prefix
Definition: vtkLogger.h:327
#define VTK_FILEPATH
static bool EnableUnsafeSignalHandler
Flag to enable/disable the logging frameworks printing of a stack trace when catching signals...
Definition: vtkLogger.h:436
#define VTK_FORMAT_STRING_TYPE
Definition: vtkLogger.h:177
void operator=(const vtkObjectBase &)
FileMode
Support log file modes: TRUNCATE truncates the file clearing any existing contents while APPEND appen...
Definition: vtkLogger.h:282