diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 1f66809df..361365961 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -251,6 +251,8 @@ const struct Command Interpreter::sCommands[] = { {"version", &Interpreter::ProcessVersion}, }; +Interpreter *Interpreter::sInterpreter = nullptr; + Interpreter::Interpreter(Instance *aInstance) : mUserCommands(nullptr) , mUserCommandsLength(0) @@ -4679,8 +4681,12 @@ extern "C" void otCliPlatLogv(otLogLevel aLogLevel, otLogRegion aLogRegion, cons OT_UNUSED_VARIABLE(aLogLevel); OT_UNUSED_VARIABLE(aLogRegion); + VerifyOrExit(Interpreter::IsInitialized(), OT_NOOP); + Interpreter::GetInterpreter().OutputFormatV(aFormat, aArgs); Interpreter::GetInterpreter().OutputFormat("\r\n"); +exit: + return; } } // namespace Cli diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index 51ae3f508..7b4d9e178 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -115,7 +115,20 @@ public: * @returns A reference to the interpreter object. * */ - static Interpreter &GetInterpreter(void); + static Interpreter &GetInterpreter(void) + { + OT_ASSERT(sInterpreter != nullptr); + + return *sInterpreter; + } + + /** + * This method returns whether the interpreter is initialized. + * + * @returns Whether the interpreter is initialized. + * + */ + static bool IsInitialized(void) { return sInterpreter != nullptr; } /** * This method interprets a CLI command. @@ -236,6 +249,9 @@ public: */ void SetUserCommands(const otCliCommand *aCommands, uint8_t aLength); +protected: + static Interpreter *sInterpreter; + private: enum { diff --git a/src/cli/cli_console.cpp b/src/cli/cli_console.cpp index b2f5647a6..55ef6bfeb 100644 --- a/src/cli/cli_console.cpp +++ b/src/cli/cli_console.cpp @@ -69,18 +69,11 @@ extern "C" void otPlatUartSendDone(void) { } -Console *Console::sConsole = nullptr; - -Interpreter &Interpreter::GetInterpreter(void) -{ - return *Console::sConsole; -} - void Console::Initialize(otInstance *aInstance, otCliConsoleOutputCallback aCallback, void *aContext) { Instance *instance = static_cast(aInstance); - sConsole = new (&sCliConsoleRaw) Console(instance, aCallback, aContext); + Interpreter::sInterpreter = new (&sCliConsoleRaw) Console(instance, aCallback, aContext); } Console::Console(Instance *aInstance, otCliConsoleOutputCallback aCallback, void *aContext) diff --git a/src/cli/cli_uart.cpp b/src/cli/cli_uart.cpp index d9dd2bbd4..b07f32f7c 100644 --- a/src/cli/cli_uart.cpp +++ b/src/cli/cli_uart.cpp @@ -105,17 +105,10 @@ namespace Cli { static OT_DEFINE_ALIGNED_VAR(sCliUartRaw, sizeof(Uart), uint64_t); -Uart *Uart::sUart = nullptr; - -Interpreter &Interpreter::GetInterpreter(void) -{ - return *Uart::sUart; -} - void Uart::Initialize(otInstance *aInstance) { - Instance *instance = static_cast(aInstance); - sUart = new (&sCliUartRaw) Uart(instance); + Instance *instance = static_cast(aInstance); + Interpreter::sInterpreter = new (&sCliUartRaw) Uart(instance); } Uart::Uart(Instance *aInstance)