From 2474274d86314deeef879b235082d0e0d25f6192 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Tue, 6 Jun 2023 18:24:43 +0200 Subject: [PATCH] [chip-tool] In interactive server mode when a response is sent over the wire it keeps creating new strings instead of concatening to the already existing strings (#27006) --- .../interactive/InteractiveCommands.cpp | 46 +++++++++++-------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/examples/chip-tool/commands/interactive/InteractiveCommands.cpp b/examples/chip-tool/commands/interactive/InteractiveCommands.cpp index 84e543afada9ba..41fcf731ebc7a3 100644 --- a/examples/chip-tool/commands/interactive/InteractiveCommands.cpp +++ b/examples/chip-tool/commands/interactive/InteractiveCommands.cpp @@ -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(); } };