Fraxinus  2023.01.05-dev+develop.0da12
An IGT application
cxLogMessage.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 "cxLogMessage.h"
13 
14 #include "cxReporter.h"
15 #include "cxLogger.h"
16 #include <QtGlobal>
17 #include <iostream>
18 #include "boost/shared_ptr.hpp"
19 #include <QString>
20 #include <QMutex>
21 #include <QSound>
22 #include <QDir>
23 #include <QTextStream>
24 #include <QThread>
25 #include <QApplication>
26 #include "cxTypeConversions.h"
27 #include "cxTime.h"
28 #include "cxMessageListener.h"
29 
30 namespace cx
31 {
32 
33 Message::Message(QString text, MESSAGE_LEVEL messageLevel, int timeoutTime) :
34  mText(text),
35  mMessageLevel(messageLevel),
36  mTimeoutTime(timeoutTime),
37  mTimeStamp(QDateTime::currentDateTime()),
38  mMuted(true),
39  mChannel("console")
40 {
41  mThread = "NotUsed";
42  //this->setThreadName(); //Cause crash on Ubuntu 20.04, when calling QApplication::instance()->thread()
43 }
44 
46 {
47 
48 }
49 
50 void Message::setThreadName()
51 {
52  if (QApplication::instance()->thread() == QThread::currentThread())
53  mThread = "main";
54  else
55  {
56  mThread = QThread::currentThread()->objectName();
57  if (mThread.isEmpty())
58  mThread = "0x"+QString::number(reinterpret_cast<long>(QThread::currentThreadId()), 16);
59  }
60 }
61 
63 {
64  QString source = this->getSourceLocation();
65 
66  QString printableMessage;
67  if(mMessageLevel == mlRAW)
68  printableMessage = mText;
69  else
70  printableMessage = QString("[%1][%2] %3")
71  .arg(mTimeStamp.toString("hh:mm:ss.zzz"))
73  .arg(mText);
74 // printableMessage = QString("[%1][%2][%3][%4] %5")
75 // .arg(mTimeStamp.toString("hh:mm:ss.zzz"))
76 // .arg(source)
77 // .arg(mThread)
78 // .arg(qstring_cast(mMessageLevel))
79 // .arg(mText);
80 
81  return printableMessage;
82 }
83 
84 MESSAGE_LEVEL Message::getMessageLevel() const
85 {
86  return mMessageLevel;
87 }
88 
89 QString Message::getText() const
90 {
91  return mText;
92 }
93 
94 QDateTime Message::getTimeStamp() const
95 {
96  return mTimeStamp;
97 }
99 {
100  return mTimeoutTime;
101 }
103 {
104  if (mSourceFile.isEmpty() && mSourceFunction.isEmpty())
105  return "";
106  return QString("%1:%2/%3").arg(mSourceFile).arg(mSourceLine).arg(mSourceFunction);
107 }
108 
109 
110 } //End namespace cx
QString qstring_cast(const T &val)
mlRAW
Definition: cxDefinitions.h:67
QString mSourceFile
Definition: cxLogMessage.h:77
Message(QString text="", MESSAGE_LEVEL messageLevel=mlDEBUG, int timeoutTime=-1)
QString getPrintableMessage() const
Text containing information appropriate to display.
QString getText() const
The raw message.
int getTimeout() const
Timout tells the statusbar how long it should be displayed, this depends on the message level...
QString mThread
Definition: cxLogMessage.h:75
QDateTime getTimeStamp() const
The time at which the message was created.
MESSAGE_LEVEL mMessageLevel
Definition: cxLogMessage.h:69
QDateTime mTimeStamp
Definition: cxLogMessage.h:71
int mTimeoutTime
Definition: cxLogMessage.h:70
QString mSourceFunction
Definition: cxLogMessage.h:78
QString getSourceLocation() const
QString mText
Definition: cxLogMessage.h:68
MESSAGE_LEVEL getMessageLevel() const
The category of the message.
Namespace for all CustusX production code.