From 237035468f840a4b8c2f3c6db94ab68057e46ac0 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Wed, 21 Dec 2022 17:12:27 +0100 Subject: [PATCH] [darwin] Add back the colored output with additional informations for chip-tool (#24094) --- src/platform/logging/BUILD.gn | 6 +- .../logging/impl/stdio/darwin/Logging.cpp | 72 +++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/platform/logging/impl/stdio/darwin/Logging.cpp diff --git a/src/platform/logging/BUILD.gn b/src/platform/logging/BUILD.gn index 402528315d5b81..505068535c748b 100644 --- a/src/platform/logging/BUILD.gn +++ b/src/platform/logging/BUILD.gn @@ -31,7 +31,11 @@ if (current_os == "android") { stdio_archive = "$root_out_dir/liblogging-stdio.a" static_library("stdio") { - sources = [ "impl/stdio/Logging.cpp" ] + if (chip_device_platform == "darwin") { + sources = [ "impl/stdio/darwin/Logging.cpp" ] + } else { + sources = [ "impl/stdio/Logging.cpp" ] + } deps = [ ":headers", diff --git a/src/platform/logging/impl/stdio/darwin/Logging.cpp b/src/platform/logging/impl/stdio/darwin/Logging.cpp new file mode 100644 index 00000000000000..f5599f5996b570 --- /dev/null +++ b/src/platform/logging/impl/stdio/darwin/Logging.cpp @@ -0,0 +1,72 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace chip { +namespace Logging { +namespace Platform { + +void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char * msg, va_list v) +{ + timeval time; + gettimeofday(&time, nullptr); + long ms = (time.tv_sec * 1000) + (time.tv_usec / 1000); + + uint64_t ktid; + pthread_threadid_np(nullptr, &ktid); + + char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE]; + int32_t prefixLen = + snprintf(formattedMsg, sizeof(formattedMsg), "[%ld] [%lld:%lld] [%s] ", ms, (long long) getpid(), (long long) ktid, module); + if (prefixLen < 0) + { + // This should not happen + return; + } + + if (static_cast(prefixLen) >= sizeof(formattedMsg)) + { + prefixLen = sizeof(formattedMsg) - 1; + } + + vsnprintf(formattedMsg + prefixLen, sizeof(formattedMsg) - static_cast(prefixLen), msg, v); + + switch (category) + { + case kLogCategory_Error: + printf("\033[1;31m%s\033[0m\n", formattedMsg); + break; + case kLogCategory_Progress: + printf("\033[0;32m%s\033[0m\n", formattedMsg); + break; + case kLogCategory_Detail: + printf("\033[0;34m%s\033[0m\n", formattedMsg); + break; + } +} + +} // namespace Platform +} // namespace Logging +} // namespace chip