Medical Imaging Interaction Toolkit  2018.4.99-08619e4f
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 {
31  void MBILOG_EXPORT RegisterBackend(BackendBase *backend);
32 
35  void MBILOG_EXPORT UnregisterBackend(BackendBase *backend);
36 
40  void MBILOG_EXPORT DistributeToBackends(LogMessage &l);
41 
45  void MBILOG_EXPORT EnableBackends(OutputType type);
49  void MBILOG_EXPORT DisableBackends(OutputType type);
53  bool MBILOG_EXPORT IsBackendEnabled(OutputType type);
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
NullStream & operator<<(std::ostream &(*)(std::ostream &))
Definition: mbilog.h:174
NullStream & operator<<(const T &)
Definition: mbilog.h:165
void MBILOG_EXPORT UnregisterBackend(BackendBase *backend)
Unregisters a backend.
Definition: mbilog.cpp:31
NullStream & operator()(const char *)
Definition: mbilog.h:175
const char * moduleName
Name of the module where the logging message was emitted which is generated by the macros in file mbi...
void MBILOG_EXPORT EnableBackends(OutputType type)
Definition: mbilog.cpp:70
PseudoStream & operator<<(const T &data)
Definition of the bit shift operator for this class.
Definition: mbilog.h:86
An object of this class simulates a std::cout stream. This means messages can be added by using the b...
Definition: mbilog.h:60
#define MBILOG_EXPORT
Definition: mbilogExports.h:15
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
NullStream & operator()(bool)
Definition: mbilog.h:176
void MBILOG_EXPORT DisableBackends(OutputType type)
Definition: mbilog.cpp:75
STL namespace.
PseudoStream & operator()(bool enabled)
Enables/disables the PseudoStream. If set to false parsing and output is suppressed.
Definition: mbilog.h:148
std::string message
The actual logging message.
void MBILOG_EXPORT DistributeToBackends(LogMessage &l)
Distributes the given message to all registered backends. Should only be called by objects of the cla...
Definition: mbilog.cpp:36
An object of this class represents a single logging message (logging event) of the mbi logging mechan...
std::stringstream ss
Definition: mbilog.h:65
bool MBILOG_EXPORT IsBackendEnabled(OutputType type)
Definition: mbilog.cpp:80
LogMessage msg
Definition: mbilog.h:64
PseudoStream & operator<<(T &data)
Definition of the bit shift operator for this class (for non const data).
Definition: mbilog.h:103
PseudoStream(int level, const char *filePath, int lineNumber, const char *functionName)
Definition: mbilog.h:68
NullStream & operator<<(T &)
Definition: mbilog.h:170
void MBILOG_EXPORT RegisterBackend(BackendBase *backend)
Registeres a backend to the mbi logging mechanism. If a backend is registered here, all mbilog messages are relayed to this backend through the method ProcessMessage. If no backend is registered the default backend is used.
Definition: mbilog.cpp:26
#define MBILOG_MODULENAME
Definition: mbilogConfig.h:25
PseudoStream & operator<<(std::ostream &(*func)(std::ostream &))
Definition of the bit shift operator for this class (for functions).
Definition: mbilog.h:119
std::string category
Category of the logging event, which was defined by the user.
std::string functionName
~PseudoStream()
The message which is stored in the member ss is written to the backend.
Definition: mbilog.h:74
An object of this class simulates a std::cout stream but does nothing. This class is for dummy object...
Definition: mbilog.h:161