Medical Imaging Interaction Toolkit  2026.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 {
52 
63 
80 
91 
102 
113 
137  {
138  public:
146  PseudoLogStream(LogLevel level, const std::string& filePath, int lineNumber, const std::string& functionName)
147  : m_Disabled(false),
148  m_Message(level, filePath, lineNumber, functionName),
149  m_Stream(std::stringstream::out)
150  {
151  }
152 
160  {
161  if (!m_Disabled)
162  {
163  m_Message.Message = m_Stream.str();
164  m_Message.ModuleName = MITKLOG_MODULENAME;
165  DistributeToBackends(m_Message);
166  }
167  }
168 
178  template <class T>
179  PseudoLogStream& operator<<(const T& data)
180  {
181  if (!m_Disabled)
182  {
183  std::locale C("C");
184  std::locale originalLocale = m_Stream.getloc();
185  m_Stream.imbue(C);
186 
187  m_Stream << data;
188 
189  m_Stream.imbue(originalLocale);
190  }
191 
192  return *this;
193  }
194 
204  template <class T>
206  {
207  if (!m_Disabled)
208  {
209  std::locale C("C");
210  std::locale originalLocale = m_Stream.getloc();
211  m_Stream.imbue(C);
212 
213  m_Stream << data;
214 
215  m_Stream.imbue(originalLocale);
216  }
217 
218  return *this;
219  }
220 
229  PseudoLogStream& operator<<(std::ostream& (*func)(std::ostream&))
230  {
231  if (!m_Disabled)
232  {
233  std::locale C("C");
234  std::locale originalLocale = m_Stream.getloc();
235  m_Stream.imbue(C);
236 
237  m_Stream << func;
238 
239  m_Stream.imbue(originalLocale);
240  }
241 
242  return *this;
243  }
244 
262  PseudoLogStream& operator()(const std::string& category)
263  {
264  if (!m_Disabled)
265  {
266  if (m_Message.Category.length())
267  m_Message.Category += ".";
268 
269  m_Message.Category += category;
270  }
271 
272  return *this;
273  }
274 
291  {
292  m_Disabled |= !enabled;
293  return *this;
294  }
295 
296  protected:
299  std::stringstream m_Stream;
300  };
301 
315  {
316  public:
318  template <class T>
320  {
321  return *this;
322  }
323 
325  template <class T>
327  {
328  return *this;
329  }
330 
332  NullLogStream& operator<<(std::ostream &(*)(std::ostream &))
333  {
334  return *this;
335  }
336 
338  NullLogStream& operator()(const char*)
339  {
340  return *this;
341  }
342 
345  {
346  return *this;
347  }
348  };
349 }
350 
358 #define MITK_INFO mitk::PseudoLogStream(mitk::LogLevel::Info, __FILE__, __LINE__, __FUNCTION__)
359 
365 #define MITK_WARN mitk::PseudoLogStream(mitk::LogLevel::Warn, __FILE__, __LINE__, __FUNCTION__)
366 
372 #define MITK_ERROR mitk::PseudoLogStream(mitk::LogLevel::Error, __FILE__, __LINE__, __FUNCTION__)
373 
379 #define MITK_FATAL mitk::PseudoLogStream(mitk::LogLevel::Fatal, __FILE__, __LINE__, __FUNCTION__)
380 
381 #ifdef MITK_ENABLE_DEBUG_MESSAGES
388 #define MITK_DEBUG mitk::PseudoLogStream(mitk::LogLevel::Debug, __FILE__, __LINE__, __FUNCTION__)
389 #else
390 #define MITK_DEBUG true ? mitk::NullLogStream() : mitk::NullLogStream()
391 #endif
392 
393 #endif
#define MITKLOG_EXPORT
Abstract interface for log backends that can be registered in the MITK log mechanism.
OutputType
Classification of the output destination of a backend.
No-op log stream that silently discards all input.
Definition: mitkLog.h:315
NullLogStream & operator()(const char *)
No-op category operator.
Definition: mitkLog.h:338
NullLogStream & operator()(bool)
No-op enable/disable operator.
Definition: mitkLog.h:344
NullLogStream & operator<<(T &)
No-op stream operator (non-const reference overload).
Definition: mitkLog.h:326
NullLogStream & operator<<(const T &)
No-op stream operator (const reference overload).
Definition: mitkLog.h:319
NullLogStream & operator<<(std::ostream &(*)(std::ostream &))
No-op stream manipulator operator.
Definition: mitkLog.h:332
Stream-based log message builder for the MITK log mechanism.
Definition: mitkLog.h:137
PseudoLogStream & operator()(const std::string &category)
Set or append a category for this log message.
Definition: mitkLog.h:262
~PseudoLogStream()
Destructor that distributes the assembled log message to all backends.
Definition: mitkLog.h:159
PseudoLogStream & operator<<(std::ostream &(*func)(std::ostream &))
Stream manipulator overload (e.g. std::endl, std::flush).
Definition: mitkLog.h:229
std::stringstream m_Stream
Definition: mitkLog.h:299
PseudoLogStream & operator()(bool enabled)
Conditionally enable or disable this log stream.
Definition: mitkLog.h:290
PseudoLogStream & operator<<(const T &data)
Stream data into the log message (const reference overload).
Definition: mitkLog.h:179
LogMessage m_Message
Definition: mitkLog.h:298
PseudoLogStream(LogLevel level, const std::string &filePath, int lineNumber, const std::string &functionName)
Construct a log stream for a message at the given level and source location.
Definition: mitkLog.h:146
PseudoLogStream & operator<<(T &data)
Stream data into the log message (non-const reference overload).
Definition: mitkLog.h:205
#define MITKLOG_MODULENAME
Definition: mitkLog.h:28
Find image slices visible on a given plane.
LogLevel
Severity levels for log messages in the MITK log mechanism.
Definition: mitkLogLevel.h:27
void MITKLOG_EXPORT UnregisterBackend(LogBackendBase *backend)
Unregister a previously registered log backend.
void MITKLOG_EXPORT EnableBackends(LogBackendBase::OutputType type)
Enable all backends of the given output type.
void MITKLOG_EXPORT DisableBackends(LogBackendBase::OutputType type)
Disable all backends of the given output type.
void MITKLOG_EXPORT DistributeToBackends(LogMessage &message)
Distribute a log message to all registered and enabled backends.
bool MITKLOG_EXPORT IsBackendEnabled(LogBackendBase::OutputType type)
Check whether backends of the given output type are enabled.
void MITKLOG_EXPORT RegisterBackend(LogBackendBase *backend)
Register a log backend in the MITK log mechanism.
A single log message (log event) of the MITK log mechanism.