///////////////////////////////////////////////////////////////////////////////// // // 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 // for more details. // ///////////////////////////////////////////////////////////////////////////////// #include "RimProcessMonitor.h" #include "RiaLogging.h" #include "RimProcessQueue.h" #include #include //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- RimProcessMonitor::RimProcessMonitor( size_t processId, bool logStdOutErr /*true*/ ) : QObject( nullptr ) , m_processId( processId ) , m_logStdOutErr( logStdOutErr ) { } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- QString RimProcessMonitor::addPrefix( QString message ) { return QString( "Process %1: %2" ).arg( m_processId ).arg( message ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- 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; } RiaLogging::error( addPrefix( errorStr ).toStdString() ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimProcessMonitor::finished( int exitCode, QProcess::ExitStatus exitStatus ) { if ( m_logStdOutErr ) { 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; } RiaLogging::debug( addPrefix( finishStr ).toStdString() ); } RimProcessQueue::onProcessFinished( m_processId ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimProcessMonitor::readyReadStandardError() { if ( QProcess* p = qobject_cast( sender() ) ) { p->setReadChannel( QProcess::StandardError ); while ( p->canReadLine() ) { QString line = p->readLine(); line = line.trimmed(); if ( line.size() == 0 ) continue; m_stdErr.append( line ); if ( m_logStdOutErr ) { RiaLogging::error( addPrefix( line ).toStdString() ); } } } } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimProcessMonitor::readyReadStandardOutput() { if ( QProcess* p = qobject_cast( sender() ) ) { p->setReadChannel( QProcess::StandardOutput ); while ( p->canReadLine() ) { QString line = p->readLine(); line = line.trimmed(); if ( line.size() == 0 ) continue; m_stdOut.append( line ); if ( m_logStdOutErr ) { RiaLogging::info( addPrefix( line ).toStdString() ); } } } } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimProcessMonitor::started() { if ( m_logStdOutErr ) RiaLogging::debug( addPrefix( "Started" ).toStdString() ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimProcessMonitor::clearStdOutErr() { m_stdOut.clear(); m_stdErr.clear(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- const QStringList& RimProcessMonitor::stdOut() const { return m_stdOut; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- const QStringList& RimProcessMonitor::stdErr() const { return m_stdErr; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimProcessMonitor::setProcessId( size_t processId ) { m_processId = processId; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- 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() ); } }