Medical Imaging Interaction Toolkit  2023.04.00
Medical Imaging Interaction Toolkit
mbilog.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef _MBILOG_H
14 #define _MBILOG_H
15 
16 #include <sstream>
17 
18 #include "mbilogBackendBase.h"
19 #include "mbilogBackendCout.h"
20 #include "mbilogConfig.h"
21 #include "mbilogExports.h"
22 #include "mbilogLogMessage.h"
23 #include "mbilogLoggingTypes.h"
24 
25 namespace mbilog
26 {
32 
36 
41 
54 
61  {
62  protected:
63  bool disabled;
65  std::stringstream ss;
66 
67  public:
68  inline PseudoStream(int level, const char *filePath, int lineNumber, const char *functionName)
69  : disabled(false), msg(LogMessage(level, filePath, lineNumber, functionName)), ss(std::stringstream::out)
70  {
71  }
72 
74  inline ~PseudoStream()
75  {
76  if (!disabled)
77  {
78  msg.message = ss.str();
81  }
82  }
83 
85  template <class T>
86  inline PseudoStream &operator<<(const T &data)
87  {
88  if (!disabled)
89  {
90  std::locale C("C");
91  std::locale originalLocale = ss.getloc();
92  ss.imbue(C);
93 
94  ss << data;
95 
96  ss.imbue(originalLocale);
97  }
98  return *this;
99  }
100 
102  template <class T>
103  inline PseudoStream &operator<<(T &data)
104  {
105  if (!disabled)
106  {
107  std::locale C("C");
108  std::locale originalLocale = ss.getloc();
109  ss.imbue(C);
110 
111  ss << data;
112 
113  ss.imbue(originalLocale);
114  }
115  return *this;
116  }
117 
119  inline PseudoStream &operator<<(std::ostream &(*func)(std::ostream &))
120  {
121  if (!disabled)
122  {
123  std::locale C("C");
124  std::locale originalLocale = ss.getloc();
125  ss.imbue(C);
126 
127  ss << func;
128 
129  ss.imbue(originalLocale);
130  }
131  return *this;
132  }
133 
136  inline PseudoStream &operator()(const char *category)
137  {
138  if (!disabled)
139  {
140  if (msg.category.length())
141  msg.category += ".";
142  msg.category += category;
143  }
144  return *this;
145  }
146 
148  inline PseudoStream &operator()(bool enabled)
149  {
150  disabled |= !enabled;
151  return *this;
152  }
153  };
154 
162  {
163  public:
164  template <class T>
165  inline NullStream &operator<<(const T & /*data*/)
166  {
167  return *this;
168  }
169  template <class T>
170  inline NullStream &operator<<(T & /*data*/)
171  {
172  return *this;
173  }
174  inline NullStream &operator<<(std::ostream &(*)(std::ostream &)) { return *this; }
175  inline NullStream &operator()(const char *) { return *this; }
176  inline NullStream &operator()(bool) { return *this; }
177  };
178 
179  // /** \brief templated backend: one can define a class and a method to create a new backend. */
180  // template<typename T>
181  // struct DelegateBackend : public BackendBase
182  // {
183  //
184  // typedef void(T::*Callback)(const mbilog::LogMessage&);
185  //
186  // DelegateBackend(T* obj, Callback callback) : m_Obj(obj), m_Callback(callback)
187  // {
188  // }
189  //
190  // void ProcessMessage(const mbilog::LogMessage& msg)
191  // {
192  // m_Obj->*m_Callback(msg);
193  // }
194  //
195  // private:
196  //
197  // T* m_Obj;
198  // Callback m_Callback;
199  // };
200 }
201 
208 #define MBI_INFO mbilog::PseudoStream(mbilog::Info, __FILE__, __LINE__, __FUNCTION__)
209 #define MBI_WARN mbilog::PseudoStream(mbilog::Warn, __FILE__, __LINE__, __FUNCTION__)
210 #define MBI_ERROR mbilog::PseudoStream(mbilog::Error, __FILE__, __LINE__, __FUNCTION__)
211 #define MBI_FATAL mbilog::PseudoStream(mbilog::Fatal, __FILE__, __LINE__, __FUNCTION__)
212 
215 #ifdef MBILOG_ENABLE_DEBUG
216 #define MBI_DEBUG mbilog::PseudoStream(mbilog::Debug, __FILE__, __LINE__, __FUNCTION__)
217 #else
218 #define MBI_DEBUG true ? mbilog::NullStream() : mbilog::NullStream() // this is magic by markus
219 #endif
220 
221 #endif
MBILOG_EXPORT
#define MBILOG_EXPORT
Definition: mbilogExports.h:15
mbilog::NullStream::operator()
NullStream & operator()(const char *)
Definition: mbilog.h:175
mbilogLoggingTypes.h
mbilog::LogMessage
An object of this class represents a single logging message (logging event) of the mbi logging mechan...
Definition: mbilogLogMessage.h:30
mbilog::DisableBackends
void MBILOG_EXPORT DisableBackends(OutputType type)
mbilogConfig.h
mbilog::PseudoStream::operator<<
PseudoStream & operator<<(std::ostream &(*func)(std::ostream &))
Definition of the bit shift operator for this class (for functions).
Definition: mbilog.h:119
mbilog
Definition: mbilog.h:25
MBILOG_MODULENAME
#define MBILOG_MODULENAME
Definition: mbilogConfig.h:25
mbilog::PseudoStream::msg
LogMessage msg
Definition: mbilog.h:64
mbilog::DistributeToBackends
void MBILOG_EXPORT DistributeToBackends(LogMessage &l)
Distributes the given message to all registered backends. Should only be called by objects of the cla...
mbilogExports.h
mbilog::NullStream::operator()
NullStream & operator()(bool)
Definition: mbilog.h:176
mbilog::PseudoStream::~PseudoStream
~PseudoStream()
The message which is stored in the member ss is written to the backend.
Definition: mbilog.h:74
mbilog::BackendBase
This class is an interface for logging backends that can be registered in the mbi logging mechanism.
Definition: mbilogBackendBase.h:37
mbilogBackendBase.h
mbilog::LogMessage::moduleName
const char * moduleName
Name of the module where the logging message was emitted which is generated by the macros in file mbi...
Definition: mbilogLogMessage.h:54
mbilog::UnregisterBackend
void MBILOG_EXPORT UnregisterBackend(BackendBase *backend)
Unregisters a backend.
mbilog::PseudoStream
An object of this class simulates a std::cout stream. This means messages can be added by using the b...
Definition: mbilog.h:60
mbilogBackendCout.h
mbilog::RegisterBackend
void MBILOG_EXPORT RegisterBackend(BackendBase *backend)
Registeres a backend to the mbi logging mechanism. If a backend is registered here,...
mbilog::NullStream
An object of this class simulates a std::cout stream but does nothing. This class is for dummy object...
Definition: mbilog.h:161
mbilog::PseudoStream::operator()
PseudoStream & operator()(bool enabled)
Enables/disables the PseudoStream. If set to false parsing and output is suppressed.
Definition: mbilog.h:148
mbilog::LogMessage::category
std::string category
Category of the logging event, which was defined by the user.
Definition: mbilogLogMessage.h:59
mbilog::PseudoStream::operator<<
PseudoStream & operator<<(const T &data)
Definition of the bit shift operator for this class.
Definition: mbilog.h:86
mbilog::NullStream::operator<<
NullStream & operator<<(T &)
Definition: mbilog.h:170
mbilog::PseudoStream::ss
std::stringstream ss
Definition: mbilog.h:65
mbilog::NullStream::operator<<
NullStream & operator<<(const T &)
Definition: mbilog.h:165
mbilog::OutputType
OutputType
Definition: mbilogBackendBase.h:26
mbilog::PseudoStream::operator()
PseudoStream & operator()(const char *category)
Sets the category of this PseudoStream object. If there already is a category it is appended,...
Definition: mbilog.h:136
mbilog::PseudoStream::disabled
bool disabled
Definition: mbilog.h:63
mbilog::EnableBackends
void MBILOG_EXPORT EnableBackends(OutputType type)
mbilog::LogMessage::message
std::string message
The actual logging message.
Definition: mbilogLogMessage.h:62
mbilog::NullStream::operator<<
NullStream & operator<<(std::ostream &(*)(std::ostream &))
Definition: mbilog.h:174
mbilog::PseudoStream::operator<<
PseudoStream & operator<<(T &data)
Definition of the bit shift operator for this class (for non const data).
Definition: mbilog.h:103
mbilog::PseudoStream::PseudoStream
PseudoStream(int level, const char *filePath, int lineNumber, const char *functionName)
Definition: mbilog.h:68
mbilogLogMessage.h
mbilog::IsBackendEnabled
bool MBILOG_EXPORT IsBackendEnabled(OutputType type)