Fraxinus  22.09
An IGT application
cxLog.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 "cxLog.h"
13 
14 #include "cxLogger.h"
15 #include <QtGlobal>
16 #include <QThread>
17 #include <iostream>
18 #include "boost/shared_ptr.hpp"
19 #include <QString>
20 #include <QMutex>
21 #include <QSound>
22 #include <QDir>
23 #include <QTextStream>
24 #include "cxTypeConversions.h"
25 #include "cxTime.h"
26 #include "cxProfile.h"
27 #include "cxMessageListener.h"
28 #include "internal/cxLogThread.h"
29 #include "QApplication"
30 
31 namespace cx
32 {
33 
34 class EventProcessingThread : public QThread
35 {
36 public:
38  {
39  }
41  {
42 
43  }
44 
45  virtual void run()
46  {
47  this->exec();
48  qApp->processEvents(); // exec() docs doesn't guarantee that the posted events are processed. - do that here.
49  }
50 };
51 
52 
54 {
55  mLogPath = this->getDefaultLogPath();
56 }
57 
59 {
60  this->stopThread();
61 }
62 
63 QString Log::getDefaultLogPath() const
64 {
65  QString isoDateFormat("yyyy-MM-dd");
66  QString isoDate = QDateTime::currentDateTime().toString(isoDateFormat);
67  QString retval = ProfileManager::getInstance()->getSettingsPath()+"/Logs/"+isoDate;
68  return retval;
69 }
70 
72 {
73  if (mThread)
74  return;
75 
76  this->stopThread();
77  this->startThread();
78 }
79 
81 {
82  if (mThread)
83  return;
84 
85  mThread.reset(new EventProcessingThread());
86  mThread->setObjectName("org.custusx.resource.core.logger");
87 
88  mWorker = this->createWorker();
89  mWorker->moveToThread(mThread.get());
90  if (!mLogPath.isEmpty())
91  mWorker->setLoggingFolder(mLogPath);
92  connect(mWorker.get(), &LogThread::emittedMessage, this, &Log::onEmittedMessage);
93 
94  mThread->start();
95 }
96 
98 {
99  if (!mThread)
100  return;
101 
102  disconnect(mWorker.get(), &LogThread::emittedMessage, this, &Log::onEmittedMessage);
103  LogThreadPtr tempWorker = mWorker;
104  mWorker.reset();
105 
106  mThread->quit();
107  mThread->wait(); // forever or until dead thread
108 
109  mThread.reset();
110  tempWorker.reset();
111 }
112 
113 void Log::setLoggingFolder(QString absoluteLoggingFolderPath)
114 {
115  mLogPath = absoluteLoggingFolderPath;
116  if (mWorker)
117  mWorker->setLoggingFolder(mLogPath);
118  emit loggingFolderChanged();
119 }
120 
121 QString Log::getLoggingFolder() const
122 {
123  return mLogPath;
124 }
125 
126 void Log::installObserver(MessageObserverPtr observer, bool resend)
127 {
128  if (mWorker)
129  mWorker->installObserver(observer, resend);
130 }
131 
133 {
134  if (mWorker)
135  mWorker->uninstallObserver(observer);
136 }
137 
138 
139 } //End namespace cx
virtual ~EventProcessingThread()
Definition: cxLog.cpp:40
virtual void onEmittedMessage(Message message)
Definition: cxLog.h:75
void stopThread()
Definition: cxLog.cpp:97
void setLoggingFolder(QString absoluteLoggingFolderPath)
Definition: cxLog.cpp:113
QString getDefaultLogPath() const
Definition: cxLog.cpp:63
void startThread()
Definition: cxLog.cpp:80
static ProfileManager * getInstance(QString defaultProfile=QString("Laboratory"))
returns the only instance of this class
Definition: cxProfile.cpp:167
virtual void run()
Definition: cxLog.cpp:45
void installObserver(MessageObserverPtr observer, bool resend)
Definition: cxLog.cpp:126
void initializeObject()
Definition: cxLog.cpp:71
QString getLoggingFolder() const
Definition: cxLog.cpp:121
void emittedMessage(Message message)
emitted for each new message, in addition to writing to observer.
boost::shared_ptr< class MessageObserver > MessageObserverPtr
Definition: cxLog.h:44
virtual ~Log()
Definition: cxLog.cpp:58
boost::shared_ptr< class LogThread > LogThreadPtr
Definition: cxLog.h:46
Log()
Definition: cxLog.cpp:53
QString getSettingsPath()
Definition: cxProfile.cpp:219
void uninstallObserver(MessageObserverPtr observer)
Definition: cxLog.cpp:132
Namespace for all CustusX production code.