CustusX  15.3.3-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxLogThread.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 "cxLogThread.h"
35 #include "boost/bind.hpp"
36 #include <iostream>
37 
38 namespace cx
39 {
40 
41 LogThread::LogThread(QObject* parent) :
42  QObject(parent)
43 {
45 }
46 
48 {
49  PendingActionType action = boost::bind(&MessageRepository::install, mRepository.get(), observer, resend);
50  this->callInLogThread(action);
51 }
52 
54 {
55  PendingActionType action = boost::bind(&MessageRepository::uninstall, mRepository.get(), observer);
56  this->callInLogThread(action);
57 }
58 
59 void LogThread::setLoggingFolder(QString absoluteLoggingFolderPath)
60 {
61  PendingActionType action = boost::bind(&LogThread::executeSetLoggingFolder, this, absoluteLoggingFolderPath);
62  this->callInLogThread(action);
63 }
64 
65 void LogThread::callInLogThread(PendingActionType& action)
66 {
67  QMutexLocker sentry(&mActionsMutex);
68  mPendingActions.push_back(action);
69  sentry.unlock();
70 
71  this->invokePendingAction();
72 }
73 
74 void LogThread::invokePendingAction()
75 {
76  QMetaObject::invokeMethod(this, "pendingAction", Qt::QueuedConnection);
77 }
78 
80 {
81  while (this->executeAction());
82 }
83 
84 bool LogThread::executeAction()
85 {
86  PendingActionType action = this->popAction();
87  if (!action)
88  return false;
89 
90  action();
91  return true;
92 }
93 
94 LogThread::PendingActionType LogThread::popAction()
95 {
96  QMutexLocker sentry(&mActionsMutex);
97  PendingActionType action;
98 
99  if (mPendingActions.isEmpty())
100  return action;
101 
102  action = mPendingActions.front();
103  mPendingActions.pop_front();
104  return action;
105 }
106 
108 {
109  message = this->cleanupMessage(message);
110 
111  emit emittedMessage(message);
112 
113  mRepository->setMessage(message);
114 }
115 
116 
118 {
119  if (message.mTimeoutTime<0)
120  message.mTimeoutTime = this->getDefaultTimeout(message.mMessageLevel);
121 
122  if (message.mChannel.isEmpty())
123  message.mChannel = "console";
124 
125  if (!message.mSourceFile.isEmpty())
126  {
127  message.mSourceFile = message.mSourceFile.split("CustusX/").back();
128  message.mSourceFile = message.mSourceFile.split("CX/").back();
129  }
130 
131  return message;
132 }
133 
134 int LogThread::getDefaultTimeout(MESSAGE_LEVEL messageLevel) const
135 {
136  switch(messageLevel)
137  {
138  case mlDEBUG: return 0;
139  case mlINFO: return 1500;
140  case mlSUCCESS: return 1500;
141  case mlWARNING: return 3000;
142  case mlERROR: return 0;
143  case mlVOLATILE: return 5000;
144  default: return 0;
145  }
146 }
147 
148 
149 
150 } //End namespace cx
void callInLogThread(PendingActionType &action)
Definition: cxLogThread.cpp:65
mlSUCCESS
Definition: cxDefinitions.h:80
void pendingAction()
Definition: cxLogThread.cpp:79
mlVOLATILE
Definition: cxDefinitions.h:80
QString mSourceFile
Definition: cxLogMessage.h:98
virtual void installObserver(MessageObserverPtr observer, bool resend)
Definition: cxLogThread.cpp:47
void uninstall(MessageObserverPtr observer)
virtual void uninstallObserver(MessageObserverPtr observer)
Definition: cxLogThread.cpp:53
static MessageRepositoryPtr create()
virtual void setLoggingFolder(QString absoluteLoggingFolderPath)
call during startup, will fail if called when running
Definition: cxLogThread.cpp:59
LogThread(QObject *parent=NULL)
Definition: cxLogThread.cpp:41
mlDEBUG
Definition: cxDefinitions.h:80
MESSAGE_LEVEL mMessageLevel
Definition: cxLogMessage.h:90
MessageRepositoryPtr mRepository
Definition: cxLogThread.h:99
QString mChannel
Definition: cxLogMessage.h:95
virtual void executeSetLoggingFolder(QString absoluteLoggingFolderPath)=0
void emittedMessage(Message message)
emitted for each new message, in addition to writing to observer.
void install(MessageObserverPtr observer, bool resend)
boost::shared_ptr< class MessageObserver > MessageObserverPtr
Definition: cxLog.h:65
int mTimeoutTime
Definition: cxLogMessage.h:91
mlINFO
Definition: cxDefinitions.h:80
mlWARNING
Definition: cxDefinitions.h:80
void processMessage(Message msg)
mlERROR
Definition: cxDefinitions.h:80
Message cleanupMessage(Message message)