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