NorMIT-nav  22.09
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 
136 } //End namespace cx
cxLogger.h
cxMessageListener.h
cx::LogThread::processMessage
void processMessage(Message msg)
Definition: cxLogThread.cpp:105
cxLogQDebugRedirecter.h
cx::ReporterThread::~ReporterThread
virtual ~ReporterThread()
Definition: cxReporterThread.cpp:53
cxLogFile.h
cx::Message
Definition: cxLogMessage.h:54
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::LogFile::getFilename
QString getFilename() const
Definition: cxLogFile.cpp:38
cx::LogFile
Definition: cxLogFile.h:25
cxLogIOStreamRedirecter.h
cx::Message::getMessageLevel
MESSAGE_LEVEL getMessageLevel() const
The category of the message.
Definition: cxLogMessage.cpp:84
cxReporterThread.h
cx::Message::mChannel
QString mChannel
Definition: cxLogMessage.h:74
cxReporter.h
cx::SingleStreamerImpl
Definition: cxLogIOStreamRedirecter.h:30
cx::Message::getPrintableMessage
QString getPrintableMessage() const
Text containing information appropriate to display.
Definition: cxLogMessage.cpp:62
cx::LogFile::writeHeader
void writeHeader()
Definition: cxLogFile.cpp:51
cx::ReporterThread::executeSetLoggingFolder
virtual void executeSetLoggingFolder(QString absoluteLoggingFolderPath)
Definition: cxReporterThread.cpp:79
mlCOUT
mlCOUT
Definition: cxDefinitions.h:71
cx::LogThread
Definition: cxLogThread.h:84
mlVOLATILE
mlVOLATILE
Definition: cxDefinitions.h:74
Message
cx::Message Message
Definition: cxLogMessage.h:86
cx::LogFile::fromChannel
static LogFile fromChannel(QString path, QString channel)
Definition: cxLogFile.cpp:30
mlERROR
mlERROR
Definition: cxDefinitions.h:69
cx::ReporterThread::ReporterThread
ReporterThread(QObject *parent=NULL)
Definition: cxReporterThread.cpp:37
cx::ReporterThread::logMessage
virtual void logMessage(Message msg)
Definition: cxReporterThread.cpp:92
cxTime.h
mlCERR
mlCERR
Definition: cxDefinitions.h:72
cx::convertQtMessagesToCxMessages
void convertQtMessagesToCxMessages(QtMsgType type, const QMessageLogContext &, const QString &msg)
Definition: cxLogQDebugRedirecter.cpp:33
cxReporterMessageRepository.h
cx::LogThread::emittedMessage
void emittedMessage(Message message)
emitted for each new message, in addition to writing to observer.
cx::LogFile::isWritable
bool isWritable() const
Definition: cxLogFile.cpp:66