Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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 (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 #include <list>
14 #include <set>
15 
16 #include "mbilog.h"
17 
18 static std::list<mbilog::BackendBase *> backends;
19 static std::set<mbilog::OutputType> disabledBackendTypes;
20 
21 namespace mbilog
22 {
23  static const std::string NA_STRING = "n/a";
24 }
25 
27 {
28  backends.push_back(backend);
29 }
30 
32 {
33  backends.remove(backend);
34 }
35 
37 {
38  // Crop Message
39  {
40  std::string::size_type i = l.message.find_last_not_of(" \t\f\v\n\r");
41  l.message = (i != std::string::npos) ? l.message.substr(0, i + 1) : "";
42  }
43 
44  // create dummy backend if there is no backend registered (so we have an output anyway)
45  static mbilog::BackendCout *dummyBackend = nullptr;
46 
47  if (backends.empty() && (dummyBackend == nullptr))
48  {
49  dummyBackend = new mbilog::BackendCout();
50  dummyBackend->SetFull(false);
51  RegisterBackend(dummyBackend);
52  }
53  else if ((backends.size() > 1) && (dummyBackend != nullptr))
54  {
55  // if there was added another backend remove the dummy backend and delete it
56  UnregisterBackend(dummyBackend);
57  delete dummyBackend;
58  dummyBackend = nullptr;
59  }
60 
61  // iterate through all registered images and call the ProcessMessage() methods of the backends
62  std::list<mbilog::BackendBase *>::iterator i;
63  for (i = backends.begin(); i != backends.end(); i++)
64  {
65  if (IsBackendEnabled((*i)->GetOutputType()))
66  (*i)->ProcessMessage(l);
67  }
68 }
69 
70 void mbilog::EnableBackends(OutputType type)
71 {
72  disabledBackendTypes.erase(type);
73 }
74 
75 void mbilog::DisableBackends(OutputType type)
76 {
77  disabledBackendTypes.insert(type);
78 }
79 
80 bool mbilog::IsBackendEnabled(OutputType type)
81 {
82  return disabledBackendTypes.find(type) == disabledBackendTypes.end();
83 }
static std::set< mbilog::OutputType > disabledBackendTypes
Definition: mbilog.cpp:19
static const std::string NA_STRING
Definition: mbilog.cpp:23
void MBILOG_EXPORT UnregisterBackend(BackendBase *backend)
Unregisters a backend.
Definition: mbilog.cpp:31
void MBILOG_EXPORT EnableBackends(OutputType type)
Definition: mbilog.cpp:70
void MBILOG_EXPORT DisableBackends(OutputType type)
Definition: mbilog.cpp:75
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:36
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:80
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:26
static std::list< mbilog::BackendBase * > backends
Definition: mbilog.cpp:18
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...