NorMIT-nav  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxLog.h"
34 
35 #include "cxLogger.h"
36 #include <QtGlobal>
37 #include <QThread>
38 #include <iostream>
39 #include "boost/shared_ptr.hpp"
40 #include <QString>
41 #include <QMutex>
42 #include <QSound>
43 #include <QDir>
44 #include <QTextStream>
45 #include "cxTypeConversions.h"
46 #include "cxDefinitionStrings.h"
47 #include "cxTime.h"
48 #include "cxProfile.h"
49 #include "cxMessageListener.h"
50 #include "internal/cxLogThread.h"
51 #include "QApplication"
52 
53 namespace cx
54 {
55 
56 class EventProcessingThread : public QThread
57 {
58 public:
60  {
61  }
63  {
64 
65  }
66 
67  virtual void run()
68  {
69  this->exec();
70  qApp->processEvents(); // exec() docs doesn't guarantee that the posted events are processed. - do that here.
71  }
72 };
73 
74 
76 {
77  mLogPath = this->getDefaultLogPath();
78 }
79 
81 {
82  this->stopThread();
83 }
84 
85 QString Log::getDefaultLogPath() const
86 {
87  QString isoDateFormat("yyyy-MM-dd");
88  QString isoDate = QDateTime::currentDateTime().toString(isoDateFormat);
89  QString retval = ProfileManager::getInstance()->getSettingsPath()+"/Logs/"+isoDate;
90  return retval;
91 }
92 
94 {
95  if (mThread)
96  return;
97 
98  this->stopThread();
99  this->startThread();
100 }
101 
103 {
104  if (mThread)
105  return;
106 
107  mThread.reset(new EventProcessingThread());
108  mThread->setObjectName("org.custusx.resource.core.logger");
109 
110  mWorker = this->createWorker();
111  mWorker->moveToThread(mThread.get());
112  if (!mLogPath.isEmpty())
113  mWorker->setLoggingFolder(mLogPath);
115 
116  mThread->start();
117 }
118 
120 {
121  if (!mThread)
122  return;
123 
124  disconnect(mWorker.get(), &LogThread::emittedMessage, this, &Log::onEmittedMessage);
125  LogThreadPtr tempWorker = mWorker;
126  mWorker.reset();
127 
128  mThread->quit();
129  mThread->wait(); // forever or until dead thread
130 
131  mThread.reset();
132  tempWorker.reset();
133 }
134 
135 void Log::setLoggingFolder(QString absoluteLoggingFolderPath)
136 {
137  mLogPath = absoluteLoggingFolderPath;
138  if (mWorker)
139  mWorker->setLoggingFolder(mLogPath);
140  emit loggingFolderChanged();
141 }
142 
143 QString Log::getLoggingFolder() const
144 {
145  return mLogPath;
146 }
147 
148 void Log::installObserver(MessageObserverPtr observer, bool resend)
149 {
150  if (mWorker)
151  mWorker->installObserver(observer, resend);
152 }
153 
155 {
156  if (mWorker)
157  mWorker->uninstallObserver(observer);
158 }
159 
160 
161 } //End namespace cx
virtual ~EventProcessingThread()
Definition: cxLog.cpp:62
virtual void onEmittedMessage(Message message)
Definition: cxLog.h:96
QString mLogPath
Definition: cxLog.h:106
void stopThread()
Definition: cxLog.cpp:119
void setLoggingFolder(QString absoluteLoggingFolderPath)
Definition: cxLog.cpp:135
QString getDefaultLogPath() const
Definition: cxLog.cpp:85
void loggingFolderChanged()
void startThread()
Definition: cxLog.cpp:102
boost::shared_ptr< class QThread > mThread
Definition: cxLog.h:107
virtual void run()
Definition: cxLog.cpp:67
void installObserver(MessageObserverPtr observer, bool resend)
Definition: cxLog.cpp:148
void initializeObject()
Definition: cxLog.cpp:93
static ProfileManager * getInstance()
returns the only instance of this class
Definition: cxProfile.cpp:183
QString getLoggingFolder() const
Definition: cxLog.cpp:143
void emittedMessage(Message message)
emitted for each new message, in addition to writing to observer.
boost::shared_ptr< class MessageObserver > MessageObserverPtr
Definition: cxLog.h:65
LogThreadPtr mWorker
Definition: cxLog.h:108
virtual ~Log()
Definition: cxLog.cpp:80
boost::shared_ptr< class LogThread > LogThreadPtr
Definition: cxLog.h:67
Log()
Definition: cxLog.cpp:75
QString getSettingsPath()
Definition: cxProfile.cpp:235
virtual LogThreadPtr createWorker()=0
void uninstallObserver(MessageObserverPtr observer)
Definition: cxLog.cpp:154