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