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 (#27006)
  • Loading branch information
vivien-apple authored and pull[bot] committed Jan 19, 2024
1 parent 3aad362 commit 3293442
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions examples/chip-tool/commands/interactive/InteractiveCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,50 +153,56 @@ struct InteractiveServerResult
{
auto lock = ScopedLock(mMutex);

std::string resultsStr;
std::stringstream content;
content << "{";

content << " \"results\": [";
if (mResults.size())
{
for (const auto & result : mResults)
{
resultsStr = resultsStr + result + ",";
content << result << ",";
}

// Remove last comma.
resultsStr.pop_back();
content.seekp(-1, std::ios_base::end);
}

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

std::string logsStr;
content << "\"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 + "},";
content << "{"
" \"module\": \"" +
log.module +
"\","
" \"category\": \"" +
log.messageType +
"\","
" \"message\": \"" +
log.message +
"\""
"},";
}

// Remove last comma.
logsStr.pop_back();
content.seekp(-1, std::ios_base::end);
}
content << "]";

std::string jsonLog;
jsonLog = jsonLog + "{";
jsonLog = jsonLog + " \"results\": [" + resultsStr + "],";
jsonLog = jsonLog + " \"logs\": [" + logsStr + "]";
jsonLog = jsonLog + "}";

return jsonLog;
content << "}";
return content.str();
}
};

Expand Down

0 comments on commit 3293442

Please sign in to comment.