Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mbilog.cpp
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 #include <list>
18 #include <set>
19 
20 #include "mbilog.h"
21 
22 static std::list<mbilog::BackendBase *> backends;
23 static std::set<mbilog::OutputType> disabledBackendTypes;
24 
25 namespace mbilog
26 {
27  static const std::string NA_STRING = "n/a";
28 }
29 
31 {
32  backends.push_back(backend);
33 }
34 
36 {
37  backends.remove(backend);
38 }
39 
41 {
42  // Crop Message
43  {
44  std::string::size_type i = l.message.find_last_not_of(" \t\f\v\n\r");
45  l.message = (i != std::string::npos) ? l.message.substr(0, i + 1) : "";
46  }
47 
48  // create dummy backend if there is no backend registered (so we have an output anyway)
49  static mbilog::BackendCout *dummyBackend = nullptr;
50 
51  if (backends.empty() && (dummyBackend == nullptr))
52  {
53  dummyBackend = new mbilog::BackendCout();
54  dummyBackend->SetFull(false);
55  RegisterBackend(dummyBackend);
56  }
57  else if ((backends.size() > 1) && (dummyBackend != nullptr))
58  {
59  // if there was added another backend remove the dummy backend and delete it
60  UnregisterBackend(dummyBackend);
61  delete dummyBackend;
62  dummyBackend = nullptr;
63  }
64 
65  // iterate through all registered images and call the ProcessMessage() methods of the backends
66  std::list<mbilog::BackendBase *>::iterator i;
67  for (i = backends.begin(); i != backends.end(); i++)
68  {
69  if (IsBackendEnabled((*i)->GetOutputType()))
70  (*i)->ProcessMessage(l);
71  }
72 }
73 
74 void mbilog::EnableBackends(OutputType type)
75 {
76  disabledBackendTypes.erase(type);
77 }
78 
79 void mbilog::DisableBackends(OutputType type)
80 {
81  disabledBackendTypes.insert(type);
82 }
83 
84 bool mbilog::IsBackendEnabled(OutputType type)
85 {
86  return disabledBackendTypes.find(type) == disabledBackendTypes.end();
87 }
static std::set< mbilog::OutputType > disabledBackendTypes
Definition: mbilog.cpp:23
static const std::string NA_STRING
Definition: mbilog.cpp:27
void MBILOG_EXPORT UnregisterBackend(BackendBase *backend)
Unregisters a backend.
Definition: mbilog.cpp:35
void MBILOG_EXPORT EnableBackends(OutputType type)
Definition: mbilog.cpp:74
void MBILOG_EXPORT DisableBackends(OutputType type)
Definition: mbilog.cpp:79
Default backend of the mbi logging mechanism. This backend is used if no other backend is registered...
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...
bool MBILOG_EXPORT IsBackendEnabled(OutputType type)
Definition: mbilog.cpp:84
void SetFull(bool full)
Sets the formatting mode. If true long messages will be displayed. Default is false (short/smart mess...
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
static std::list< mbilog::BackendBase * > backends
Definition: mbilog.cpp:22
virtual void ProcessMessage(const mbilog::LogMessage &l) override
This method is called by the mbi logging mechanism if the object is registered in the mbi logging mec...
This class is an interface for logging backends that can be registered in the mbi logging mechanism...