SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
logmessage.cpp
Go to the documentation of this file.
1 
6 #include "logmessage.h"
7 #include <QThread>
8 
9 using namespace qtwebapp;
10 
11 LogMessage::LogMessage(const QtMsgType type, const QString& message, QHash<QString, QString>* logVars, const QString &file, const QString &function, const int line)
12 {
13  this->type=type;
14  this->message=message;
15  this->file=file;
16  this->function=function;
17  this->line=line;
18  timestamp=QDateTime::currentDateTime();
19  threadId=QThread::currentThreadId();
20 
21  // Copy the logVars if not null,
22  // so that later changes in the original do not affect the copy
23  if (logVars)
24  {
25  this->logVars=*logVars;
26  }
27 }
28 
29 QString LogMessage::toString(const QString& msgFormat, const QString& timestampFormat) const
30 {
31  QString decorated=msgFormat+"\n";
32  decorated.replace("{msg}",message);
33 
34  if (decorated.contains("{timestamp}"))
35  {
36  decorated.replace("{timestamp}",timestamp.toString(timestampFormat));
37  }
38 
39  QString typeNr;
40  typeNr.setNum(type);
41  decorated.replace("{typeNr}",typeNr);
42 
43  switch (type)
44  {
45  case QtDebugMsg:
46  decorated.replace("{type}","(D)");
47  break;
48  case QtWarningMsg:
49  decorated.replace("{type}","(W)");
50  break;
51  case QtCriticalMsg:
52  decorated.replace("{type}","(C)");
53  break;
54  case QtFatalMsg:
55  decorated.replace("{type}","(F)");
56  break;
57  #if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
58  case QtInfoMsg:
59  decorated.replace("{type}","(I)");
60  break;
61  #endif
62  default:
63  decorated.replace("{type}",typeNr);
64  }
65 
66  decorated.replace("{file}",file);
67  decorated.replace("{function}",function);
68  decorated.replace("{line}",QString::number(line));
69 
70  QString threadId;
71  threadId.setNum((std::size_t)QThread::currentThreadId());
72  decorated.replace("{thread}",threadId);
73 
74  // Fill in variables
75  if (decorated.contains("{") && !logVars.isEmpty())
76  {
77  QList<QString> keys=logVars.keys();
78  foreach (QString key, keys)
79  {
80  decorated.replace("{"+key+"}",logVars.value(key));
81  }
82  }
83 
84  return decorated;
85 }
86 
87 QtMsgType LogMessage::getType() const
88 {
89  return type;
90 }
QHash< QString, QString > logVars
Definition: logmessage.h:72
LogMessage(const QtMsgType type, const QString &message, QHash< QString, QString > *logVars, const QString &file, const QString &function, const int line)
Definition: logmessage.cpp:11
QtMsgType getType() const
Definition: logmessage.cpp:87
Qt::HANDLE threadId
Definition: logmessage.h:81
QDateTime timestamp
Definition: logmessage.h:75
QString toString(const QString &msgFormat, const QString &timestampFormat) const
Definition: logmessage.cpp:29