CustusX  22.04-rc5
An IGT application
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) 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 "cxProcessReporter.h"
13 
14 
15 #include "cxLogger.h"
16 
17 
18 namespace cx
19 {
20 ProcessReporter::ProcessReporter(QProcess* process, QString name) :
21  mName(name)
22 {
23  CX_ASSERT(process);
24  mProcess = process;
25 
26  connect(mProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(processStateChanged(QProcess::ProcessState)));
27  connect(mProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
28  connect(mProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
29  connect(mProcess, SIGNAL(readyRead()), this, SLOT(processReadyRead()));
30 }
31 
33 {
34  disconnect(mProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(processStateChanged(QProcess::ProcessState)));
35  disconnect(mProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
36  disconnect(mProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
37  disconnect(mProcess, SIGNAL(readyRead()), this, SLOT(processReadyRead()));
38 }
39 
40 void ProcessReporter::processReadyRead()
41 {
42  report(QString(mProcess->readAllStandardOutput()));
43 }
44 
45 void ProcessReporter::processStateChanged(QProcess::ProcessState newState)
46 {
47  if (newState == QProcess::Running)
48  {
49  report(QString("%1 started.").arg(mName));
50  }
51  if (newState == QProcess::NotRunning)
52  {
53  report(QString("%1 stopped.").arg(mName));
54  }
55  if (newState == QProcess::Starting)
56  {
57  report(QString("%1 starting.").arg(mName));
58  }
59 }
60 
61 void ProcessReporter::processError(QProcess::ProcessError error)
62 {
63  QString msg;
64  msg += QString("%1 reported an error: ").arg(mName);
65 
66  switch (error)
67  {
68  case QProcess::FailedToStart:
69  msg += "Failed to start";
70  break;
71  case QProcess::Crashed:
72  msg += "Crashed";
73  break;
74  case QProcess::Timedout:
75  msg += "Timed out";
76  break;
77  case QProcess::WriteError:
78  msg += "Write Error";
79  break;
80  case QProcess::ReadError:
81  msg += "Read Error";
82  break;
83  case QProcess::UnknownError:
84  msg += "Unknown Error";
85  break;
86  default:
87  msg += "Invalid error";
88  }
89 
90  reportError(msg);
91 }
92 
93 void ProcessReporter::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
94 {
95  QString msg = QString("%1 exited with exit status %3. (%1 last exit code was %4.)").arg(mName).arg(exitStatus).arg(exitCode);
96  if(exitStatus == 0)
97  reportSuccess(msg);
98  else
99  reportError(msg);
100 }
101 
102 } /* namespace cx */
void reportError(QString msg)
Definition: cxLogger.cpp:71
#define CX_ASSERT(statement)
Definition: cxLogger.h:116
ProcessReporter(QProcess *process, QString name)
void reportSuccess(QString msg)
Definition: cxLogger.cpp:72
void report(QString msg)
Definition: cxLogger.cpp:69
Namespace for all CustusX production code.