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