Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef _MBILOG_H
18 #define _MBILOG_H
19 
20 #include <sstream>
21 
22 #include "mbilogBackendBase.h"
23 #include "mbilogBackendCout.h"
24 #include "mbilogConfig.h"
25 #include "mbilogExports.h"
26 #include "mbilogLogMessage.h"
27 #include "mbilogLoggingTypes.h"
28 
29 #ifdef _MSC_VER
30 #pragma warning(push)
31 #pragma warning(disable : 4251)
32 #endif
33 
34 namespace mbilog
35 {
40  void MBILOG_EXPORT RegisterBackend(BackendBase *backend);
41 
44  void MBILOG_EXPORT UnregisterBackend(BackendBase *backend);
45 
49  void MBILOG_EXPORT DistributeToBackends(LogMessage &l);
50 
54  void MBILOG_EXPORT EnableBackends(OutputType type);
58  void MBILOG_EXPORT DisableBackends(OutputType type);
62  bool MBILOG_EXPORT IsBackendEnabled(OutputType type);
63 
70  {
71  protected:
72  bool disabled;
74  std::stringstream ss;
75 
76  public:
77  inline PseudoStream(int level, const char *filePath, int lineNumber, const char *functionName)
78  : disabled(false), msg(LogMessage(level, filePath, lineNumber, functionName)), ss(std::stringstream::out)
79  {
80  }
81 
83  inline ~PseudoStream()
84  {
85  if (!disabled)
86  {
87  msg.message = ss.str();
90  }
91  }
92 
94  template <class T>
95  inline PseudoStream &operator<<(const T &data)
96  {
97  if (!disabled)
98  {
99  std::locale C("C");
100  std::locale originalLocale = ss.getloc();
101  ss.imbue(C);
102 
103  ss << data;
104 
105  ss.imbue(originalLocale);
106  }
107  return *this;
108  }
109 
111  template <class T>
112  inline PseudoStream &operator<<(T &data)
113  {
114  if (!disabled)
115  {
116  std::locale C("C");
117  std::locale originalLocale = ss.getloc();
118  ss.imbue(C);
119 
120  ss << data;
121 
122  ss.imbue(originalLocale);
123  }
124  return *this;
125  }
126 
128  inline PseudoStream &operator<<(std::ostream &(*func)(std::ostream &))
129  {
130  if (!disabled)
131  {
132  std::locale C("C");
133  std::locale originalLocale = ss.getloc();
134  ss.imbue(C);
135 
136  ss << func;
137 
138  ss.imbue(originalLocale);
139  }
140  return *this;
141  }
142 
145  inline PseudoStream &operator()(const char *category)
146  {
147  if (!disabled)
148  {
149  if (msg.category.length())
150  msg.category += ".";
151  msg.category += category;
152  }
153  return *this;
154  }
155 
157  inline PseudoStream &operator()(bool enabled)
158  {
159  disabled |= !enabled;
160  return *this;
161  }
162  };
163 
171  {
172  public:
173  template <class T>
174  inline NullStream &operator<<(const T & /*data*/)
175  {
176  return *this;
177  }
178  template <class T>
179  inline NullStream &operator<<(T & /*data*/)
180  {
181  return *this;
182  }
183  inline NullStream &operator<<(std::ostream &(*)(std::ostream &)) { return *this; }
184  inline NullStream &operator()(const char *) { return *this; }
185  inline NullStream &operator()(bool) { return *this; }
186  };
187 
188  // /** \brief templated backend: one can define a class and a method to create a new backend. */
189  // template<typename T>
190  // struct DelegateBackend : public BackendBase
191  // {
192  //
193  // typedef void(T::*Callback)(const mbilog::LogMessage&);
194  //
195  // DelegateBackend(T* obj, Callback callback) : m_Obj(obj), m_Callback(callback)
196  // {
197  // }
198  //
199  // void ProcessMessage(const mbilog::LogMessage& msg)
200  // {
201  // m_Obj->*m_Callback(msg);
202  // }
203  //
204  // private:
205  //
206  // T* m_Obj;
207  // Callback m_Callback;
208  // };
209 }
210 
211 #ifdef _MSC_VER
212 #pragma warning(pop)
213 #endif
214 
221 #define MBI_INFO mbilog::PseudoStream(mbilog::Info, __FILE__, __LINE__, __FUNCTION__)
222 #define MBI_WARN mbilog::PseudoStream(mbilog::Warn, __FILE__, __LINE__, __FUNCTION__)
223 #define MBI_ERROR mbilog::PseudoStream(mbilog::Error, __FILE__, __LINE__, __FUNCTION__)
224 #define MBI_FATAL mbilog::PseudoStream(mbilog::Fatal, __FILE__, __LINE__, __FUNCTION__)
225 
228 #ifdef MBILOG_ENABLE_DEBUG
229 #define MBI_DEBUG mbilog::PseudoStream(mbilog::Debug, __FILE__, __LINE__, __FUNCTION__)
230 #else
231 #define MBI_DEBUG true ? mbilog::NullStream() : mbilog::NullStream() // this is magic by markus
232 #endif
233 
234 #endif
NullStream & operator<<(std::ostream &(*)(std::ostream &))
Definition: mbilog.h:183
NullStream & operator<<(const T &)
Definition: mbilog.h:174
void MBILOG_EXPORT UnregisterBackend(BackendBase *backend)
Unregisters a backend.
Definition: mbilog.cpp:35
NullStream & operator()(const char *)
Definition: mbilog.h:184
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:74
PseudoStream & operator<<(const T &data)
Definition of the bit shift operator for this class.
Definition: mbilog.h:95
An object of this class simulates a std::cout stream. This means messages can be added by using the b...
Definition: mbilog.h:69
#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:145
NullStream & operator()(bool)
Definition: mbilog.h:185
void MBILOG_EXPORT DisableBackends(OutputType type)
Definition: mbilog.cpp:79
STL namespace.
PseudoStream & operator()(bool enabled)
Enables/disables the PseudoStream. If set to false parsing and output is suppressed.
Definition: mbilog.h:157
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:40
An object of this class represents a single logging message (logging event) of the mbi logging mechan...
std::stringstream ss
Definition: mbilog.h:74
bool MBILOG_EXPORT IsBackendEnabled(OutputType type)
Definition: mbilog.cpp:84
LogMessage msg
Definition: mbilog.h:73
PseudoStream & operator<<(T &data)
Definition of the bit shift operator for this class (for non const data).
Definition: mbilog.h:112
PseudoStream(int level, const char *filePath, int lineNumber, const char *functionName)
Definition: mbilog.h:77
NullStream & operator<<(T &)
Definition: mbilog.h:179
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:30
#define MBILOG_MODULENAME
Definition: mbilogConfig.h:29
PseudoStream & operator<<(std::ostream &(*func)(std::ostream &))
Definition of the bit shift operator for this class (for functions).
Definition: mbilog.h:128
std::string category
Category of the logging event, which was defined by the user.
~PseudoStream()
The message which is stored in the member ss is written to the backend.
Definition: mbilog.h:83
An object of this class simulates a std::cout stream but does nothing. This class is for dummy object...
Definition: mbilog.h:170