Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
Logging.h
Go to the documentation of this file.
1 //===-- serialbox/core/Logging.h ----------------------------------------------------*- C++ -*-===//
2 //
3 // S E R I A L B O X
4 //
5 // This file is distributed under terms of BSD license.
6 // See LICENSE.txt for more information
7 //
8 //===------------------------------------------------------------------------------------------===//
9 //
13 //===------------------------------------------------------------------------------------------===//
14 
15 #ifndef SERIALBOX_CORE_LOGGING_H
16 #define SERIALBOX_CORE_LOGGING_H
17 
18 #include <iostream>
19 
20 namespace serialbox {
21 
24 
25 namespace internal {
26 
27 class NullLogger {
28 public:
29  static NullLogger& getInstance() noexcept;
30 
31  template <class T>
32  NullLogger& operator<<(T&& t) noexcept {
33  return (*this);
34  }
35 
36 private:
37  static NullLogger* instance_;
38 };
39 
40 class LoggerProxy {
41 public:
43  ~LoggerProxy();
44 
45  template <class StreamableValueType>
46  LoggerProxy& operator<<(StreamableValueType&& value) {
47  std::cout << value;
48  return *this;
49  }
50 };
51 
52 class Logger {
53 public:
54  Logger() = default;
55 
56  static Logger& getInstance() noexcept;
57 
58  LoggerProxy trace() noexcept;
59  LoggerProxy debug() noexcept;
60  LoggerProxy info() noexcept;
61  LoggerProxy warning() noexcept;
62  LoggerProxy error() noexcept;
63  LoggerProxy fatal() noexcept;
64 
65 private:
66  static Logger* instance_;
67 };
68 
69 extern bool LoggingIsEnabled;
70 
71 } // namespace internal
72 
76 class Logging {
77 public:
78  Logging() = delete;
79 
81  static void enable() noexcept { internal::LoggingIsEnabled = true; }
82 
84  static void disable() noexcept { internal::LoggingIsEnabled = false; }
85 
87  static bool isEnabled() noexcept { return internal::LoggingIsEnabled; }
88 };
89 
102 #define LOG(severity) SERIALBOX_INTERNAL_LOG(severity)
103 
104 #ifdef SERIALBOX_DISABLE_LOGGING
105 
106 #define SERIALBOX_INTERNAL_LOG(severity) \
107  while(0) \
108  serialbox::internal::NullLogger::getInstance()
109 
110 #else
111 
112 #define SERIALBOX_INTERNAL_LOG(severity) \
113  if(serialbox::Logging::isEnabled()) \
114  serialbox::internal::Logger::getInstance().severity()
115 
116 #endif
117 
119 
120 } // namespace serialbox
121 
122 #endif
static void enable() noexcept
Disable logging.
Definition: Logging.h:81
Namespace of the serialbox library.
Definition: Archive.h:25
static bool isEnabled() noexcept
Return true if logging is eneabled.
Definition: Logging.h:87
Control the logging behaviour.
Definition: Logging.h:76
static void disable() noexcept
Enable logging.
Definition: Logging.h:84
std::ostream & operator<<(std::ostream &stream, const FieldID &f)
Convert FieldID to stream.
Definition: FieldID.cpp:26