CustusX  15.4.0-beta
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  if (!mChannels.contains(message.mChannel))
45  {
46  mChannels.append(message.mChannel);
47  emit newChannel(message.mChannel);
48  }
49 
50  if (this->testFilter(message))
51  {
52  emit newMessage(message);
53  }
54 }
55 
56 bool MessageObserver::testFilter(const Message &msg) const
57 {
58  if (!mFilter)
59  return true; // always succeed with no filter.
60  return (*mFilter)(msg);
61 }
62 
64 {
65  // Clone to ensure filter is standalone
66  // and safely can be passed to the reporter thread.
67  mFilter = filter->clone();
68 }
69 
70 
74 
75 
77 {
79 }
80 
81 MessageRepository::MessageRepository() :
82  mMessageHistoryMaxSize(3000)
83 {
84 }
85 
87 {
88 }
89 
91 {
92  mMessages.push_back(message);
93  this->limitQueueSize();
94  this->emitThroughFilter(message);
95 }
96 
97 void MessageRepository::limitQueueSize()
98 {
99  if (mMessageHistoryMaxSize<0)
100  return;
101 
102  while (mMessages.size() > mMessageHistoryMaxSize)
103  mMessages.pop_front();
104 }
105 
106 void MessageRepository::emitThroughFilter(const Message& message)
107 {
108  for (unsigned i=0; i<mObservers.size(); ++i)
109  {
110  mObservers[i]->sendMessage(message);
111  }
112 }
113 
115 {
116  this->uninstall(observer);
117 
118  mObservers.push_back(observer);
119 
120  if (resend)
121  {
122  for (QList<Message>::iterator i=mMessages.begin(); i!=mMessages.end(); ++i)
123  observer->sendMessage(*i);
124  }
125 }
126 
128 {
129  if (!std::count(mObservers.begin(), mObservers.end(), observer))
130  return;
131  mObservers.erase(std::find(mObservers.begin(), mObservers.end(), observer));
132 }
133 
135 {
136  mMessageHistoryMaxSize = count;
137  this->limitQueueSize();
138 }
139 
141 {
142  return mMessageHistoryMaxSize;
143 }
144 
146 {
147  mMessages.clear();
148 }
149 
150 
151 
152 } // namespace cx
153 
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:70
void newMessage(Message message)