Fraxinus  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
mlSUCCESS
Definition: cxDefinitions.h:67
mlVOLATILE
Definition: cxDefinitions.h:67
QString mSourceFile
Definition: cxLogMessage.h:77
virtual void installObserver(MessageObserverPtr observer, bool resend)
Definition: cxLogThread.cpp:82
void uninstall(MessageObserverPtr observer)
ThreadMethodInvoker(QObject *parent)
Definition: cxLogThread.cpp:20
virtual void uninstallObserver(MessageObserverPtr observer)
Definition: cxLogThread.cpp:88
static MessageRepositoryPtr create()
virtual void setLoggingFolder(QString absoluteLoggingFolderPath)
call during startup, will fail if called when running
Definition: cxLogThread.cpp:94
LogThread(QObject *parent=NULL)
Definition: cxLogThread.cpp:75
mlDEBUG
Definition: cxDefinitions.h:67
MESSAGE_LEVEL mMessageLevel
Definition: cxLogMessage.h:69
MessageRepositoryPtr mRepository
Definition: cxLogThread.h:105
QString mChannel
Definition: cxLogMessage.h:74
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:44
int mTimeoutTime
Definition: cxLogMessage.h:70
boost::function< void()> ActionType
Definition: cxLogThread.h:63
mlINFO
Definition: cxDefinitions.h:67
void callInLogThread(ThreadMethodInvoker::ActionType action)
void callInLogThread(ActionType action)
Definition: cxLogThread.cpp:25
mlWARNING
Definition: cxDefinitions.h:67
void processMessage(Message msg)
mlERROR
Definition: cxDefinitions.h:67
Message cleanupMessage(Message message)
Namespace for all CustusX production code.