Medical Imaging Interaction Toolkit  2024.06.00
Medical Imaging Interaction Toolkit
mitkLog.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 mitkLog_h
14 #define mitkLog_h
15 
16 #include <mitkLogBackendBase.h>
17 
18 #include <sstream>
19 
20 #include <MitkLogExports.h>
21 
22 #ifndef MITKLOG_MODULENAME
23 # if defined(US_MODULE_NAME)
24 # define MITKLOG_STR_(x) #x
25 # define MITKLOG_STR(x) MITKLOG_STR_(x)
26 # define MITKLOG_MODULENAME MITKLOG_STR(US_MODULE_NAME)
27 # else
28 # define MITKLOG_MODULENAME "n/a"
29 # endif
30 #endif
31 
32 namespace mitk
33 {
39  void MITKLOG_EXPORT RegisterBackend(LogBackendBase* backend);
40 
43  void MITKLOG_EXPORT UnregisterBackend(LogBackendBase* backend);
44 
49  void MITKLOG_EXPORT DistributeToBackends(LogMessage& message);
50 
54 
58 
62 
68  {
69  public:
70  PseudoLogStream(LogLevel level, const std::string& filePath, int lineNumber, const std::string& functionName)
71  : m_Disabled(false),
72  m_Message(level, filePath, lineNumber, functionName),
73  m_Stream(std::stringstream::out)
74  {
75  }
76 
80  {
81  if (!m_Disabled)
82  {
83  m_Message.Message = m_Stream.str();
84  m_Message.ModuleName = MITKLOG_MODULENAME;
85  DistributeToBackends(m_Message);
86  }
87  }
88 
89  template <class T>
90  PseudoLogStream& operator<<(const T& data)
91  {
92  if (!m_Disabled)
93  {
94  std::locale C("C");
95  std::locale originalLocale = m_Stream.getloc();
96  m_Stream.imbue(C);
97 
98  m_Stream << data;
99 
100  m_Stream.imbue(originalLocale);
101  }
102 
103  return *this;
104  }
105 
106  template <class T>
108  {
109  if (!m_Disabled)
110  {
111  std::locale C("C");
112  std::locale originalLocale = m_Stream.getloc();
113  m_Stream.imbue(C);
114 
115  m_Stream << data;
116 
117  m_Stream.imbue(originalLocale);
118  }
119 
120  return *this;
121  }
122 
123  PseudoLogStream& operator<<(std::ostream& (*func)(std::ostream&))
124  {
125  if (!m_Disabled)
126  {
127  std::locale C("C");
128  std::locale originalLocale = m_Stream.getloc();
129  m_Stream.imbue(C);
130 
131  m_Stream << func;
132 
133  m_Stream.imbue(originalLocale);
134  }
135 
136  return *this;
137  }
138 
143  PseudoLogStream& operator()(const std::string& category)
144  {
145  if (!m_Disabled)
146  {
147  if (m_Message.Category.length())
148  m_Message.Category += ".";
149 
150  m_Message.Category += category;
151  }
152 
153  return *this;
154  }
155 
161  {
162  m_Disabled |= !enabled;
163  return *this;
164  }
165 
166  protected:
169  std::stringstream m_Stream;
170  };
171 
178  {
179  public:
180  template <class T>
182  {
183  return *this;
184  }
185 
186  template <class T>
188  {
189  return *this;
190  }
191 
192  NullLogStream& operator<<(std::ostream &(*)(std::ostream &))
193  {
194  return *this;
195  }
196 
197  NullLogStream& operator()(const char*)
198  {
199  return *this;
200  }
201 
203  {
204  return *this;
205  }
206  };
207 }
208 
209 #define MITK_INFO mitk::PseudoLogStream(mitk::LogLevel::Info, __FILE__, __LINE__, __FUNCTION__)
210 #define MITK_WARN mitk::PseudoLogStream(mitk::LogLevel::Warn, __FILE__, __LINE__, __FUNCTION__)
211 #define MITK_ERROR mitk::PseudoLogStream(mitk::LogLevel::Error, __FILE__, __LINE__, __FUNCTION__)
212 #define MITK_FATAL mitk::PseudoLogStream(mitk::LogLevel::Fatal, __FILE__, __LINE__, __FUNCTION__)
213 
214 #ifdef MITK_ENABLE_DEBUG_MESSAGES
215 #define MITK_DEBUG mitk::PseudoLogStream(mitk::LogLevel::Debug, __FILE__, __LINE__, __FUNCTION__)
216 #else
217 #define MITK_DEBUG true ? mitk::NullLogStream() : mitk::NullLogStream()
218 #endif
219 
220 #endif
mitk::NullLogStream::operator<<
NullLogStream & operator<<(std::ostream &(*)(std::ostream &))
Definition: mitkLog.h:192
mitk::PseudoLogStream::m_Stream
std::stringstream m_Stream
Definition: mitkLog.h:169
mitk::LogLevel
LogLevel
Message/event levels of the MITK log mechanism.
Definition: mitkLogLevel.h:20
mitk::NullLogStream::operator<<
NullLogStream & operator<<(const T &)
Definition: mitkLog.h:181
mitk::PseudoLogStream::operator<<
PseudoLogStream & operator<<(T &data)
Definition: mitkLog.h:107
mitk::NullLogStream
Simulates a std::cout stream but does nothing.
Definition: mitkLog.h:177
mitk::PseudoLogStream
Simulates a std::cout stream.
Definition: mitkLog.h:67
mitkLogBackendBase.h
mitk::EnableBackends
void MITKLOG_EXPORT EnableBackends(LogBackendBase::OutputType type)
Enable the output of a backend.
mitk::LogMessage
A single log message (log event) of the MITK log mechanism.
Definition: mitkLogMessage.h:29
mitk::PseudoLogStream::operator<<
PseudoLogStream & operator<<(std::ostream &(*func)(std::ostream &))
Definition: mitkLog.h:123
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
MITKLOG_EXPORT
#define MITKLOG_EXPORT
Definition: MitkLogExports.h:15
mitk::PseudoLogStream::~PseudoLogStream
~PseudoLogStream()
The encapsulated message is written to the backend.
Definition: mitkLog.h:79
mitk::RegisterBackend
void MITKLOG_EXPORT RegisterBackend(LogBackendBase *backend)
Register a backend in the MITK log mechanism.
mitk::PseudoLogStream::m_Message
LogMessage m_Message
Definition: mitkLog.h:168
mitk::PseudoLogStream::m_Disabled
bool m_Disabled
Definition: mitkLog.h:167
mitk::NullLogStream::operator<<
NullLogStream & operator<<(T &)
Definition: mitkLog.h:187
mitk::PseudoLogStream::operator()
PseudoLogStream & operator()(bool enabled)
Enables/disables the PseudoLogStream.
Definition: mitkLog.h:160
MITKLOG_MODULENAME
#define MITKLOG_MODULENAME
Definition: mitkLog.h:28
mitk::DistributeToBackends
void MITKLOG_EXPORT DistributeToBackends(LogMessage &message)
Distribute the given message to all registered backends.
mitk::PseudoLogStream::operator<<
PseudoLogStream & operator<<(const T &data)
Definition: mitkLog.h:90
mitk::IsBackendEnabled
bool MITKLOG_EXPORT IsBackendEnabled(LogBackendBase::OutputType type)
Check whether the output of this backend is enabled.
mitk::NullLogStream::operator()
NullLogStream & operator()(const char *)
Definition: mitkLog.h:197
mitk::LogBackendBase::OutputType
OutputType
Definition: mitkLogBackendBase.h:28
mitk::NullLogStream::operator()
NullLogStream & operator()(bool)
Definition: mitkLog.h:202
mitk::UnregisterBackend
void MITKLOG_EXPORT UnregisterBackend(LogBackendBase *backend)
Unregister a backend.
mitk::DisableBackends
void MITKLOG_EXPORT DisableBackends(LogBackendBase::OutputType type)
Disable the output of a backend.
MitkLogExports.h
mitk::PseudoLogStream::operator()
PseudoLogStream & operator()(const std::string &category)
Sets the category of this PseudoLogStream object.
Definition: mitkLog.h:143
mitk::PseudoLogStream::PseudoLogStream
PseudoLogStream(LogLevel level, const std::string &filePath, int lineNumber, const std::string &functionName)
Definition: mitkLog.h:70