diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 3ded9fa02..16817db6b 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -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; diff --git a/src/cli/cli_output.cpp b/src/cli/cli_output.cpp index 7e65eb00c..99a3e6fcf 100644 --- a/src/cli/cli_output.cpp +++ b/src/cli/cli_output.cpp @@ -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 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++) diff --git a/src/cli/cli_output.hpp b/src/cli/cli_output.hpp index b501bbadf..d0f95a6d8 100644 --- a/src/cli/cli_output.hpp +++ b/src/cli/cli_output.hpp @@ -41,6 +41,7 @@ #include #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 };