Skip to content

Commit

Permalink
[Darwin] Print the queue name (or a shorter version for common queues…
Browse files Browse the repository at this point in the history
…) when using the stdio logging backend (project-chip#36764)

* [Darwin] Print the queue name (or a shorter version for common queues) when using the stdio logging backend

* [darwin-framework-tool] Print the queue name (or a shorter version for common queues) when logging
  • Loading branch information
vivien-apple authored Dec 10, 2024
1 parent 8606290 commit 43167a0
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 4 deletions.
15 changes: 12 additions & 3 deletions examples/darwin-framework-tool/logging/logging.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <algorithm>
#include <cstdio>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/Darwin/DispatchQueueNames.h>
#include <pthread.h>

namespace dft {
Expand Down Expand Up @@ -56,9 +57,17 @@ void LogMessage(MTRLogType type, NSString * component, NSString * message)
int pid = [[NSProcessInfo processInfo] processIdentifier];

auto tid = pthread_mach_thread_np(pthread_self());

fprintf(stdout, "%s%s [%d:%u] [%s]: %s%s\n", loggingColor.UTF8String, formattedDate.UTF8String, pid, tid,
component.UTF8String, message.UTF8String, kLoggingColorEnd.UTF8String);
const char * label = chip::darwin::queues::CurrentLabel();

fprintf(stdout, "%s%s [%d:%u:%s] [%s]: %s%s\n",
loggingColor.UTF8String,
formattedDate.UTF8String,
pid,
tid,
label,
component.UTF8String,
message.UTF8String,
kLoggingColorEnd.UTF8String);
}

void LogRedirectCallback(const char * moduleName, uint8_t category, const char * format, va_list args)
Expand Down
2 changes: 2 additions & 0 deletions src/platform/Darwin/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ source_set("tracing") {

static_library("logging") {
sources = [
"DispatchQueueNames.cpp",
"DispatchQueueNames.h",
"Logging.h",
"Logging.mm",
]
Expand Down
81 changes: 81 additions & 0 deletions src/platform/Darwin/DispatchQueueNames.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2024 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 "DispatchQueueNames.h"

#include <dispatch/dispatch.h>

namespace {
constexpr const char * kMainQueue = "com.apple.main-thread";
constexpr const char * kChipQueue = "org.csa-iot.matter.workqueue";
constexpr const char * kBleQueue = "org.csa-iot.matter.framework.ble.workqueue";
constexpr const char * kXPCQueue = "org.csa-iot.matter.framework.xpc.workqueue";
constexpr const char * kDeviceQueue = "org.csa-iot.matter.framework.device.workqueue";
constexpr const char * kOTAProviderQueue = "org.csa-iot.matter.framework.otaprovider.workqueue";
constexpr const char * kDeviceAttestationQueue = "org.csa-iot.matter.framework.device_attestation.workqueue";

constexpr const char * kMainQueueShort = "main";
constexpr const char * kChipQueueShort = "chip";
constexpr const char * kBleQueueShort = "ble";
constexpr const char * kXPCQueueShort = "xpc";
constexpr const char * kDeviceQueueShort = "device";
constexpr const char * kOTAProviderQueueShort = "ota-provider";
constexpr const char * kDeviceAttestationQueueShort = "device-attestation";
} // namespace

namespace chip {
namespace darwin {
namespace queues {

const char * CurrentLabel()
{
const char * label = dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL);

if (strcmp(label, kMainQueue) == 0)
{
label = kMainQueueShort;
}
else if (strcmp(label, kChipQueue) == 0)
{
label = kChipQueueShort;
}
else if (strcmp(label, kBleQueue) == 0)
{
label = kBleQueueShort;
}
else if (strcmp(label, kXPCQueue) == 0)
{
label = kXPCQueueShort;
}
else if (strcmp(label, kDeviceQueue) == 0)
{
label = kDeviceQueueShort;
}
else if (strcmp(label, kOTAProviderQueue) == 0)
{
label = kOTAProviderQueueShort;
}
else if (strcmp(label, kDeviceAttestationQueue) == 0)
{
label = kDeviceAttestationQueueShort;
}

return label;
}

} // namespace queues
} // namespace darwin
} // namespace chip
27 changes: 27 additions & 0 deletions src/platform/Darwin/DispatchQueueNames.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2024 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.
*/

#pragma once

namespace chip {
namespace darwin {
namespace queues {

const char * CurrentLabel();

} // namespace queues
} // namespace darwin
} // namespace chip
4 changes: 3 additions & 1 deletion src/platform/logging/impl/Stdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <time.h>

#if defined(__APPLE__)
#include <platform/Darwin/DispatchQueueNames.h>
#include <pthread.h>
#include <unistd.h>
#elif defined(__gnu_linux__)
Expand Down Expand Up @@ -65,7 +66,8 @@ void LogV(const char * module, uint8_t category, const char * msg, va_list v)
#if defined(__APPLE__)
uint64_t ktid;
pthread_threadid_np(nullptr, &ktid);
printf("[%lld:%lld] ", static_cast<long long>(getpid()), static_cast<long long>(ktid));
const char * label = darwin::queues::CurrentLabel();
printf("[%lld:%lld:%s] ", static_cast<long long>(getpid()), static_cast<long long>(ktid), label);
#elif defined(__gnu_linux__) && !defined(__NuttX__)
// TODO: change to getpid() and gettid() after glib upgrade
printf("[%lld:%lld] ", static_cast<long long>(syscall(SYS_getpid)), static_cast<long long>(syscall(SYS_gettid)));
Expand Down

0 comments on commit 43167a0

Please sign in to comment.