|
13 | 13 | #include "ghc/filesystem.hpp" |
14 | 14 |
|
15 | 15 | #include <fstream> |
| 16 | +#include <chrono> |
16 | 17 |
|
17 | 18 | namespace |
18 | 19 | { |
@@ -64,37 +65,55 @@ namespace |
64 | 65 | return false; |
65 | 66 | } |
66 | 67 |
|
67 | | - std::vector<char> output; |
| 68 | + int statusCode = 0; |
68 | 69 | do |
69 | 70 | { |
70 | 71 | uint32_t byteRead = 0; |
71 | | - output.resize(output.size() + 4096); |
| 72 | + const int bufferSize = 4096; |
| 73 | + char output[bufferSize] = {0}; |
72 | 74 |
|
73 | 75 | result = System2ReadFromOutput( &runCommandInfo, |
74 | | - output.data() + output.size() - 4096, |
75 | | - 4096 - 1, |
| 76 | + output, |
| 77 | + bufferSize, |
76 | 78 | &byteRead); |
77 | 79 |
|
78 | | - output.resize(output.size() - 4096 + byteRead + 1); |
79 | | - output.back() = '\0'; |
80 | | - } |
81 | | - while(result == SYSTEM2_RESULT_READ_NOT_FINISHED); |
82 | | - |
83 | | - if(result != SYSTEM2_RESULT_SUCCESS) |
84 | | - { |
85 | | - ssLOG_ERROR("Failed to read from output with result: " << result); |
86 | | - ghc::filesystem::remove(scriptDirectory + "/" + std::string(scriptName + exeExt), _); |
87 | | - return false; |
| 80 | + output[byteRead] = '\0'; |
| 81 | + |
| 82 | + //Log the output and continue fetching output |
| 83 | + if(result == SYSTEM2_RESULT_READ_NOT_FINISHED) |
| 84 | + { |
| 85 | + ssLOG_SIMPLE(output); |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + //If we have finished reading the output, check if the command has finished |
| 90 | + if(result == SYSTEM2_RESULT_SUCCESS) |
| 91 | + { |
| 92 | + ssLOG_SIMPLE(output); |
| 93 | + |
| 94 | + result = System2GetCommandReturnValueAsync(&runCommandInfo, &statusCode); |
| 95 | + |
| 96 | + //If the command is not finished, continue reading output |
| 97 | + if(result == SYSTEM2_RESULT_COMMAND_NOT_FINISHED) |
| 98 | + { |
| 99 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 100 | + continue; |
| 101 | + } |
| 102 | + else |
| 103 | + break; |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + ssLOG_ERROR("Failed to read from output with result: " << result); |
| 108 | + ghc::filesystem::remove(scriptDirectory + "/" + std::string(scriptName + exeExt), _); |
| 109 | + return false; |
| 110 | + } |
88 | 111 | } |
89 | | - |
90 | | - ssLOG_SIMPLE(output.data()); |
91 | | - |
92 | | - int statusCode = 0; |
93 | | - result = System2GetCommandReturnValueSync(&runCommandInfo, &statusCode); |
| 112 | + while(true); |
94 | 113 |
|
95 | 114 | if(result != SYSTEM2_RESULT_SUCCESS) |
96 | 115 | { |
97 | | - ssLOG_ERROR("System2GetCommandReturnValueSync failed with result: " << result); |
| 116 | + ssLOG_ERROR("System2GetCommandReturnValueASync failed with result: " << result); |
98 | 117 | ghc::filesystem::remove(scriptDirectory + "/" + std::string(scriptName + exeExt), _); |
99 | 118 | return false; |
100 | 119 | } |
|
0 commit comments