Skip to content

Commit 8498dc0

Browse files
Fixing print when script is being run instead of printing when it finish
1 parent 393db0f commit 8498dc0

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

Src/runcpp2/runcpp2.cpp

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "ghc/filesystem.hpp"
1414

1515
#include <fstream>
16+
#include <chrono>
1617

1718
namespace
1819
{
@@ -64,37 +65,55 @@ namespace
6465
return false;
6566
}
6667

67-
std::vector<char> output;
68+
int statusCode = 0;
6869
do
6970
{
7071
uint32_t byteRead = 0;
71-
output.resize(output.size() + 4096);
72+
const int bufferSize = 4096;
73+
char output[bufferSize] = {0};
7274

7375
result = System2ReadFromOutput( &runCommandInfo,
74-
output.data() + output.size() - 4096,
75-
4096 - 1,
76+
output,
77+
bufferSize,
7678
&byteRead);
7779

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+
}
88111
}
89-
90-
ssLOG_SIMPLE(output.data());
91-
92-
int statusCode = 0;
93-
result = System2GetCommandReturnValueSync(&runCommandInfo, &statusCode);
112+
while(true);
94113

95114
if(result != SYSTEM2_RESULT_SUCCESS)
96115
{
97-
ssLOG_ERROR("System2GetCommandReturnValueSync failed with result: " << result);
116+
ssLOG_ERROR("System2GetCommandReturnValueASync failed with result: " << result);
98117
ghc::filesystem::remove(scriptDirectory + "/" + std::string(scriptName + exeExt), _);
99118
return false;
100119
}

0 commit comments

Comments
 (0)