NorMIT-nav  16.5
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 
42  QObject(parent)
43 {
44 }
45 
47 {
48  QMutexLocker sentry(&mActionsMutex);
49  mPendingActions.push_back(action);
50  sentry.unlock();
51 
52  this->invokePendingAction();
53 }
54 
55 void ThreadMethodInvoker::invokePendingAction()
56 {
57  QMetaObject::invokeMethod(this, "pendingAction", Qt::QueuedConnection);
58 }
59 
60 void ThreadMethodInvoker::pendingAction()
61 {
62  while (this->executeAction());
63 }
64 
65 bool ThreadMethodInvoker::executeAction()
66 {
67  ActionType action = this->popAction();
68  if (!action)
69  return false;
70 
71  action();
72  return true;
73 }
74 
75 ThreadMethodInvoker::ActionType ThreadMethodInvoker::popAction()
76 {
77  QMutexLocker sentry(&mActionsMutex);
78  ActionType action;
79 
80  if (mPendingActions.isEmpty())
81  return action;
82 
83  action = mPendingActions.front();
84  mPendingActions.pop_front();
85  return action;
86 }
87 
88 } // namespace cx
89 
90 
91 
92 
93 namespace cx
94 {
95 
96 LogThread::LogThread(QObject* parent) :
97  QObject(parent)
98 {
99  mQueue = new ThreadMethodInvoker(this);
101 }
102 
104 {
105  ActionType action = boost::bind(&MessageRepository::install, mRepository, observer, resend);
106  this->callInLogThread(action);
107 }
108 
110 {
111  ActionType action = boost::bind(&MessageRepository::uninstall, mRepository, observer);
112  this->callInLogThread(action);
113 }
114 
115 void LogThread::setLoggingFolder(QString absoluteLoggingFolderPath)
116 {
117  ActionType action = boost::bind(&LogThread::executeSetLoggingFolder, this, absoluteLoggingFolderPath);
118  this->callInLogThread(action);
119 }
120 
122 {
123  mQueue->callInLogThread(action);
124 }
125 
127 {
128  message = this->cleanupMessage(message);
129 
130  emit emittedMessage(message);
131 
132  mRepository->setMessage(message);
133 }
134 
135 
137 {
138  if (message.mTimeoutTime<0)
139  message.mTimeoutTime = this->getDefaultTimeout(message.mMessageLevel);
140 
141  if (message.mChannel.isEmpty())
142  message.mChannel = "console";
143 
144  if (!message.mSourceFile.isEmpty())
145  {
146  message.mSourceFile = message.mSourceFile.split("CustusX/").back();
147  message.mSourceFile = message.mSourceFile.split("CX/").back();
148  }
149 
150  return message;
151 }
152 
153 int LogThread::getDefaultTimeout(MESSAGE_LEVEL messageLevel) const
154 {
155  switch(messageLevel)
156  {
157  case mlDEBUG: return 0;
158  case mlINFO: return 1500;
159  case mlSUCCESS: return 1500;
160  case mlWARNING: return 3000;
161  case mlERROR: return 0;
162  case mlVOLATILE: return 5000;
163  default: return 0;
164  }
165 }
166 
167 
168 
169 } //End namespace cx
mlSUCCESS
Definition: cxDefinitions.h:82
mlVOLATILE
Definition: cxDefinitions.h:82
QString mSourceFile
Definition: cxLogMessage.h:98
virtual void installObserver(MessageObserverPtr observer, bool resend)
void uninstall(MessageObserverPtr observer)
ThreadMethodInvoker(QObject *parent)
Definition: cxLogThread.cpp:41
virtual void uninstallObserver(MessageObserverPtr observer)
static MessageRepositoryPtr create()
virtual void setLoggingFolder(QString absoluteLoggingFolderPath)
call during startup, will fail if called when running
LogThread(QObject *parent=NULL)
Definition: cxLogThread.cpp:96
mlDEBUG
Definition: cxDefinitions.h:82
MESSAGE_LEVEL mMessageLevel
Definition: cxLogMessage.h:90
MessageRepositoryPtr mRepository
Definition: cxLogThread.h:126
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
boost::function< void()> ActionType
Definition: cxLogThread.h:84
mlINFO
Definition: cxDefinitions.h:82
void callInLogThread(ThreadMethodInvoker::ActionType action)
void callInLogThread(ActionType action)
Definition: cxLogThread.cpp:46
mlWARNING
Definition: cxDefinitions.h:82
void processMessage(Message msg)
mlERROR
Definition: cxDefinitions.h:82
Message cleanupMessage(Message message)