Medical Imaging Interaction Toolkit  2025.12.02
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 {
40 
44 
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
#define MITKLOG_EXPORT
Interface for log backends that can be registered in the MITK log mechanism.
Simulates a std::cout stream but does nothing.
Definition: mitkLog.h:178
NullLogStream & operator()(const char *)
Definition: mitkLog.h:197
NullLogStream & operator()(bool)
Definition: mitkLog.h:202
NullLogStream & operator<<(T &)
Definition: mitkLog.h:187
NullLogStream & operator<<(const T &)
Definition: mitkLog.h:181
NullLogStream & operator<<(std::ostream &(*)(std::ostream &))
Definition: mitkLog.h:192
Simulates a std::cout stream.
Definition: mitkLog.h:68
PseudoLogStream & operator()(const std::string &category)
Sets the category of this PseudoLogStream object.
Definition: mitkLog.h:143
~PseudoLogStream()
The encapsulated message is written to the backend.
Definition: mitkLog.h:79
PseudoLogStream & operator<<(std::ostream &(*func)(std::ostream &))
Definition: mitkLog.h:123
std::stringstream m_Stream
Definition: mitkLog.h:169
PseudoLogStream & operator()(bool enabled)
Enables/disables the PseudoLogStream.
Definition: mitkLog.h:160
PseudoLogStream & operator<<(const T &data)
Definition: mitkLog.h:90
LogMessage m_Message
Definition: mitkLog.h:168
PseudoLogStream(LogLevel level, const std::string &filePath, int lineNumber, const std::string &functionName)
Definition: mitkLog.h:70
PseudoLogStream & operator<<(T &data)
Definition: mitkLog.h:107
#define MITKLOG_MODULENAME
Definition: mitkLog.h:28
Find image slices visible on a given plane.
LogLevel
Message/event levels of the MITK log mechanism.
Definition: mitkLogLevel.h:21
void MITKLOG_EXPORT UnregisterBackend(LogBackendBase *backend)
Unregister a backend.
void MITKLOG_EXPORT EnableBackends(LogBackendBase::OutputType type)
Enable the output of a backend.
void MITKLOG_EXPORT DisableBackends(LogBackendBase::OutputType type)
Disable the output of a backend.
void MITKLOG_EXPORT DistributeToBackends(LogMessage &message)
Distribute the given message to all registered backends.
bool MITKLOG_EXPORT IsBackendEnabled(LogBackendBase::OutputType type)
Check whether the output of this backend is enabled.
void MITKLOG_EXPORT RegisterBackend(LogBackendBase *backend)
Register a backend in the MITK log mechanism.
A single log message (log event) of the MITK log mechanism.