Fraxinus  2023.01.05-dev+develop.0da12
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"
30 #include "QApplication"
31 
32 namespace cx
33 {
34 
35 class EventProcessingThread : public QThread
36 {
37 public:
39  {
40  }
42  {
43 
44  }
45 
46  virtual void run()
47  {
48  this->exec();
49  qApp->processEvents(); // exec() docs doesn't guarantee that the posted events are processed. - do that here.
50  }
51 };
52 
53 
55 {
56  mLogPath = this->getDefaultLogPath();
57 }
58 
60 {
61  this->stopThread();
62 }
63 
64 QString Log::getDefaultLogPath() const
65 {
66  QString isoDateFormat("yyyy-MM-dd");
67  QString isoDate = QDateTime::currentDateTime().toString(isoDateFormat);
68  QString retval = ProfileManager::getInstance()->getSettingsPath()+"/Logs/"+isoDate;
69  return retval;
70 }
71 
73 {
74  if (mThread)
75  return;
76 
77  this->stopThread();
78  this->startThread();
79 }
80 
82 {
83  if (mThread)
84  return;
85 
86  mThread.reset(new EventProcessingThread());
87  mThread->setObjectName("org.custusx.resource.core.logger");
88 
89  mWorker = this->createWorker();
90  mWorker->moveToThread(mThread.get());
91  if (!mLogPath.isEmpty())
92  mWorker->setLoggingFolder(mLogPath);
93  connect(mWorker.get(), &LogThread::emittedMessage, this, &Log::onEmittedMessage);
94 
95  mThread->start();
96 }
97 
99 {
100  if (!mThread)
101  return;
102 
103  disconnect(mWorker.get(), &LogThread::emittedMessage, this, &Log::onEmittedMessage);
104  LogThreadPtr tempWorker = mWorker;
105  mWorker.reset();
106 
107  mThread->quit();
108  mThread->wait(); // forever or until dead thread
109 
110  mThread.reset();
111  tempWorker.reset();
112 }
113 
114 void Log::setLoggingFolder(QString absoluteLoggingFolderPath)
115 {
116  mLogPath = absoluteLoggingFolderPath;
117  if (mWorker)
118  mWorker->setLoggingFolder(mLogPath);
119  emit loggingFolderChanged();
120 }
121 
122 QString Log::getLoggingFolder() const
123 {
124  return mLogPath;
125 }
126 
127 void Log::installObserver(MessageObserverPtr observer, bool resend)
128 {
129  if (mWorker)
130  mWorker->installObserver(observer, resend);
131 }
132 
134 {
135  if (mWorker)
136  mWorker->uninstallObserver(observer);
137 }
138 
140 {
141  ReporterThreadPtr reporterThread = boost::dynamic_pointer_cast<ReporterThread>(mWorker);
142  if (reporterThread)
143  reporterThread->stopQtMessages();
144  else
145  CX_LOG_WARNING() << "Found no reporterThread";
146 }
148 {
149  ReporterThreadPtr reporterThread = boost::dynamic_pointer_cast<ReporterThread>(mWorker);
150  if (reporterThread)
151  reporterThread->startQtMessages();
152 }
153 
154 
155 } //End namespace cx
virtual ~EventProcessingThread()
Definition: cxLog.cpp:41
boost::shared_ptr< class ReporterThread > ReporterThreadPtr
Definition: cxLog.h:47
virtual void onEmittedMessage(Message message)
Definition: cxLog.h:78
void stopThread()
Definition: cxLog.cpp:98
void setLoggingFolder(QString absoluteLoggingFolderPath)
Definition: cxLog.cpp:114
void startQtMessages()
Definition: cxLog.cpp:147
QString getDefaultLogPath() const
Definition: cxLog.cpp:64
void startThread()
Definition: cxLog.cpp:81
static ProfileManager * getInstance(QString defaultProfile=QString("Laboratory"))
returns the only instance of this class
Definition: cxProfile.cpp:167
virtual void run()
Definition: cxLog.cpp:46
void installObserver(MessageObserverPtr observer, bool resend)
Definition: cxLog.cpp:127
void initializeObject()
Definition: cxLog.cpp:72
QString getLoggingFolder() const
Definition: cxLog.cpp:122
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:59
boost::shared_ptr< class LogThread > LogThreadPtr
Definition: cxLog.h:46
Log()
Definition: cxLog.cpp:54
#define CX_LOG_WARNING
Definition: cxLogger.h:98
QString getSettingsPath()
Definition: cxProfile.cpp:220
void stopQtMessages()
Definition: cxLog.cpp:139
void uninstallObserver(MessageObserverPtr observer)
Definition: cxLog.cpp:133
Namespace for all CustusX production code.