NorMIT-nav  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);
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
cx::Log::Log
Log()
Definition: cxLog.cpp:53
cxLogger.h
cxMessageListener.h
cx::LogThreadPtr
boost::shared_ptr< class LogThread > LogThreadPtr
Definition: cxLog.h:46
cx::Log::createWorker
virtual LogThreadPtr createWorker()=0
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::Log::initializeObject
void initializeObject()
Definition: cxLog.cpp:71
cx::Log::mLogPath
QString mLogPath
Definition: cxLog.h:85
cx::Log::mWorker
LogThreadPtr mWorker
Definition: cxLog.h:87
cx::EventProcessingThread::~EventProcessingThread
virtual ~EventProcessingThread()
Definition: cxLog.cpp:40
cx::Log::stopThread
void stopThread()
Definition: cxLog.cpp:97
cxLogThread.h
cxProfile.h
cx::MessageObserverPtr
boost::shared_ptr< class MessageObserver > MessageObserverPtr
Definition: cxLog.h:44
cxTypeConversions.h
cx::ProfileManager::getSettingsPath
QString getSettingsPath()
Definition: cxProfile.cpp:219
cx::Log::startThread
void startThread()
Definition: cxLog.cpp:80
cx::Log::uninstallObserver
void uninstallObserver(MessageObserverPtr observer)
Definition: cxLog.cpp:132
cx::Log::loggingFolderChanged
void loggingFolderChanged()
cx::Log::getDefaultLogPath
QString getDefaultLogPath() const
Definition: cxLog.cpp:63
cx::Log::installObserver
void installObserver(MessageObserverPtr observer, bool resend)
Definition: cxLog.cpp:126
cx::Log::onEmittedMessage
virtual void onEmittedMessage(Message message)
Definition: cxLog.h:75
cx::Log::mThread
boost::shared_ptr< class QThread > mThread
Definition: cxLog.h:86
cx::EventProcessingThread::EventProcessingThread
EventProcessingThread()
Definition: cxLog.cpp:37
cx::EventProcessingThread::run
virtual void run()
Definition: cxLog.cpp:45
cxTime.h
cx::Log::getLoggingFolder
QString getLoggingFolder() const
Definition: cxLog.cpp:121
cx::LogThread::emittedMessage
void emittedMessage(Message message)
emitted for each new message, in addition to writing to observer.
cx::Log::setLoggingFolder
void setLoggingFolder(QString absoluteLoggingFolderPath)
Definition: cxLog.cpp:113
cx::ProfileManager::getInstance
static ProfileManager * getInstance(QString defaultProfile=QString("Laboratory"))
returns the only instance of this class
Definition: cxProfile.cpp:167
cxLog.h
cx::Log::~Log
virtual ~Log()
Definition: cxLog.cpp:58