[cli] skip CLI prompt when emitting command input/output to logs (#7030)

This commit updates CLI to not include the CLI prompt string "> " when
emitting the command's input and output to the logs under the config
`OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_ENABLE`. It also changes how
the input command is logged to avoid logging empty lines or extra
spaces in between the args.
This commit is contained in:
Abtin Keshavarzian
2021-09-26 21:51:54 -07:00
committed by GitHub
parent 47efcc6745
commit e20a0a0cff
3 changed files with 43 additions and 29 deletions
+15 -23
View File
@@ -280,13 +280,11 @@ void Interpreter::ProcessLine(char *aBuf)
VerifyOrExit(StringLength(aBuf, kMaxLineLength) <= kMaxLineLength - 1, error = OT_ERROR_PARSE);
#if OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_ENABLE
otLogNoteCli("Input: %s", aBuf);
#endif
SuccessOrExit(error = Utils::CmdLineParser::ParseCmd(aBuf, args, kMaxArgs));
VerifyOrExit(!args[0].IsEmpty(), mCommandIsPending = false);
LogInput(args);
#if OPENTHREAD_CONFIG_DIAG_ENABLE
if (otDiagIsEnabled(GetInstancePtr()) && (args[0] != "diag"))
{
@@ -4940,7 +4938,14 @@ void Interpreter::OutputPrompt(void)
{
static const char sPrompt[] = "> ";
// The `OutputFormat()` below is adding the prompt which is not
// part of any command output, so we set the `EmittingCommandOutput`
// flag to false to avoid it being included in the command output
// log (under `OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_ENABLE`).
SetEmittingCommandOutput(false);
OutputFormat("%s", sPrompt);
SetEmittingCommandOutput(true);
}
void Interpreter::HandleTimer(Timer &aTimer)
@@ -5009,19 +5014,12 @@ extern "C" void otCliPlatLogv(otLogLevel aLogLevel, otLogRegion aLogRegion, cons
VerifyOrExit(Interpreter::IsInitialized());
#if OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_ENABLE
// CLI output can be used for logging. The `IsLogging` flag is
// used to indicate whether it is being used for a CLI command
// output or for logging.
Interpreter::GetInterpreter().SetIsLogging(true);
#endif
// CLI output is being used for logging, so we set the flag
// `EmittingCommandOutput` to false indicate this.
Interpreter::GetInterpreter().SetEmittingCommandOutput(false);
Interpreter::GetInterpreter().OutputFormatV(aFormat, aArgs);
Interpreter::GetInterpreter().OutputLine("");
#if OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_ENABLE
Interpreter::GetInterpreter().SetIsLogging(false);
#endif
Interpreter::GetInterpreter().SetEmittingCommandOutput(true);
exit:
return;
@@ -5034,15 +5032,9 @@ extern "C" void otCliPlatLogLine(otLogLevel aLogLevel, otLogRegion aLogRegion, c
VerifyOrExit(Interpreter::IsInitialized());
#if OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_ENABLE
Interpreter::GetInterpreter().SetIsLogging(true);
#endif
Interpreter::GetInterpreter().SetEmittingCommandOutput(false);
Interpreter::GetInterpreter().OutputLine(aLogLine);
#if OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_ENABLE
Interpreter::GetInterpreter().SetIsLogging(false);
#endif
Interpreter::GetInterpreter().SetEmittingCommandOutput(true);
exit:
return;
+16 -2
View File
@@ -53,7 +53,7 @@ Output::Output(otInstance *aInstance, otCliOutputCallback aCallback, void *aCall
, mCallbackContext(aCallbackContext)
#if OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_ENABLE
, mOutputLength(0)
, mIsLogging(false)
, mEmittingCommandOutput(true)
#endif
{
}
@@ -233,7 +233,7 @@ void Output::OutputFormatV(const char *aFormat, va_list aArguments)
mCallback(mCallbackContext, aFormat, aArguments);
#if OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_ENABLE
VerifyOrExit(!IsLogging());
VerifyOrExit(mEmittingCommandOutput);
charsWritten = vsnprintf(&mOutputString[mOutputLength], sizeof(mOutputString) - mOutputLength, aFormat, args);
@@ -312,6 +312,20 @@ exit:
#endif // OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_ENABLE
}
#if OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_ENABLE
void Output::LogInput(const Arg *aArgs)
{
String<kInputOutputLogStringSize> inputString;
for (bool isFirst = true; !aArgs->IsEmpty(); aArgs++, isFirst = false)
{
inputString.Append(isFirst ? "%s" : " %s", aArgs->GetCString());
}
otLogNoteCli("Input: %s", inputString.AsCString());
}
#endif
void Output::OutputTableHeader(uint8_t aNumColumns, const char *const aTitles[], const uint8_t aWidths[])
{
for (uint8_t index = 0; index < aNumColumns; index++)
+12 -4
View File
@@ -41,6 +41,7 @@
#include <openthread/cli.h>
#include "cli_config.h"
#include "utils/parse_cmdline.hpp"
namespace ot {
namespace Cli {
@@ -292,14 +293,21 @@ public:
}
protected:
typedef Utils::CmdLineParser::Arg Arg;
void OutputFormatV(const char *aFormat, va_list aArguments);
#if OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_ENABLE
bool IsLogging(void) const { return mIsLogging; }
void SetIsLogging(bool aIsLogging) { mIsLogging = aIsLogging; }
void LogInput(const Arg *aArgs);
void SetEmittingCommandOutput(bool aEmittingOutput) { mEmittingCommandOutput = aEmittingOutput; }
#else
void LogInput(const Arg *) {}
void SetEmittingCommandOutput(bool) {}
#endif
private:
static constexpr uint16_t kInputOutputLogStringSize = OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_LOG_STRING_SIZE;
void OutputTableHeader(uint8_t aNumColumns, const char *const aTitles[], const uint8_t aWidths[]);
void OutputTableSeparator(uint8_t aNumColumns, const uint8_t aWidths[]);
@@ -307,9 +315,9 @@ private:
otCliOutputCallback mCallback;
void * mCallbackContext;
#if OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_ENABLE
char mOutputString[OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_LOG_STRING_SIZE];
char mOutputString[kInputOutputLogStringSize];
uint16_t mOutputLength;
bool mIsLogging;
bool mEmittingCommandOutput;
#endif
};