Skip to content

Commit

Permalink
[chip-tool] In interactive server mode when a response is sent over t…
Browse files Browse the repository at this point in the history
…he wire it keeps creating new strings instead of concatening to the already existing strings
  • Loading branch information
vivien-apple committed Jun 5, 2023
1 parent 275341e commit 2a41daf
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions examples/chip-tool/commands/interactive/InteractiveCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,48 +153,58 @@ struct InteractiveServerResult
{
auto lock = ScopedLock(mMutex);

std::string resultsStr;
std::stringstream results;
if (mResults.size())
{
for (const auto & result : mResults)
{
resultsStr = resultsStr + result + ",";
results << result << ",";
}

// Remove last comma.
resultsStr.pop_back();
results.seekp(-1, std::ios_base::end);
results << '\0';
}

if (mStatus != EXIT_SUCCESS)
{
if (resultsStr.size())
if (results.tellp())
{
resultsStr = resultsStr + ",";
results << ",";
}
resultsStr = resultsStr + "{ \"error\": \"FAILURE\" }";
results << "{ \"error\": \"FAILURE\" }";
}

std::string logsStr;
std::stringstream logs;
if (mLogs.size())
{
for (const auto & log : mLogs)
{
logsStr = logsStr + "{";
logsStr = logsStr + " \"module\": \"" + log.module + "\",";
logsStr = logsStr + " \"category\": \"" + log.messageType + "\",";
logsStr = logsStr + " \"message\": \"" + log.message + "\"";
logsStr = logsStr + "},";
logs << "{"
" \"module\": \"" +
log.module +
"\","
" \"category\": \"" +
log.messageType +
"\","
" \"message\": \"" +
log.message +
"\""
"},";
}

// Remove last comma.
logsStr.pop_back();
logs.seekp(-1, std::ios_base::end);
logs << '\0';
}

std::string jsonLog;
jsonLog = jsonLog + "{";
jsonLog = jsonLog + " \"results\": [" + resultsStr + "],";
jsonLog = jsonLog + " \"logs\": [" + logsStr + "]";
jsonLog = jsonLog + "}";
auto resultsStr = results.str();
auto logsStr = logs.str();
jsonLog = jsonLog + "{";
jsonLog = jsonLog + " \"results\": [" + resultsStr.c_str() + "],";
jsonLog = jsonLog + " \"logs\": [" + logsStr.c_str() + "]";
jsonLog = jsonLog + "}";

return jsonLog;
}
Expand Down

0 comments on commit 2a41daf

Please sign in to comment.