CustusX  16.5.0-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxProcessReporter.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 "cxProcessReporter.h"
34 
35 
36 #include "cxLogger.h"
37 
38 
39 namespace cx
40 {
41 ProcessReporter::ProcessReporter(QProcess* process, QString name) :
42  mName(name)
43 {
44  CX_ASSERT(process);
45  mProcess = process;
46 
47  connect(mProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(processStateChanged(QProcess::ProcessState)));
48  connect(mProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
49  connect(mProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
50  connect(mProcess, SIGNAL(readyRead()), this, SLOT(processReadyRead()));
51 }
52 
54 {
55  disconnect(mProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(processStateChanged(QProcess::ProcessState)));
56  disconnect(mProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
57  disconnect(mProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
58  disconnect(mProcess, SIGNAL(readyRead()), this, SLOT(processReadyRead()));
59 }
60 
61 void ProcessReporter::processReadyRead()
62 {
63  report(QString(mProcess->readAllStandardOutput()));
64 }
65 
66 void ProcessReporter::processStateChanged(QProcess::ProcessState newState)
67 {
68  if (newState == QProcess::Running)
69  {
70  report(QString("%1 started.").arg(mName));
71  }
72  if (newState == QProcess::NotRunning)
73  {
74  report(QString("%1 stopped.").arg(mName));
75  }
76  if (newState == QProcess::Starting)
77  {
78  report(QString("%1 starting.").arg(mName));
79  }
80 }
81 
82 void ProcessReporter::processError(QProcess::ProcessError error)
83 {
84  QString msg;
85  msg += QString("%1 reported an error: ").arg(mName);
86 
87  switch (error)
88  {
89  case QProcess::FailedToStart:
90  msg += "Failed to start";
91  break;
92  case QProcess::Crashed:
93  msg += "Crashed";
94  break;
95  case QProcess::Timedout:
96  msg += "Timed out";
97  break;
98  case QProcess::WriteError:
99  msg += "Write Error";
100  break;
101  case QProcess::ReadError:
102  msg += "Read Error";
103  break;
104  case QProcess::UnknownError:
105  msg += "Unknown Error";
106  break;
107  default:
108  msg += "Invalid error";
109  }
110 
111  reportError(msg);
112 }
113 
114 void ProcessReporter::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
115 {
116  QString msg = QString("%1 exited with exit status %3. (%1 last exit code was %4.)").arg(mName).arg(exitStatus).arg(exitCode);
117  if(exitStatus == 0)
118  reportSuccess(msg);
119  else
120  reportError(msg);
121 }
122 
123 } /* namespace cx */
void reportError(QString msg)
Definition: cxLogger.cpp:92
#define CX_ASSERT(statement)
Definition: cxLogger.h:131
ProcessReporter(QProcess *process, QString name)
void reportSuccess(QString msg)
Definition: cxLogger.cpp:93
void report(QString msg)
Definition: cxLogger.cpp:90