NorMIT-nav  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);
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
cx::Log::Log
Log()
Definition: cxLog.cpp:54
cxLogger.h
cxMessageListener.h
cx::LogThreadPtr
boost::shared_ptr< class LogThread > LogThreadPtr
Definition: cxLog.h:46
cx::ReporterThreadPtr
boost::shared_ptr< class ReporterThread > ReporterThreadPtr
Definition: cxLog.h:47
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:72
cxReporterThread.h
cx::Log::mLogPath
QString mLogPath
Definition: cxLog.h:88
cx::Log::mWorker
LogThreadPtr mWorker
Definition: cxLog.h:90
cx::EventProcessingThread::~EventProcessingThread
virtual ~EventProcessingThread()
Definition: cxLog.cpp:41
cx::Log::stopThread
void stopThread()
Definition: cxLog.cpp:98
cxLogThread.h
cx::Log::startQtMessages
void startQtMessages()
Definition: cxLog.cpp:147
cxProfile.h
cx::MessageObserverPtr
boost::shared_ptr< class MessageObserver > MessageObserverPtr
Definition: cxLog.h:44
cxTypeConversions.h
cx::ProfileManager::getSettingsPath
QString getSettingsPath()
Definition: cxProfile.cpp:220
cx::Log::startThread
void startThread()
Definition: cxLog.cpp:81
cx::Log::uninstallObserver
void uninstallObserver(MessageObserverPtr observer)
Definition: cxLog.cpp:133
cx::Log::loggingFolderChanged
void loggingFolderChanged()
cx::Log::stopQtMessages
void stopQtMessages()
Definition: cxLog.cpp:139
cx::Log::getDefaultLogPath
QString getDefaultLogPath() const
Definition: cxLog.cpp:64
cx::Log::installObserver
void installObserver(MessageObserverPtr observer, bool resend)
Definition: cxLog.cpp:127
cx::Log::onEmittedMessage
virtual void onEmittedMessage(Message message)
Definition: cxLog.h:78
cx::Log::mThread
boost::shared_ptr< class QThread > mThread
Definition: cxLog.h:89
cx::EventProcessingThread::EventProcessingThread
EventProcessingThread()
Definition: cxLog.cpp:38
cx::EventProcessingThread::run
virtual void run()
Definition: cxLog.cpp:46
cxTime.h
cx::Log::getLoggingFolder
QString getLoggingFolder() const
Definition: cxLog.cpp:122
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:114
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_WARNING
#define CX_LOG_WARNING
Definition: cxLogger.h:98
cx::Log::~Log
virtual ~Log()
Definition: cxLog.cpp:59