Fraxinus  16.5.0-fx-rc3
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
34 #include "cxMessageListener.h"
35 #include <iostream>
36 #include <QThread>
37 #include "cxLogMessageFilter.h"
38 
39 namespace cx
40 {
41 
43 {
44  QMutexLocker locker(&mMutex);
45  if (!mChannels.contains(message.mChannel))
46  {
47  mChannels.append(message.mChannel);
48  locker.unlock();
49  emit newChannel(message.mChannel);
50  locker.relock();
51  }
52 
53  if (this->testFilter(message))
54  {
55  locker.unlock();
56  emit newMessage(message);
57  locker.relock();
58  }
59 }
60 
61 bool MessageObserver::testFilter(const Message &msg) const
62 {
63  if (!mFilter)
64  return true; // always succeed with no filter.
65  return (*mFilter)(msg);
66 }
67 
69 {
70  QMutexLocker locker(&mMutex);
71  // Clone to ensure filter is standalone
72  // and safely can be passed to the reporter thread.
73  mFilter = filter->clone();
74 }
75 
76 
80 
81 
83 {
85 }
86 
87 MessageRepository::MessageRepository() :
88  mMessageHistoryMaxSize(3000)
89 {
90 }
91 
93 {
94 }
95 
97 {
98  mMessages.push_back(message);
99  this->limitQueueSize();
100  this->emitThroughFilter(message);
101 }
102 
103 void MessageRepository::limitQueueSize()
104 {
105  if (mMessageHistoryMaxSize<0)
106  return;
107 
108  while (mMessages.size() > mMessageHistoryMaxSize)
109  mMessages.pop_front();
110 }
111 
112 void MessageRepository::emitThroughFilter(const Message& message)
113 {
114  for (unsigned i=0; i<mObservers.size(); ++i)
115  {
116  mObservers[i]->sendMessage(message);
117  }
118 }
119 
121 {
122  if(!this->exists(observer))
123  mObservers.push_back(observer);
124 
125  if (resend)
126  {
127  for (unsigned i = 0; i < mMessages.size(); ++i)
128  observer->sendMessage(mMessages[i]);
129 
130  }
131 }
132 
133 bool MessageRepository::exists(MessageObserverPtr observer)
134 {
135  if (std::count(mObservers.begin(), mObservers.end(), observer))
136  return true;
137  return false;
138 }
139 
141 {
142  if (!this->exists(observer))
143  return;
144  mObservers.erase(std::find(mObservers.begin(), mObservers.end(), observer));
145 }
146 
148 {
149  mMessageHistoryMaxSize = count;
150  this->limitQueueSize();
151 }
152 
154 {
155  return mMessageHistoryMaxSize;
156 }
157 
159 {
160  mMessages.clear();
161 }
162 
163 
164 
165 } // namespace cx
166 
void sendMessage(const Message &message)
void uninstall(MessageObserverPtr observer)
static MessageRepositoryPtr create()
boost::shared_ptr< class MessageFilter > MessageFilterPtr
QString mChannel
Definition: cxLogMessage.h:95
void newChannel(QString channel)
void installFilter(MessageFilterPtr filter)
void install(MessageObserverPtr observer, bool resend)
boost::shared_ptr< class MessageObserver > MessageObserverPtr
Definition: cxLog.h:65
boost::shared_ptr< class MessageRepository > MessageRepositoryPtr
Definition: cxLogThread.h:71
void newMessage(Message message)