NorMIT-nav  22.09
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
cx::Reporter::sendDebug
void sendDebug(QString debug)
Used to output debug info, no sound associated.
Definition: cxReporter.cpp:114
cxLogger.h
cxMessageListener.h
cx::LogThreadPtr
boost::shared_ptr< class LogThread > LogThreadPtr
Definition: cxLog.h:46
cx::Reporter::playErrorSound
void playErrorSound()
automatically called by sendError if not muted
Definition: cxReporter.cpp:196
cx::Reporter::shutdown
static void shutdown()
shutdown service, destroy static object if none holds a reference.
Definition: cxReporter.cpp:73
cx::Reporter::playSuccessSound
void playSuccessSound()
automatically called by sendSuccess if not muted
Definition: cxReporter.cpp:184
mlDEBUG
mlDEBUG
Definition: cxDefinitions.h:70
cx::Message
Definition: cxLogMessage.h:54
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::Message::getMessageLevel
MESSAGE_LEVEL getMessageLevel() const
The category of the message.
Definition: cxLogMessage.cpp:84
cxReporterThread.h
cx::Reporter::setAudioSource
void setAudioSource(AudioPtr audioSource)
define sounds to go with the messages.
Definition: cxReporter.cpp:84
cxReporter.h
cx::reporter
ReporterPtr reporter()
Definition: cxReporter.cpp:36
cx::Reporter::sendVolatile
void sendVolatile(QString volatile_msg)
Used to output volatile info that changes rapidly, not suited for logging.
Definition: cxReporter.cpp:119
cx::Reporter::sendSuccess
void sendSuccess(QString success)
Used to report larger successful operations, default not muted.
Definition: cxReporter.cpp:99
cx::Reporter::playStopSound
void playStopSound()
plays a sound signaling that something has stopped
Definition: cxReporter.cpp:172
cx::Reporter::sendWarning
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
mlRAW
mlRAW
Definition: cxDefinitions.h:75
cx::Reporter::~Reporter
virtual ~Reporter()
Definition: cxReporter.cpp:45
cx::Log::mWorker
LogThreadPtr mWorker
Definition: cxLog.h:87
cx::ReporterPtr
boost::shared_ptr< class Reporter > ReporterPtr
Definition: cxReporter.h:46
cx::Reporter::playSampleSound
void playSampleSound()
plays a sound signaling that something has been sampled
Definition: cxReporter.cpp:208
cx::Message::getPrintableMessage
QString getPrintableMessage() const
Text containing information appropriate to display.
Definition: cxLogMessage.cpp:62
cx::Reporter::playCancelSound
void playCancelSound()
plays a sound signaling that something has been canceled
Definition: cxReporter.cpp:178
cx::Reporter::playScreenShotSound
void playScreenShotSound()
plays a sound signaling that a screen shot has been taken
Definition: cxReporter.cpp:202
cx::ReporterThread
Definition: cxReporterThread.h:55
cx::Reporter::sendError
void sendError(QString error)
The program (might) need to terminate, default not muted.
Definition: cxReporter.cpp:109
mlVOLATILE
mlVOLATILE
Definition: cxDefinitions.h:74
mlERROR
mlERROR
Definition: cxDefinitions.h:69
cx::Log::mThread
boost::shared_ptr< class QThread > mThread
Definition: cxLog.h:86
mlWARNING
mlWARNING
Definition: cxDefinitions.h:68
cxTime.h
cx::Reporter::sendInfo
void sendInfo(QString info)
Used to report normal interesting activity, no sound associated.
Definition: cxReporter.cpp:94
cx::Reporter::playStartSound
void playStartSound()
plays a sound signaling that something has started
Definition: cxReporter.cpp:166
cx::Reporter::getInstance
static ReporterPtr getInstance()
Returns a reference to the only Reporter that exists.
Definition: cxReporter.cpp:49
cx::AudioPtr
boost::shared_ptr< Audio > AudioPtr
Definition: cxAudio.h:47
mlSUCCESS
mlSUCCESS
Definition: cxDefinitions.h:73
cx::Reporter::sendMessage
void sendMessage(QString text, MESSAGE_LEVEL messageLevel=mlDEBUG, int timeout=-1, bool mute=false)
Definition: cxReporter.cpp:129
mlINFO
mlINFO
Definition: cxDefinitions.h:67
cx::Reporter::playWarningSound
void playWarningSound()
automatically called by sendWarning if not muted
Definition: cxReporter.cpp:190
cx::Reporter
Definition: cxReporter.h:63
cx::Reporter::sendRaw
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
cx::Reporter::createWorker
virtual LogThreadPtr createWorker()
Definition: cxReporter.cpp:68
cx::Message::mMuted
bool mMuted
Definition: cxLogMessage.h:72
cx::Reporter::initialize
static void initialize()
Initialize logging, static object is guaranteed to exist at least until shutdown.
Definition: cxReporter.cpp:60
cxLogFileWatcherThread.h