VTK  9.0.1
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 =========================================================================*/
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 class VTKCOMMONCORE_EXPORT vtkLogger : public vtkObjectBase
169 {
170 public:
171  vtkBaseTypeMacro(vtkLogger, vtkObjectBase);
172  void PrintSelf(ostream& os, vtkIndent indent) override;
173 
175  {
176  // Used to mark an invalid verbosity. Do not log to this level.
177  VERBOSITY_INVALID = -10, // Never do LOG_F(INVALID)
178 
179  // You may use VERBOSITY_OFF on g_stderr_verbosity, but for nothing else!
180  VERBOSITY_OFF = -9, // Never do LOG_F(OFF)
181 
182  VERBOSITY_ERROR = -2,
183  VERBOSITY_WARNING = -1,
184 
185  // Normal messages. By default written to stderr.
186  VERBOSITY_INFO = 0,
187 
188  // Same as VERBOSITY_INFO in every way.
189  VERBOSITY_0 = 0,
190 
191  // Verbosity levels 1-9 are generally not written to stderr, but are written to file.
192  VERBOSITY_1 = +1,
193  VERBOSITY_2 = +2,
194  VERBOSITY_3 = +3,
195  VERBOSITY_4 = +4,
196  VERBOSITY_5 = +5,
197  VERBOSITY_6 = +6,
198  VERBOSITY_7 = +7,
199  VERBOSITY_8 = +8,
200  VERBOSITY_9 = +9,
201 
202  // trace level, same as VERBOSITY_9
203  VERBOSITY_TRACE = +9,
204 
205  // Don not use higher verbosity levels, as that will make grepping log files harder.
206  VERBOSITY_MAX = +9,
207  };
208 
241  static void Init(int& argc, char* argv[], const char* verbosity_flag = "-v");
242  static void Init();
251  static void SetStderrVerbosity(Verbosity level);
252 
257  enum FileMode
258  {
260  APPEND
261  };
262 
269  static void LogToFile(const char* path, FileMode filemode, Verbosity verbosity);
270 
274  static void EndLogToFile(const char* path);
275 
277 
280  static void SetThreadName(const std::string& name);
281  static std::string GetThreadName();
283 
287  static std::string GetIdentifier(vtkObjectBase* obj);
288 
293  struct Message
294  {
295  // You would generally print a Message by just concatenating the buffers without spacing.
296  // Optionally, ignore preamble and indentation.
297  Verbosity verbosity; // Already part of preamble
298  const char* filename; // Already part of preamble
299  unsigned line; // Already part of preamble
300  const char* preamble; // Date, time, uptime, thread, file:line, verbosity.
301  const char* indentation; // Just a bunch of spacing.
302  const char* prefix; // Assertion failure info goes here (or "").
303  const char* message; // User message goes here.
304  };
305 
307 
310  using LogHandlerCallbackT = void (*)(void* user_data, const Message& message);
311  using CloseHandlerCallbackT = void (*)(void* user_data);
312  using FlushHandlerCallbackT = void (*)(void* user_data);
314 
324  static void AddCallback(const char* id, LogHandlerCallbackT callback, void* user_data,
325  Verbosity verbosity, CloseHandlerCallbackT on_close = nullptr,
326  FlushHandlerCallbackT on_flush = nullptr);
327 
332  static bool RemoveCallback(const char* id);
333 
337  static bool IsEnabled();
338 
344  static Verbosity GetCurrentVerbosityCutoff();
345 
352  static Verbosity ConvertToVerbosity(int value);
353 
360  static Verbosity ConvertToVerbosity(const char* value);
361 
363 
368  static void Log(Verbosity verbosity, const char* fname, unsigned int lineno, const char* txt);
369  static void StartScope(
370  Verbosity verbosity, const char* id, const char* fname, unsigned int lineno);
371  static void EndScope(const char* id);
372 #if !defined(__WRAP__)
373  static void LogF(Verbosity verbosity, const char* fname, unsigned int lineno,
374  VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(4, 5);
375  static void StartScopeF(Verbosity verbosity, const char* id, const char* fname,
376  unsigned int lineno, VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(5, 6);
377 
378  class VTKCOMMONCORE_EXPORT LogScopeRAII
379  {
380  public:
381  LogScopeRAII();
382  LogScopeRAII(vtkLogger::Verbosity verbosity, const char* fname, unsigned int lineno,
383  VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(5, 6);
384  ~LogScopeRAII();
385 #if defined(_MSC_VER) && _MSC_VER > 1800
386  // see loguru.hpp for the reason why this is needed on MSVC
387  LogScopeRAII(LogScopeRAII&& other)
388  : Internals(other.Internals)
389  {
390  other.Internals = nullptr;
391  }
392 #else
393  LogScopeRAII(LogScopeRAII&&) = default;
394 #endif
395 
396  private:
397  LogScopeRAII(const LogScopeRAII&) = delete;
398  void operator=(const LogScopeRAII&) = delete;
399  class LSInternals;
400  LSInternals* Internals;
401  };
402 #endif
403 
404 protected:
405  vtkLogger();
406  ~vtkLogger() override;
407 
408 private:
409  vtkLogger(const vtkLogger&) = delete;
410  void operator=(const vtkLogger&) = delete;
411 };
412 
414 
428 #define vtkVLogF(level, ...) \
429  ((level) > vtkLogger::GetCurrentVerbosityCutoff()) \
430  ? (void)0 \
431  : vtkLogger::LogF(level, __FILE__, __LINE__, __VA_ARGS__)
432 #define vtkLogF(verbosity_name, ...) vtkVLogF(vtkLogger::VERBOSITY_##verbosity_name, __VA_ARGS__)
433 #define vtkVLog(level, x) \
434  if ((level) <= vtkLogger::GetCurrentVerbosityCutoff()) \
435  { \
436  vtkOStrStreamWrapper::EndlType endl; \
437  vtkOStrStreamWrapper::UseEndl(endl); \
438  vtkOStrStreamWrapper vtkmsg; \
439  vtkmsg << "" x; \
440  vtkLogger::Log(level, __FILE__, __LINE__, vtkmsg.str()); \
441  vtkmsg.rdbuf()->freeze(0); \
442  }
443 #define vtkLog(verbosity_name, x) vtkVLog(vtkLogger::VERBOSITY_##verbosity_name, x)
444 
445 
447 
459 #define vtkVLogIfF(level, cond, ...) \
460  ((level) > vtkLogger::GetCurrentVerbosityCutoff() || (cond) == false) \
461  ? (void)0 \
462  : vtkLogger::LogF(level, __FILE__, __LINE__, __VA_ARGS__)
463 
464 #define vtkLogIfF(verbosity_name, cond, ...) \
465  vtkVLogIfF(vtkLogger::VERBOSITY_##verbosity_name, cond, __VA_ARGS__)
466 
467 #define vtkVLogIf(level, cond, x) \
468  if ((level) <= vtkLogger::GetCurrentVerbosityCutoff() && (cond)) \
469  { \
470  vtkOStrStreamWrapper::EndlType endl; \
471  vtkOStrStreamWrapper::UseEndl(endl); \
472  vtkOStrStreamWrapper vtkmsg; \
473  vtkmsg << "" x; \
474  vtkLogger::Log(level, __FILE__, __LINE__, vtkmsg.str()); \
475  vtkmsg.rdbuf()->freeze(0); \
476  }
477 #define vtkLogIf(verbosity_name, cond, x) vtkVLogIf(vtkLogger::VERBOSITY_##verbosity_name, cond, x)
478 
479 
480 #define VTKLOG_CONCAT_IMPL(s1, s2) s1##s2
481 #define VTKLOG_CONCAT(s1, s2) VTKLOG_CONCAT_IMPL(s1, s2)
482 #define VTKLOG_ANONYMOUS_VARIABLE(x) VTKLOG_CONCAT(x, __LINE__)
483 
484 #define vtkVLogScopeF(level, ...) \
485  auto VTKLOG_ANONYMOUS_VARIABLE(msg_context) = ((level) > vtkLogger::GetCurrentVerbosityCutoff()) \
486  ? vtkLogger::LogScopeRAII() \
487  : vtkLogger::LogScopeRAII(level, __FILE__, __LINE__, __VA_ARGS__)
488 
489 #define vtkLogScopeF(verbosity_name, ...) \
490  vtkVLogScopeF(vtkLogger::VERBOSITY_##verbosity_name, __VA_ARGS__)
491 
492 #define vtkLogScopeFunction(verbosity_name) vtkLogScopeF(verbosity_name, "%s", __func__)
493 #define vtkVLogScopeFunction(level) vtkVLogScopeF(level, "%s", __func__)
494 
496 
500 #define vtkLogStartScope(verbosity_name, id) \
501  vtkLogger::StartScope(vtkLogger::VERBOSITY_##verbosity_name, id, __FILE__, __LINE__)
502 #define vtkLogEndScope(id) vtkLogger::EndScope(id)
503 
504 #define vtkLogStartScopeF(verbosity_name, id, ...) \
505  vtkLogger::StartScopeF(vtkLogger::VERBOSITY_##verbosity_name, id, __FILE__, __LINE__, __VA_ARGS__)
506 
507 #define vtkVLogStartScope(level, id) vtkLogger::StartScope(level, id, __FILE__, __LINE__)
508 #define vtkVLogStartScopeF(level, id, ...) \
509  vtkLogger::StartScopeF(level, id, __FILE__, __LINE__, __VA_ARGS__)
510 
511 
517 #define vtkLogIdentifier(vtkobject) vtkLogger::GetIdentifier(vtkobject).c_str()
518 
519 #endif
Verbosity verbosity
Definition: vtkLogger.h:297
The message structure that is passed to custom callbacks registered using vtkLogger::AddCallback.
Definition: vtkLogger.h:293
const char * indentation
Definition: vtkLogger.h:301
const char * filename
Definition: vtkLogger.h:298
void(*)(void *user_data) CloseHandlerCallbackT
Callback handle types.
Definition: vtkLogger.h:311
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:312
logging framework for use in VTK and in applications based on VTK
Definition: vtkLogger.h:168
a simple class to control print indentation
Definition: vtkIndent.h:33
const char * preamble
Definition: vtkLogger.h:300
#define VTK_PRINTF_LIKE(fmtarg, firstvararg)
Definition: vtkLogger.h:164
abstract base class for most VTK objects
Definition: vtkObjectBase.h:63
const char * message
Definition: vtkLogger.h:303
void(*)(void *user_data, const Message &message) LogHandlerCallbackT
Callback handle types.
Definition: vtkLogger.h:310
const char * prefix
Definition: vtkLogger.h:302
#define VTK_FORMAT_STRING_TYPE
Definition: vtkLogger.h:165
void operator=(const vtkObjectBase &)
FileMode
Support log file modes: TRUNCATE truncates the file clearing any existing contents while APPEND appen...
Definition: vtkLogger.h:257