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