Fraxinus  16.5.0-fx-rc1
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxReporter.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 
33 #include "cxReporter.h"
34 #include "cxLogger.h"
35 #include <QtGlobal>
36 #include <iostream>
37 #include "boost/shared_ptr.hpp"
38 #include <QString>
39 #include <QMutex>
40 #include <QSound>
41 #include <QDir>
42 #include <QTextStream>
43 #include "cxTypeConversions.h"
44 #include "cxDefinitionStrings.h"
45 #include "cxTime.h"
46 #include "cxMessageListener.h"
47 
50 
51 namespace cx
52 {
53 
54 // --------------------------------------------------------
55 boost::weak_ptr<Reporter> Reporter::mWeakInstance;
56 boost::shared_ptr<Reporter> Reporter::mPersistentInstance;
57 // --------------------------------------------------------
58 
60 {
61  return Reporter::getInstance();
62 }
63 
64 Reporter::Reporter()
65 {
66 }
67 
69 {
70 }
71 
73 {
74  ReporterPtr retval = mWeakInstance.lock();
75  if (!retval)
76  {
77  retval.reset(new Reporter());
78  mWeakInstance = retval;
79  }
80  return retval;
81 }
82 
84 {
86 
87  mPersistentInstance = object;
88  object->initializeObject();
89 }
90 
92 {
93  return LogThreadPtr(new ReporterThread());
94 }
95 
97 {
98  mPersistentInstance.reset();
99 }
100 
101 void Reporter::onEmittedMessage(Message message)
102 {
103  if (!message.mMuted)
104  this->playSound(message.getMessageLevel());
105 }
106 
108 {
109  mAudioSource = audioSource;
110 }
111 
112 bool Reporter::hasAudioSource() const
113 {
114  return mAudioSource ? true : false;
115 }
116 
117 void Reporter::sendInfo(QString info)
118 {
119  this->sendMessage(info, mlINFO);
120 }
121 
122 void Reporter::sendSuccess(QString success)
123 {
124  this->sendMessage(success, mlSUCCESS);
125 }
126 
127 void Reporter::sendWarning(QString warning)
128 {
129  this->sendMessage(warning, mlWARNING);
130 }
131 
132 void Reporter::sendError(QString error)
133 {
134  this->sendMessage(error, mlERROR);
135 }
136 
137 void Reporter::sendDebug(QString debug)
138 {
139  this->sendMessage(debug, mlDEBUG);
140 }
141 
142 void Reporter::sendVolatile(QString volatile_msg)
143 {
144  this->sendMessage(volatile_msg, mlVOLATILE);
145 }
146 
147 void Reporter::sendRaw(QString raw)
148 {
149  this->sendMessage(raw, mlRAW);
150 }
151 
152 void Reporter::sendMessage(QString text, MESSAGE_LEVEL messageLevel, int timeout, bool mute)
153 {
154  Message message(text, messageLevel, timeout);
155  message.mMuted = mute;
156  this->sendMessage(message);
157 }
158 
160 {
161  if (!mThread)
162  {
163  std::cout << message.getPrintableMessage() << std::endl;
164  return;
165  }
166 
167  if (mWorker)
168  mWorker->logMessage(message);
169 }
170 
171 void Reporter::playSound(MESSAGE_LEVEL messageLevel)
172 {
173  switch (messageLevel)
174  {
175  case mlSUCCESS:
176  this->playSuccessSound();
177  break;
178  case mlWARNING:
179  this->playWarningSound();
180  break;
181  case mlERROR:
182  this->playErrorSound();
183  break;
184  default:
185  break;
186  }
187 }
188 
190 {
191  if(this->hasAudioSource())
192  mAudioSource->playStartSound();
193 }
194 
196 {
197  if(this->hasAudioSource())
198  mAudioSource->playStopSound();
199 }
200 
202 {
203  if(this->hasAudioSource())
204  mAudioSource->playCancelSound();
205 }
206 
208 {
209  if(this->hasAudioSource())
210  mAudioSource->playSuccessSound();
211 }
212 
214 {
215  if(this->hasAudioSource())
216  mAudioSource->playWarningSound();
217 }
218 
220 {
221  if(this->hasAudioSource())
222  mAudioSource->playErrorSound();
223 }
224 
226 {
227  if(this->hasAudioSource())
228  mAudioSource->playScreenShotSound();
229 }
230 
232 {
233  if(this->hasAudioSource())
234  mAudioSource->playSampleSound();
235 }
236 
237 } //End namespace cx
void playScreenShotSound()
plays a sound signaling that a screen shot has been taken
Definition: cxReporter.cpp:225
mlSUCCESS
Definition: cxDefinitions.h:82
mlVOLATILE
Definition: cxDefinitions.h:82
mlRAW
Definition: cxDefinitions.h:82
ReporterPtr reporter()
Definition: cxReporter.cpp:59
void sendVolatile(QString volatile_msg)
Used to output volatile info that changes rapidly, not suited for logging.
Definition: cxReporter.cpp:142
QString getPrintableMessage() const
Text containing information appropriate to display.
static void shutdown()
shutdown service, destroy static object if none holds a reference.
Definition: cxReporter.cpp:96
virtual ~Reporter()
Definition: cxReporter.cpp:68
static void initialize()
Initialize logging, static object is guaranteed to exist at least until shutdown. ...
Definition: cxReporter.cpp:83
void playWarningSound()
automatically called by sendWarning if not muted
Definition: cxReporter.cpp:213
void playSuccessSound()
automatically called by sendSuccess if not muted
Definition: cxReporter.cpp:207
void sendInfo(QString info)
Used to report normal interesting activity, no sound associated.
Definition: cxReporter.cpp:117
boost::shared_ptr< class Reporter > ReporterPtr
Definition: cxReporter.h:67
boost::shared_ptr< class QThread > mThread
Definition: cxLog.h:107
void playErrorSound()
automatically called by sendError if not muted
Definition: cxReporter.cpp:219
void sendDebug(QString debug)
Used to output debug info, no sound associated.
Definition: cxReporter.cpp:137
mlDEBUG
Definition: cxDefinitions.h:82
void playCancelSound()
plays a sound signaling that something has been canceled
Definition: cxReporter.cpp:201
void sendWarning(QString warning)
The program does not need to terminate, but the user might need to do something, default not muted...
Definition: cxReporter.cpp:127
void playStartSound()
plays a sound signaling that something has started
Definition: cxReporter.cpp:189
void sendSuccess(QString success)
Used to report larger successful operations, default not muted.
Definition: cxReporter.cpp:122
LogThreadPtr mWorker
Definition: cxLog.h:108
boost::shared_ptr< class LogThread > LogThreadPtr
Definition: cxLog.h:67
virtual LogThreadPtr createWorker()
Definition: cxReporter.cpp:91
void sendError(QString error)
The program (might) need to terminate, default not muted.
Definition: cxReporter.cpp:132
mlINFO
Definition: cxDefinitions.h:82
void sendRaw(QString raw)
Used to output messages without adding anything to them, can be used as cout when mangling needs to b...
Definition: cxReporter.cpp:147
void playStopSound()
plays a sound signaling that something has stopped
Definition: cxReporter.cpp:195
mlWARNING
Definition: cxDefinitions.h:82
static ReporterPtr getInstance()
Returns a reference to the only Reporter that exists.
Definition: cxReporter.cpp:72
void sendMessage(QString text, MESSAGE_LEVEL messageLevel=mlDEBUG, int timeout=-1, bool mute=false)
Definition: cxReporter.cpp:152
boost::shared_ptr< Audio > AudioPtr
Definition: cxAudio.h:68
MESSAGE_LEVEL getMessageLevel() const
The category of the message.
mlERROR
Definition: cxDefinitions.h:82
void setAudioSource(AudioPtr audioSource)
define sounds to go with the messages.
Definition: cxReporter.cpp:107
void playSampleSound()
plays a sound signaling that something has been sampled
Definition: cxReporter.cpp:231