diff --git a/src/platform/logging/BUILD.gn b/src/platform/logging/BUILD.gn index 6b914cab7357d2..632c4ee5353224 100644 --- a/src/platform/logging/BUILD.gn +++ b/src/platform/logging/BUILD.gn @@ -109,11 +109,7 @@ if (current_os == "android") { stdio_archive = "$root_out_dir/liblogging-stdio.a" source_set("stdio") { - if (chip_device_platform == "darwin") { - sources = [ "impl/stdio/darwin/Logging.cpp" ] - } else { - sources = [ "impl/stdio/Logging.cpp" ] - } + sources = [ "impl/stdio/Logging.cpp" ] deps = [ ":headers", diff --git a/src/platform/logging/impl/stdio/Logging.cpp b/src/platform/logging/impl/stdio/Logging.cpp index c403c29b913293..26e050f0286f9f 100644 --- a/src/platform/logging/impl/stdio/Logging.cpp +++ b/src/platform/logging/impl/stdio/Logging.cpp @@ -4,17 +4,65 @@ #include +#include #include +#include + +#if defined(__APPLE__) +#include +#include +#elif defined(__gnu_linux__) +#include +#include +#endif namespace chip { namespace Logging { namespace Platform { +// TODO: investigate whether pw_chrono or pw_log could be used here void LogV(const char * module, uint8_t category, const char * msg, va_list v) { - printf("CHIP:%s: ", module); + // Stdout needs to be locked, because it's printed in pieces +#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) + flockfile(stdout); +#endif + + switch (category) + { + case kLogCategory_Error: + printf("\033[1;31m"); + break; + case kLogCategory_Progress: + printf("\033[0;32m"); + break; + case kLogCategory_Detail: + printf("\033[0;34m"); + break; + } + +#if defined(__APPLE__) || defined(__gnu_linux__) + timespec ts; + timespec_get(&ts, TIME_UTC); + printf("[%lld.%03ld] ", static_cast(ts.tv_sec), static_cast(ts.tv_nsec / 1000000)); +#endif + +#if defined(__APPLE__) + uint64_t ktid; + pthread_threadid_np(nullptr, &ktid); + printf("[%lld:%lld] ", static_cast(getpid()), static_cast(ktid)); +#elif defined(__gnu_linux__) && !defined(__NuttX__) + // TODO: change to getpid() and gettid() after glib upgrade + printf("[%lld:%lld] ", static_cast(syscall(SYS_getpid)), static_cast(syscall(SYS_gettid))); +#endif + + printf("[%s] ", module); vprintf(msg, v); - printf("\n"); + printf("\033[0m\n"); + +#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) + funlockfile(stdout); +#endif } } // namespace Platform diff --git a/src/platform/logging/impl/stdio/darwin/Logging.cpp b/src/platform/logging/impl/stdio/darwin/Logging.cpp deleted file mode 100644 index 49c58eea1d7bba..00000000000000 --- a/src/platform/logging/impl/stdio/darwin/Logging.cpp +++ /dev/null @@ -1,72 +0,0 @@ -/* - * - * 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 - -namespace chip { -namespace Logging { -namespace Platform { - -void 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 diff --git a/src/test_driver/linux-cirque/helper/CHIPTestBase.py b/src/test_driver/linux-cirque/helper/CHIPTestBase.py index 7fd0c81e86f3ca..ea45a62221e7e5 100644 --- a/src/test_driver/linux-cirque/helper/CHIPTestBase.py +++ b/src/test_driver/linux-cirque/helper/CHIPTestBase.py @@ -145,7 +145,7 @@ def reset_thread_devices(self, devices: Union[List[str], str]): self.assertTrue(self.wait_for_device_output( device_id, "Thread Border Router started on AIL", 10)) self.assertTrue(self.wait_for_device_output( - device_id, "CHIP:SVR: Server Listening...", 15)) + device_id, "[SVR] Server Listening...", 15)) # Clear default Thread network commissioning data self.logger.info("Resetting thread network on {}".format( self.get_device_pretty_id(device_id)))