CustusX  2023.01.05-dev+develop.0da12
An IGT application
cxReporterMessageRepository.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
13 #include "cxMessageListener.h"
14 #include <iostream>
15 #include <QThread>
16 #include "cxLogMessageFilter.h"
17 
18 namespace cx
19 {
20 
22 {
23  QMutexLocker locker(&mMutex);
24  if (!mChannels.contains(message.mChannel))
25  {
26  mChannels.append(message.mChannel);
27  locker.unlock();
28  emit newChannel(message.mChannel);
29  locker.relock();
30  }
31 
32  if (this->testFilter(message))
33  {
34  locker.unlock();
35  emit newMessage(message);
36  locker.relock();
37  }
38 }
39 
40 bool MessageObserver::testFilter(const Message &msg) const
41 {
42  if (!mFilter)
43  return true; // always succeed with no filter.
44  return (*mFilter)(msg);
45 }
46 
48 {
49  QMutexLocker locker(&mMutex);
50  // Clone to ensure filter is standalone
51  // and safely can be passed to the reporter thread.
52  mFilter = filter->clone();
53 }
54 
55 
59 
60 
62 {
64 }
65 
66 MessageRepository::MessageRepository() :
67  mMessageHistoryMaxSize(3000)
68 {
69 }
70 
72 {
73 }
74 
76 {
77  mMessages.push_back(message);
78  this->limitQueueSize();
79  this->emitThroughFilter(message);
80 }
81 
82 void MessageRepository::limitQueueSize()
83 {
84  if (mMessageHistoryMaxSize<0)
85  return;
86 
87  while (mMessages.size() > mMessageHistoryMaxSize)
88  mMessages.pop_front();
89 }
90 
91 void MessageRepository::emitThroughFilter(const Message& message)
92 {
93  for (unsigned i=0; i<mObservers.size(); ++i)
94  {
95  mObservers[i]->sendMessage(message);
96  }
97 }
98 
100 {
101  if(!this->exists(observer))
102  mObservers.push_back(observer);
103 
104  if (resend)
105  {
106  for (unsigned i = 0; i < mMessages.size(); ++i)
107  observer->sendMessage(mMessages[i]);
108 
109  }
110 }
111 
112 bool MessageRepository::exists(MessageObserverPtr observer)
113 {
114  if (std::count(mObservers.begin(), mObservers.end(), observer))
115  return true;
116  return false;
117 }
118 
120 {
121  if (!this->exists(observer))
122  return;
123  mObservers.erase(std::find(mObservers.begin(), mObservers.end(), observer));
124 }
125 
127 {
128  mMessageHistoryMaxSize = count;
129  this->limitQueueSize();
130 }
131 
133 {
134  return mMessageHistoryMaxSize;
135 }
136 
138 {
139  mMessages.clear();
140 }
141 
142 
143 
144 } // namespace cx
145 
void sendMessage(const Message &message)
void uninstall(MessageObserverPtr observer)
static MessageRepositoryPtr create()
boost::shared_ptr< class MessageFilter > MessageFilterPtr
QString mChannel
Definition: cxLogMessage.h:74
void newChannel(QString channel)
void installFilter(MessageFilterPtr filter)
void install(MessageObserverPtr observer, bool resend)
boost::shared_ptr< class MessageObserver > MessageObserverPtr
Definition: cxLog.h:44
boost::shared_ptr< class MessageRepository > MessageRepositoryPtr
Definition: cxLogThread.h:50
Namespace for all CustusX production code.
void newMessage(Message message)