From 581b1894f2b17c8d0fec25a8b6a7e468f9f2035a Mon Sep 17 00:00:00 2001 From: mlangkabel Date: Mon, 27 May 2019 14:05:02 +0200 Subject: [PATCH] logic: write output of Python indexer processes to log file --- .../indexer/TaskExecuteCustomCommands.cpp | 39 +++++----- src/lib_gui/utility/utilityApp.cpp | 75 ++++++++++++++----- src/lib_gui/utility/utilityApp.h | 2 +- 3 files changed, 76 insertions(+), 40 deletions(-) diff --git a/src/lib/data/indexer/TaskExecuteCustomCommands.cpp b/src/lib/data/indexer/TaskExecuteCustomCommands.cpp index 245678e5..f447c3bb 100644 --- a/src/lib/data/indexer/TaskExecuteCustomCommands.cpp +++ b/src/lib/data/indexer/TaskExecuteCustomCommands.cpp @@ -219,37 +219,32 @@ void TaskExecuteCustomCommands::runIndexerCommand(std::shared_ptrbeforeErrorRecording(); - std::wstring processOutput; std::wstring errorMessage; - const int result = utility::executeProcessAndGetExitCode(command, {}, m_projectDirectory, -1, &processOutput, &errorMessage); + const int result = utility::executeProcessAndGetExitCode(command, {}, m_projectDirectory, -1, true, &errorMessage); m_storage->afterErrorRecording(); - if (errorMessage.size() > 0 || processOutput.size() > 3 || result != 0) + if (result == 0 && errorMessage.empty()) { - if (result == 0 && errorMessage.empty()) + std::wstring message = L"Process returned successfully.\n"; + LOG_INFO(message); + } + else + { + std::wstring statusText = L"command \"" + indexerCommand->getCustomCommand() + L"\" returned"; + if (result != 0) { - std::wstring message = L"Process returned successfully"; - if (processOutput.empty()) - { - message += L"."; - } - else - { - message += L" with message \"" + processOutput + L"\"."; - } - message += L"\n"; - LOG_INFO(message); + statusText += L" code \"" + std::to_wstring(result) + L"\""; } - else + if (!errorMessage.empty()) { - LOG_ERROR_STREAM(<< "process returned \"" << result << "\" with message:\n" << utility::encodeToUtf8(processOutput)); - MessageShowStatus().dispatch(); - MessageStatus( - L"command \"" + indexerCommand->getCustomCommand() + L"\" returned code \"" + std::to_wstring(result) + L"\"" + - L" with message \"" + errorMessage + L"\"" + - L" and output \"" + processOutput + L"\".", true, false, true).dispatch(); + statusText += L" with message \"" + errorMessage + L"\""; } + statusText += L"."; + + LOG_ERROR(statusText); + MessageShowStatus().dispatch(); + MessageStatus(statusText, true, false, true).dispatch(); } indexedSourceFileCount++; diff --git a/src/lib_gui/utility/utilityApp.cpp b/src/lib_gui/utility/utilityApp.cpp index a7301257..ab15452d 100644 --- a/src/lib_gui/utility/utilityApp.cpp +++ b/src/lib_gui/utility/utilityApp.cpp @@ -13,6 +13,39 @@ #include "UserPaths.h" #include "utilityString.h" +namespace +{ + void logProcessStreams(QProcess& process, std::wstring& outputBuffer, std::wstring& errorBuffer) + { + { + outputBuffer += QString(process.readAllStandardOutput()).toStdWString(); + std::vector outputLines = utility::split>(outputBuffer, L"\n"); + for (size_t i = 0; i < outputLines.size() - 1; i++) + { + if (outputLines[i].back() == L'\r') + { + outputLines[i].pop_back(); + } + LOG_INFO_BARE(L"Process output: " + outputLines[i]); + } + outputBuffer = outputLines.back(); + } + { + errorBuffer += QString(process.readAllStandardError()).toStdWString(); + std::vector errorLines = utility::split>(errorBuffer, L"\n"); + for (size_t i = 0; i < errorLines.size() - 1; i++) + { + if (errorLines[i].back() == L'\r') + { + errorLines[i].pop_back(); + } + LOG_ERROR_BARE(L"Process error: " + errorLines[i]); + } + errorBuffer = errorLines.back(); + } + } +} + namespace utility { std::mutex s_runningProcessesMutex; @@ -108,7 +141,7 @@ int utility::executeProcessAndGetExitCode( const std::vector& commandArguments, const FilePath& workingDirectory, const int timeout, - std::wstring* processOutput, + bool logProcessOutput, std::wstring* errorMessage ){ QProcess process; @@ -163,28 +196,36 @@ int utility::executeProcessAndGetExitCode( s_runningProcesses.insert(&process); } - process.waitForFinished(timeout); + { + std::wstring outputBuffer; + std::wstring errorBuffer; + if (timeout == -1) + { + while (!process.waitForFinished(1000)) + { + if (logProcessOutput) + { + logProcessStreams(process, outputBuffer, errorBuffer); + } + } + } + else + { + process.waitForFinished(timeout); + } + + if (logProcessOutput) + { + logProcessStreams(process, outputBuffer, errorBuffer); + } + } { std::lock_guard lock(s_runningProcessesMutex); s_runningProcesses.erase(&process); } - int exitCode = process.exitCode(); - - if (processOutput != nullptr) - { - if (exitCode != 0) - { - *processOutput = utility::trim(QString(process.readAllStandardError()).toStdWString()); - } - else - { - *processOutput = utility::trim(QString(process.readAll()).toStdWString()); - } - } - + const int exitCode = process.exitCode(); process.close(); - return exitCode; } diff --git a/src/lib_gui/utility/utilityApp.h b/src/lib_gui/utility/utilityApp.h index fd0b2f1f..667146c4 100644 --- a/src/lib_gui/utility/utilityApp.h +++ b/src/lib_gui/utility/utilityApp.h @@ -19,7 +19,7 @@ namespace utility const std::vector& commandArguments, const FilePath& workingDirectory = FilePath(), const int timeout = 30000, - std::wstring* processOutput = nullptr, + bool logProcessOutput = false, std::wstring* errorMessage = nullptr );