CustusX  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxMessageListener.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 #include "cxMessageListener.h"
33 #include <iostream>
34 #include <QThread>
36 #include "cxLogFileWatcher.h"
37 #include "cxNullDeleter.h"
38 #include "cxReporter.h"
39 #include <QApplication>
40 
41 namespace cx
42 {
43 
45 {
46  return MessageListenerPtr(new MessageListener(log));
47 }
48 
50 {
51  MessageListenerPtr retval(new MessageListener(log));
52  retval->setMessageQueueMaxSize(size);
53  return retval;
54 }
55 
57 {
58  MessageListenerPtr retval(new MessageListener(this->mManager));
59  retval->mMessages = this->mMessages;
60  retval->mManager = this->mManager;
61  retval->mMessageHistoryMaxSize = this->mMessageHistoryMaxSize;
62  return retval;
63 }
64 
65 
66 MessageListener::MessageListener(LogPtr log) :
67  mManager(log),
68  mMessageHistoryMaxSize(0)
69 {
70  mManager = log;
71  if (!mManager)
72  mManager = reporter();
73 
74  mObserver.reset(new MessageObserver());
75  connect(mObserver.get(), &MessageObserver::newMessage, this, &MessageListener::messageReceived);
76  connect(mObserver.get(), &MessageObserver::newChannel, this, &MessageListener::newChannel);
77 
78  mManager->installObserver(mObserver, false);
79 }
80 
82 {
83  if (mManager)
84  {
85  mManager->uninstallObserver(mObserver);
86  }
87 }
88 
89 void MessageListener::messageReceived(Message message)
90 {
91  mMessages.push_back(message);
92  this->limitQueueSize();
93  emit newMessage(message);
94 }
95 
96 void MessageListener::limitQueueSize()
97 {
98  if (mMessageHistoryMaxSize<0)
99  return;
100 
101  while (mMessages.size() > mMessageHistoryMaxSize)
102  {
103  mMessages.pop_front();
104  }
105 }
106 
107 bool MessageListener::isError(MESSAGE_LEVEL level) const
108 {
109  return ((level==mlERROR )||( level==mlCERR ));
110 }
111 
113 {
114  QApplication::processEvents();
115 
116  for (QList<Message>::const_iterator i=mMessages.begin(); i!=mMessages.end(); ++i)
117  {
118  if (this->isError(i->getMessageLevel()))
119  return true;
120  }
121  return false;
122 }
123 
124 bool MessageListener::containsText(const QString text) const
125 {
126  QApplication::processEvents();
127  for (QList<Message>::const_iterator i=mMessages.begin(); i!=mMessages.end(); ++i)
128  {
129  QString message = i->getText();
130  if(i->getText().contains(text, Qt::CaseInsensitive))
131  return true;
132  }
133  return false;
134 }
135 
137 {
138  mManager->installObserver(mObserver, true);
139 }
140 
142 {
143  mObserver->installFilter(filter);
144  mManager->installObserver(mObserver, false);
145 }
146 
148 {
149  mMessageHistoryMaxSize = count;
150 }
151 
153 {
154  return mMessageHistoryMaxSize;
155 }
156 
157 QList<Message> MessageListener::getMessages() const
158 {
159  return mMessages;
160 }
161 
162 
163 } // namespace cx
164 
static MessageListenerPtr create(LogPtr log=LogPtr())
int getMessageQueueMaxSize() const
ReporterPtr reporter()
Definition: cxReporter.cpp:59
void newChannel(QString channel)
void installFilter(MessageFilterPtr)
MessageListenerPtr clone()
mlCERR
Definition: cxDefinitions.h:82
QList< Message > getMessages() const
QString getText() const
The raw message.
bool containsText(const QString text) const
boost::shared_ptr< class MessageFilter > MessageFilterPtr
boost::shared_ptr< class Log > LogPtr
Definition: cxLog.h:68
void newChannel(QString channel)
static MessageListenerPtr createWithQueue(LogPtr log=LogPtr(), int size=1000)
bool containsErrors() const
void setMessageQueueMaxSize(int count)
void newMessage(Message message)
mlERROR
Definition: cxDefinitions.h:82
boost::shared_ptr< class MessageListener > MessageListenerPtr
Definition: cxLog.h:66
void newMessage(Message message)