CustusX  2023.01.05-dev+develop.0da12
An IGT application
cxReporterThread.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 "cxReporterThread.h"
13 #include "cxReporter.h"
14 #include "cxLogger.h"
15 #include <QtGlobal>
16 #include <iostream>
17 #include "boost/bind.hpp"
18 #include "boost/shared_ptr.hpp"
19 #include <QString>
20 #include <QMutex>
21 #include <QSound>
22 #include <QDir>
23 #include <QTextStream>
24 #include <QTimer>
25 #include "cxTime.h"
26 #include "cxMessageListener.h"
27 
28 #include "cxLogQDebugRedirecter.h"
31 #include "cxTime.h"
32 #include "cxLogFile.h"
33 
34 namespace cx
35 {
36 
38  LogThread(parent)
39 {
40  qInstallMessageHandler(convertQtMessagesToCxMessages);
41  qRegisterMetaType<Message>("Message");
42 
43  connect(this, &LogThread::emittedMessage, this, &ReporterThread::onMessageEmitted);
44 
45  // make sure streams are closed properly before reconnecting.
46  mCout.reset();
47  mCerr.reset();
48 
49  mCout.reset(new SingleStreamerImpl(std::cout, mlCOUT));
50  mCerr.reset(new SingleStreamerImpl(std::cerr, mlCERR));
51 }
52 
54 {
55  qInstallMessageHandler(0);
56  mCout.reset();
57  mCerr.reset();
58 }
59 
60 bool ReporterThread::initializeLogFile(LogFile file)
61 {
62  QString filename = file.getFilename();
63  if (mInitializedFiles.contains(filename))
64  return true;
65 
66  mInitializedFiles << filename;
67 
68  file.writeHeader();
69 
70  if (!file.isWritable())
71  {
72  this->processMessage(Message("Failed to open log file " + filename, mlERROR));
73  return false;
74  }
75 
76  return true;
77 }
78 
79 void ReporterThread::executeSetLoggingFolder(QString absoluteLoggingFolderPath)
80 {
81  mLogPath = absoluteLoggingFolderPath;
82 
83  QFileInfo(mLogPath+"/").absoluteDir().mkpath(".");
84 
85 // this->initializeLogFile(this->getFilenameForChannel("console"));
86 // this->initializeLogFile(this->getFilenameForChannel("all"));
87 
88  this->initializeLogFile(LogFile::fromChannel(mLogPath, "console"));
89  this->initializeLogFile(LogFile::fromChannel(mLogPath, "all"));
90 }
91 
93 {
94  // send in calling thread- this helps if the app
95  // is about to crash and we need debug info.
96  this->sendToCout(msg);
97 
98  QMetaObject::invokeMethod(this, "processMessage",
99  Qt::QueuedConnection,
100  Q_ARG(Message, msg));
101 }
102 
103 void ReporterThread::onMessageEmitted(Message msg)
104 {
105  // this->sendToCout(message);
106  this->sendToFile(msg);
107 }
108 
109 void ReporterThread::sendToFile(Message message)
110 {
111  if (message.getMessageLevel()==mlVOLATILE)
112  return;
113 
114 // QString channelFile = this->getFilenameForChannel(message.mChannel);
115  LogFile channelLog = LogFile::fromChannel(mLogPath, message.mChannel);
116  LogFile allLog = LogFile::fromChannel(mLogPath, "all");
117 
118  this->initializeLogFile(channelLog);
119 
120  channelLog.write(message);
121  allLog.write(message);
122 }
123 
124 void ReporterThread::sendToCout(Message message)
125 {
126  if (!mCout)
127  return;
128  if (message.getMessageLevel()==mlVOLATILE)
129  return;
130  if (( message.getMessageLevel() == mlCOUT )||( message.getMessageLevel() == mlCERR ))
131  return;
132 
133  mCout->sendUnredirected(message.getPrintableMessage()+"\n");
134 }
135 
137 {
138  qInstallMessageHandler(silenceQtMessages);
139 }
140 
142 {
143  qInstallMessageHandler(convertQtMessagesToCxMessages);
144 }
145 
146 
147 } //End namespace cx
mlVOLATILE
Definition: cxDefinitions.h:67
QString getPrintableMessage() const
Text containing information appropriate to display.
virtual void logMessage(Message msg)
virtual void executeSetLoggingFolder(QString absoluteLoggingFolderPath)
mlCERR
Definition: cxDefinitions.h:67
void write(Message message)
Definition: cxLogFile.cpp:60
void writeHeader()
Definition: cxLogFile.cpp:51
QString mChannel
Definition: cxLogMessage.h:74
void emittedMessage(Message message)
emitted for each new message, in addition to writing to observer.
void silenceQtMessages(QtMsgType type, const QMessageLogContext &, const QString &msg)
static LogFile fromChannel(QString path, QString channel)
Definition: cxLogFile.cpp:30
cx::Message Message
Definition: cxLogMessage.h:86
QString getFilename() const
Definition: cxLogFile.cpp:38
bool isWritable() const
Definition: cxLogFile.cpp:66
void convertQtMessagesToCxMessages(QtMsgType type, const QMessageLogContext &, const QString &msg)
mlCOUT
Definition: cxDefinitions.h:67
void processMessage(Message msg)
ReporterThread(QObject *parent=NULL)
MESSAGE_LEVEL getMessageLevel() const
The category of the message.
mlERROR
Definition: cxDefinitions.h:67
Namespace for all CustusX production code.