2021-05-18 14:28:54 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// Copyright (C) 2021 Equinor ASA
|
|
|
|
|
//
|
|
|
|
|
// ResInsight is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
//
|
|
|
|
|
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
|
|
|
|
// for more details.
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
#include "RimProcessMonitor.h"
|
|
|
|
|
|
|
|
|
|
#include "RiaLogging.h"
|
|
|
|
|
|
2026-04-17 19:55:50 +02:00
|
|
|
#include "RimProcessQueue.h"
|
|
|
|
|
|
2021-05-18 14:28:54 +02:00
|
|
|
#include <QProcess>
|
|
|
|
|
#include <QtCore/QtCore>
|
|
|
|
|
|
2025-06-11 15:43:53 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2026-04-17 19:55:50 +02:00
|
|
|
RimProcessMonitor::RimProcessMonitor( size_t processId, bool logStdOutErr /*true*/ )
|
2021-05-18 14:28:54 +02:00
|
|
|
: QObject( nullptr )
|
|
|
|
|
, m_processId( processId )
|
2025-06-11 15:43:53 +02:00
|
|
|
, m_logStdOutErr( logStdOutErr )
|
2021-05-18 14:28:54 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-11 15:43:53 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2021-05-18 14:28:54 +02:00
|
|
|
QString RimProcessMonitor::addPrefix( QString message )
|
|
|
|
|
{
|
|
|
|
|
return QString( "Process %1: %2" ).arg( m_processId ).arg( message );
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-11 15:43:53 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2021-05-18 14:28:54 +02:00
|
|
|
void RimProcessMonitor::error( QProcess::ProcessError error )
|
|
|
|
|
{
|
|
|
|
|
QString errorStr;
|
|
|
|
|
|
|
|
|
|
switch ( error )
|
|
|
|
|
{
|
|
|
|
|
case QProcess::FailedToStart:
|
|
|
|
|
errorStr = "Failed to start";
|
|
|
|
|
break;
|
|
|
|
|
case QProcess::Crashed:
|
|
|
|
|
errorStr = "Crashed";
|
|
|
|
|
break;
|
|
|
|
|
case QProcess::Timedout:
|
|
|
|
|
errorStr = "Timed out";
|
|
|
|
|
break;
|
|
|
|
|
case QProcess::ReadError:
|
|
|
|
|
errorStr = "Read error";
|
|
|
|
|
break;
|
|
|
|
|
case QProcess::WriteError:
|
|
|
|
|
errorStr = "Write error";
|
|
|
|
|
break;
|
|
|
|
|
case QProcess::UnknownError:
|
|
|
|
|
default:
|
|
|
|
|
errorStr = "Unknown";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-06 12:00:10 +02:00
|
|
|
RiaLogging::error( addPrefix( errorStr ).toStdString() );
|
2021-05-18 14:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-11 15:43:53 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2021-05-18 14:28:54 +02:00
|
|
|
void RimProcessMonitor::finished( int exitCode, QProcess::ExitStatus exitStatus )
|
|
|
|
|
{
|
2026-04-17 19:55:50 +02:00
|
|
|
if ( m_logStdOutErr )
|
2021-05-18 14:28:54 +02:00
|
|
|
{
|
2026-04-17 19:55:50 +02:00
|
|
|
QString finishStr;
|
|
|
|
|
switch ( exitStatus )
|
|
|
|
|
{
|
|
|
|
|
case QProcess::NormalExit:
|
|
|
|
|
finishStr = QString( "Normal exit, code %1" ).arg( exitCode );
|
|
|
|
|
break;
|
|
|
|
|
case QProcess::CrashExit:
|
|
|
|
|
default:
|
|
|
|
|
finishStr = QString( "Crash exit, code %1" ).arg( exitCode );
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-05-06 12:00:10 +02:00
|
|
|
RiaLogging::debug( addPrefix( finishStr ).toStdString() );
|
2021-05-18 14:28:54 +02:00
|
|
|
}
|
2026-04-17 19:55:50 +02:00
|
|
|
|
|
|
|
|
RimProcessQueue::onProcessFinished( m_processId );
|
2021-05-18 14:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-11 15:43:53 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2021-05-18 14:28:54 +02:00
|
|
|
void RimProcessMonitor::readyReadStandardError()
|
|
|
|
|
{
|
2025-10-18 02:42:11 +02:00
|
|
|
if ( QProcess* p = qobject_cast<QProcess*>( sender() ) )
|
2021-05-18 14:28:54 +02:00
|
|
|
{
|
2025-10-18 02:42:11 +02:00
|
|
|
p->setReadChannel( QProcess::StandardError );
|
|
|
|
|
while ( p->canReadLine() )
|
2025-06-11 15:43:53 +02:00
|
|
|
{
|
2025-10-18 02:42:11 +02:00
|
|
|
QString line = p->readLine();
|
|
|
|
|
line = line.trimmed();
|
|
|
|
|
if ( line.size() == 0 ) continue;
|
|
|
|
|
m_stdErr.append( line );
|
|
|
|
|
if ( m_logStdOutErr )
|
|
|
|
|
{
|
2026-05-06 12:00:10 +02:00
|
|
|
RiaLogging::error( addPrefix( line ).toStdString() );
|
2025-10-18 02:42:11 +02:00
|
|
|
}
|
2025-06-11 15:43:53 +02:00
|
|
|
}
|
2021-05-18 14:28:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-11 15:43:53 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2021-05-18 14:28:54 +02:00
|
|
|
void RimProcessMonitor::readyReadStandardOutput()
|
|
|
|
|
{
|
2025-10-18 02:42:11 +02:00
|
|
|
if ( QProcess* p = qobject_cast<QProcess*>( sender() ) )
|
2021-05-18 14:28:54 +02:00
|
|
|
{
|
2025-10-18 02:42:11 +02:00
|
|
|
p->setReadChannel( QProcess::StandardOutput );
|
|
|
|
|
while ( p->canReadLine() )
|
2025-06-11 15:43:53 +02:00
|
|
|
{
|
2025-10-18 02:42:11 +02:00
|
|
|
QString line = p->readLine();
|
|
|
|
|
line = line.trimmed();
|
|
|
|
|
if ( line.size() == 0 ) continue;
|
|
|
|
|
m_stdOut.append( line );
|
|
|
|
|
if ( m_logStdOutErr )
|
|
|
|
|
{
|
2026-05-06 12:00:10 +02:00
|
|
|
RiaLogging::info( addPrefix( line ).toStdString() );
|
2025-10-18 02:42:11 +02:00
|
|
|
}
|
2025-06-11 15:43:53 +02:00
|
|
|
}
|
2021-05-18 14:28:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-11 15:43:53 +02:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2021-05-18 14:28:54 +02:00
|
|
|
void RimProcessMonitor::started()
|
|
|
|
|
{
|
2026-05-06 12:00:10 +02:00
|
|
|
if ( m_logStdOutErr ) RiaLogging::debug( addPrefix( "Started" ).toStdString() );
|
2025-06-11 15:43:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
void RimProcessMonitor::clearStdOutErr()
|
|
|
|
|
{
|
|
|
|
|
m_stdOut.clear();
|
|
|
|
|
m_stdErr.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2026-09-16 18:41:26 +02:00
|
|
|
const QStringList& RimProcessMonitor::stdOut() const
|
2025-06-11 15:43:53 +02:00
|
|
|
{
|
|
|
|
|
return m_stdOut;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2026-09-16 18:41:26 +02:00
|
|
|
const QStringList& RimProcessMonitor::stdErr() const
|
2025-06-11 15:43:53 +02:00
|
|
|
{
|
|
|
|
|
return m_stdErr;
|
2021-05-18 14:28:54 +02:00
|
|
|
}
|
2025-10-18 02:42:11 +02:00
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2026-04-17 19:55:50 +02:00
|
|
|
void RimProcessMonitor::setProcessId( size_t processId )
|
2025-10-18 02:42:11 +02:00
|
|
|
{
|
|
|
|
|
m_processId = processId;
|
|
|
|
|
}
|
2026-09-16 18:41:26 +02:00
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
void RimProcessMonitor::appendStdOut( QString line )
|
|
|
|
|
{
|
|
|
|
|
m_stdOut.append( line );
|
|
|
|
|
if ( m_logStdOutErr )
|
|
|
|
|
{
|
|
|
|
|
RiaLogging::info( addPrefix( line ).toStdString() );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
///
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
void RimProcessMonitor::appendStdErr( QString line )
|
|
|
|
|
{
|
|
|
|
|
m_stdErr.append( line );
|
|
|
|
|
if ( m_logStdOutErr )
|
|
|
|
|
{
|
|
|
|
|
RiaLogging::error( addPrefix( line ).toStdString() );
|
|
|
|
|
}
|
|
|
|
|
}
|