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=========================================================================*/
162#ifndef vtkLogger_h
163#define vtkLogger_h
164
165#include "vtkObjectBase.h"
166#include "vtkSetGet.h" // needed for macros
167
168#include <string> // needed for std::string
169
170#if defined(_MSC_VER)
171#include <sal.h> // Needed for _In_z_ etc annotations
172#endif
173
174// this is copied from `loguru.hpp`
175#if defined(__clang__) || defined(__GNUC__)
176// Helper macro for declaring functions as having similar signature to printf.
177// This allows the compiler to catch format errors at compile-time.
178#define VTK_PRINTF_LIKE(fmtarg, firstvararg) \
179 __attribute__((__format__(__printf__, fmtarg, firstvararg)))
180#define VTK_FORMAT_STRING_TYPE const char*
181#elif defined(_MSC_VER)
182#define VTK_PRINTF_LIKE(fmtarg, firstvararg)
183#define VTK_FORMAT_STRING_TYPE _In_z_ _Printf_format_string_ const char*
184#else
185#define VTK_PRINTF_LIKE(fmtarg, firstvararg)
186#define VTK_FORMAT_STRING_TYPE const char*
187#endif
188
189class VTKCOMMONCORE_EXPORT vtkLogger : public vtkObjectBase
190{
191public:
193 void PrintSelf(ostream& os, vtkIndent indent) override;
194
196 {
197 // Used to mark an invalid verbosity. Do not log to this level.
198 VERBOSITY_INVALID = -10, // Never do LOG_F(INVALID)
199
200 // You may use VERBOSITY_OFF on g_stderr_verbosity, but for nothing else!
201 VERBOSITY_OFF = -9, // Never do LOG_F(OFF)
202
203 VERBOSITY_ERROR = -2,
204 VERBOSITY_WARNING = -1,
205
206 // Normal messages. By default written to stderr.
207 VERBOSITY_INFO = 0,
208
209 // Same as VERBOSITY_INFO in every way.
210 VERBOSITY_0 = 0,
211
212 // Verbosity levels 1-9 are generally not written to stderr, but are written to file.
213 VERBOSITY_1 = +1,
214 VERBOSITY_2 = +2,
215 VERBOSITY_3 = +3,
216 VERBOSITY_4 = +4,
217 VERBOSITY_5 = +5,
218 VERBOSITY_6 = +6,
219 VERBOSITY_7 = +7,
220 VERBOSITY_8 = +8,
221 VERBOSITY_9 = +9,
222
223 // trace level, same as VERBOSITY_9
224 VERBOSITY_TRACE = +9,
225
226 // Don not use higher verbosity levels, as that will make grepping log files harder.
227 VERBOSITY_MAX = +9,
228 };
229
267 static void Init(int& argc, char* argv[], const char* verbosity_flag = "-v");
268 static void Init();
277 static void SetStderrVerbosity(Verbosity level);
278
286
292 {
294 APPEND
295 };
296
303 static void LogToFile(const char* path, FileMode filemode, Verbosity verbosity);
304
308 static void EndLogToFile(const char* path);
309
311
314 static void SetThreadName(const std::string& name);
315 static std::string GetThreadName();
317
321 static std::string GetIdentifier(vtkObjectBase* obj);
322
327 struct Message
328 {
329 // You would generally print a Message by just concatenating the buffers without spacing.
330 // Optionally, ignore preamble and indentation.
331 Verbosity verbosity; // Already part of preamble
332 const char* filename; // Already part of preamble
333 unsigned line; // Already part of preamble
334 const char* preamble; // Date, time, uptime, thread, file:line, verbosity.
335 const char* indentation; // Just a bunch of spacing.
336 const char* prefix; // Assertion failure info goes here (or "").
337 const char* message; // User message goes here.
338 };
339
341
344 using LogHandlerCallbackT = void (*)(void* user_data, const Message& message);
345 using CloseHandlerCallbackT = void (*)(void* user_data);
346 using FlushHandlerCallbackT = void (*)(void* user_data);
348
358 static void AddCallback(const char* id, LogHandlerCallbackT callback, void* user_data,
359 Verbosity verbosity, CloseHandlerCallbackT on_close = nullptr,
360 FlushHandlerCallbackT on_flush = nullptr);
361
366 static bool RemoveCallback(const char* id);
367
371 static bool IsEnabled();
372
379
386 static Verbosity ConvertToVerbosity(int value);
387
394 static Verbosity ConvertToVerbosity(const char* text);
395
397
402 static void Log(
403 Verbosity verbosity, VTK_FILEPATH const char* fname, unsigned int lineno, const char* txt);
404 static void StartScope(
405 Verbosity verbosity, const char* id, VTK_FILEPATH const char* fname, unsigned int lineno);
406 static void EndScope(const char* id);
407#if !defined(__WRAP__)
408 static void LogF(Verbosity verbosity, VTK_FILEPATH const char* fname, unsigned int lineno,
409 VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(4, 5);
410 static void StartScopeF(Verbosity verbosity, const char* id, VTK_FILEPATH const char* fname,
411 unsigned int lineno, VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(5, 6);
412
413 class VTKCOMMONCORE_EXPORT LogScopeRAII
414 {
415 public:
417 LogScopeRAII(vtkLogger::Verbosity verbosity, const char* fname, unsigned int lineno,
418 VTK_FORMAT_STRING_TYPE format, ...) VTK_PRINTF_LIKE(5, 6);
420#if defined(_MSC_VER) && _MSC_VER > 1800
421 // see loguru.hpp for the reason why this is needed on MSVC
423 : Internals(other.Internals)
424 {
425 other.Internals = nullptr;
426 }
427#else
429#endif
430
431 private:
432 LogScopeRAII(const LogScopeRAII&) = delete;
433 void operator=(const LogScopeRAII&) = delete;
434 class LSInternals;
435 LSInternals* Internals;
436 };
437#endif
439
446
447protected:
449 ~vtkLogger() override;
450
451private:
452 vtkLogger(const vtkLogger&) = delete;
453 void operator=(const vtkLogger&) = delete;
454 static vtkLogger::Verbosity InternalVerbosityLevel;
455};
456
458
472#define vtkVLogF(level, ...) \
473 ((level) > vtkLogger::GetCurrentVerbosityCutoff()) \
474 ? (void)0 \
475 : vtkLogger::LogF(level, __FILE__, __LINE__, __VA_ARGS__)
476#define vtkLogF(verbosity_name, ...) vtkVLogF(vtkLogger::VERBOSITY_##verbosity_name, __VA_ARGS__)
477#define vtkVLog(level, x) \
478 if ((level) <= vtkLogger::GetCurrentVerbosityCutoff()) \
479 { \
480 vtkOStrStreamWrapper::EndlType endl; \
481 vtkOStrStreamWrapper::UseEndl(endl); \
482 vtkOStrStreamWrapper vtkmsg; \
483 vtkmsg << "" x; \
484 vtkLogger::Log(level, __FILE__, __LINE__, vtkmsg.str()); \
485 vtkmsg.rdbuf()->freeze(0); \
486 }
487#define vtkLog(verbosity_name, x) vtkVLog(vtkLogger::VERBOSITY_##verbosity_name, x)
489
491
503#define vtkVLogIfF(level, cond, ...) \
504 ((level) > vtkLogger::GetCurrentVerbosityCutoff() || (cond) == false) \
505 ? (void)0 \
506 : vtkLogger::LogF(level, __FILE__, __LINE__, __VA_ARGS__)
507
508#define vtkLogIfF(verbosity_name, cond, ...) \
509 vtkVLogIfF(vtkLogger::VERBOSITY_##verbosity_name, cond, __VA_ARGS__)
510
511#define vtkVLogIf(level, cond, x) \
512 if ((level) <= vtkLogger::GetCurrentVerbosityCutoff() && (cond)) \
513 { \
514 vtkOStrStreamWrapper::EndlType endl; \
515 vtkOStrStreamWrapper::UseEndl(endl); \
516 vtkOStrStreamWrapper vtkmsg; \
517 vtkmsg << "" x; \
518 vtkLogger::Log(level, __FILE__, __LINE__, vtkmsg.str()); \
519 vtkmsg.rdbuf()->freeze(0); \
520 }
521#define vtkLogIf(verbosity_name, cond, x) vtkVLogIf(vtkLogger::VERBOSITY_##verbosity_name, cond, x)
523
524#define VTKLOG_CONCAT_IMPL(s1, s2) s1##s2
525#define VTKLOG_CONCAT(s1, s2) VTKLOG_CONCAT_IMPL(s1, s2)
526#define VTKLOG_ANONYMOUS_VARIABLE(x) VTKLOG_CONCAT(x, __LINE__)
527
528#define vtkVLogScopeF(level, ...) \
529 auto VTKLOG_ANONYMOUS_VARIABLE(msg_context) = ((level) > vtkLogger::GetCurrentVerbosityCutoff()) \
530 ? vtkLogger::LogScopeRAII() \
531 : vtkLogger::LogScopeRAII(level, __FILE__, __LINE__, __VA_ARGS__)
532
533#define vtkLogScopeF(verbosity_name, ...) \
534 vtkVLogScopeF(vtkLogger::VERBOSITY_##verbosity_name, __VA_ARGS__)
535
536#define vtkLogScopeFunction(verbosity_name) vtkLogScopeF(verbosity_name, "%s", __func__)
537#define vtkVLogScopeFunction(level) vtkVLogScopeF(level, "%s", __func__)
538
540
544#define vtkLogStartScope(verbosity_name, id) \
545 vtkLogger::StartScope(vtkLogger::VERBOSITY_##verbosity_name, id, __FILE__, __LINE__)
546#define vtkLogEndScope(id) vtkLogger::EndScope(id)
547
548#define vtkLogStartScopeF(verbosity_name, id, ...) \
549 vtkLogger::StartScopeF(vtkLogger::VERBOSITY_##verbosity_name, id, __FILE__, __LINE__, __VA_ARGS__)
550
551#define vtkVLogStartScope(level, id) vtkLogger::StartScope(level, id, __FILE__, __LINE__)
552#define vtkVLogStartScopeF(level, id, ...) \
553 vtkLogger::StartScopeF(level, id, __FILE__, __LINE__, __VA_ARGS__)
555
561#define vtkLogIdentifier(vtkobject) vtkLogger::GetIdentifier(vtkobject).c_str()
562
563#endif
a simple class to control print indentation
Definition vtkIndent.h:49
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:190
static bool EnableUnsafeSignalHandler
Flag to enable/disable the logging frameworks printing of a stack trace when catching signals,...
Definition vtkLogger.h:445
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:345
void(*)(void *user_data, const Message &message) LogHandlerCallbackT
Callback handle types.
Definition vtkLogger.h:344
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:346
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:292
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
void operator=(const vtkObjectBase &)
The message structure that is passed to custom callbacks registered using vtkLogger::AddCallback.
Definition vtkLogger.h:328
const char * filename
Definition vtkLogger.h:332
const char * preamble
Definition vtkLogger.h:334
Verbosity verbosity
Definition vtkLogger.h:331
const char * message
Definition vtkLogger.h:337
const char * indentation
Definition vtkLogger.h:335
const char * prefix
Definition vtkLogger.h:336
#define VTK_FORMAT_STRING_TYPE
Definition vtkLogger.h:186
#define VTK_PRINTF_LIKE(fmtarg, firstvararg)
Definition vtkLogger.h:185
#define VTK_FILEPATH