Fraxinus  16.5.0-fx-rc6
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxReporterThread.h"
34 #include "cxReporter.h"
35 #include "cxLogger.h"
36 #include <QtGlobal>
37 #include <iostream>
38 #include "boost/bind.hpp"
39 #include "boost/shared_ptr.hpp"
40 #include <QString>
41 #include <QMutex>
42 #include <QSound>
43 #include <QDir>
44 #include <QTextStream>
45 #include <QTimer>
46 #include "cxTypeConversions.h"
47 #include "cxDefinitionStrings.h"
48 #include "cxTime.h"
49 #include "cxMessageListener.h"
50 
51 #include "cxLogQDebugRedirecter.h"
54 #include "cxTime.h"
55 #include "cxLogFile.h"
56 
57 namespace cx
58 {
59 
61  LogThread(parent)
62 {
63  qInstallMessageHandler(convertQtMessagesToCxMessages);
64  qRegisterMetaType<Message>("Message");
65 
66  connect(this, &LogThread::emittedMessage, this, &ReporterThread::onMessageEmitted);
67 
68  // make sure streams are closed properly before reconnecting.
69  mCout.reset();
70  mCerr.reset();
71 
72  mCout.reset(new SingleStreamerImpl(std::cout, mlCOUT));
73  mCerr.reset(new SingleStreamerImpl(std::cerr, mlCERR));
74 }
75 
77 {
78  qInstallMessageHandler(0);
79  mCout.reset();
80  mCerr.reset();
81 }
82 
83 bool ReporterThread::initializeLogFile(LogFile file)
84 {
85  QString filename = file.getFilename();
86  if (mInitializedFiles.contains(filename))
87  return true;
88 
89  mInitializedFiles << filename;
90 
91  file.writeHeader();
92 
93  if (!file.isWritable())
94  {
95  this->processMessage(Message("Failed to open log file " + filename, mlERROR));
96  return false;
97  }
98 
99  return true;
100 }
101 
102 void ReporterThread::executeSetLoggingFolder(QString absoluteLoggingFolderPath)
103 {
104  mLogPath = absoluteLoggingFolderPath;
105 
106  QFileInfo(mLogPath+"/").absoluteDir().mkpath(".");
107 
108 // this->initializeLogFile(this->getFilenameForChannel("console"));
109 // this->initializeLogFile(this->getFilenameForChannel("all"));
110 
111  this->initializeLogFile(LogFile::fromChannel(mLogPath, "console"));
112  this->initializeLogFile(LogFile::fromChannel(mLogPath, "all"));
113 }
114 
116 {
117  // send in calling thread- this helps if the app
118  // is about to crash and we need debug info.
119  this->sendToCout(msg);
120 
121  QMetaObject::invokeMethod(this, "processMessage",
122  Qt::QueuedConnection,
123  Q_ARG(Message, msg));
124 }
125 
126 void ReporterThread::onMessageEmitted(Message msg)
127 {
128  // this->sendToCout(message);
129  this->sendToFile(msg);
130 }
131 
132 void ReporterThread::sendToFile(Message message)
133 {
134  if (message.getMessageLevel()==mlVOLATILE)
135  return;
136 
137 // QString channelFile = this->getFilenameForChannel(message.mChannel);
138  LogFile channelLog = LogFile::fromChannel(mLogPath, message.mChannel);
139  LogFile allLog = LogFile::fromChannel(mLogPath, "all");
140 
141  this->initializeLogFile(channelLog);
142 
143  channelLog.write(message);
144  allLog.write(message);
145 }
146 
147 void ReporterThread::sendToCout(Message message)
148 {
149  if (!mCout)
150  return;
151  if (message.getMessageLevel()==mlVOLATILE)
152  return;
153  if (( message.getMessageLevel() == mlCOUT )||( message.getMessageLevel() == mlCERR ))
154  return;
155 
156  mCout->sendUnredirected(message.getPrintableMessage()+"\n");
157 }
158 
159 } //End namespace cx
mlVOLATILE
Definition: cxDefinitions.h:82
QString getPrintableMessage() const
Text containing information appropriate to display.
virtual void logMessage(Message msg)
virtual void executeSetLoggingFolder(QString absoluteLoggingFolderPath)
mlCERR
Definition: cxDefinitions.h:82
void writeHeader()
Definition: cxLogFile.cpp:73
QString mChannel
Definition: cxLogMessage.h:95
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:52
cx::Message Message
Definition: cxLogMessage.h:107
QString getFilename() const
Definition: cxLogFile.cpp:60
bool isWritable() const
Definition: cxLogFile.cpp:88
void convertQtMessagesToCxMessages(QtMsgType type, const QMessageLogContext &, const QString &msg)
mlCOUT
Definition: cxDefinitions.h:82
void processMessage(Message msg)
ReporterThread(QObject *parent=NULL)
MESSAGE_LEVEL getMessageLevel() const
The category of the message.
mlERROR
Definition: cxDefinitions.h:82