From 9e9fa047f7e782cc48813aa7d9e327bd49006e39 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sat, 7 Aug 2021 00:30:53 +0200 Subject: [PATCH] printf support for cmd executor --- src/fsfw/osal/linux/CommandExecutor.cpp | 27 ++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/fsfw/osal/linux/CommandExecutor.cpp b/src/fsfw/osal/linux/CommandExecutor.cpp index 5d131472..f0b6dd31 100644 --- a/src/fsfw/osal/linux/CommandExecutor.cpp +++ b/src/fsfw/osal/linux/CommandExecutor.cpp @@ -62,8 +62,13 @@ ReturnValue_t CommandExecutor::close() { void CommandExecutor::printLastError(std::string funcName) const { if(lastError != 0) { +#if FSFW_CPP_OSTREAM_ENABLED == 1 sif::error << funcName << " pclose failed with code " << lastError << ": " << strerror(lastError) << std::endl; +#else + sif::printError("%s pclose failed with code %d: %s", funcName, lastError, + strerror(lastError)); +#endif } } @@ -98,15 +103,23 @@ ReturnValue_t CommandExecutor::check(bool& bytesRead) { ssize_t readBytes = read(currentFd, readVec.data(), readVec.size()); if(readBytes == 0) { // Should not happen +#if FSFW_CPP_OSTREAM_ENABLED == 1 sif::warning << "CommandExecutor::check: " "No bytes read after poll event.." << std::endl; +#else + sif::printWarning("CommandExecutor::check: No bytes read after poll event..\n"); +#endif break; } else if(readBytes > 0) { bytesRead = true; if(printOutput) { +#if FSFW_CPP_OSTREAM_ENABLED == 1 // It is assumed the command output is line terminated sif::info << currentCmd << " | " << readVec.data(); +#else + sif::printInfo("%s | %s", currentCmd, readVec.data()); +#endif } if(ringBuffer != nullptr) { ringBuffer->writeData(reinterpret_cast( @@ -120,17 +133,25 @@ ReturnValue_t CommandExecutor::check(bool& bytesRead) { return BYTES_READ; } else { +#if FSFW_CPP_OSTREAM_ENABLED == 1 // Should also not happen sif::warning << "CommandExecutor::check: Error " << errno << ": " << strerror(errno) << std::endl; +#else + sif::printWarning("CommandExecutor::check: Error %d: %s\n", errno, strerror(errno)); +#endif } } else if(waiter.revents & POLLERR) { +#if FSFW_CPP_OSTREAM_ENABLED == 1 sif::warning << "CommandExecuter::check: Poll error" << std::endl; +#else + sif::printWarning("CommandExecuter::check: Poll error\n"); +#endif return COMMAND_ERROR; } else if(waiter.revents & POLLHUP) { - int result = pclose(currentCmdFile); + result = pclose(currentCmdFile); if(result != 0) { lastError = result; return HasReturnvaluesIF::RETURN_FAILED; @@ -165,7 +186,11 @@ ReturnValue_t CommandExecutor::executeBlocking() { while(fgets(readVec.data(), readVec.size(), currentCmdFile) != nullptr) { std::string output(readVec.data()); if(printOutput) { +#if FSFW_CPP_OSTREAM_ENABLED == 1 sif::info << currentCmd << " | " << output; +#else + sif::printInfo("%s | %s", currentCmd, output); +#endif } if(ringBuffer != nullptr) { ringBuffer->writeData(reinterpret_cast(output.data()), output.size());