Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Darwin: Avoid autorelease handling around LoggerForModule #35884

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/platform/Darwin/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
ChipPlatformValidateLogFormat(MSG, ##__VA_ARGS__); /* validate once and ignore warnings from os_log() / Log() */ \
_Pragma("clang diagnostic push"); \
_Pragma("clang diagnostic ignored \"-Wformat\""); \
os_log_with_type(chip::Logging::Platform::LoggerForModule(chip::Logging::kLogModule_##MOD, #MOD), \
os_log_with_type(ChipPlatformLogger(::chip::Logging::Platform::LoggerForModule(chip::Logging::kLogModule_##MOD, #MOD)), \
static_cast<os_log_type_t>(chip::Logging::Platform::kOSLogCategory_##CAT), MSG, ##__VA_ARGS__); \
ChipInternalLogImpl(MOD, CHIP_LOG_CATEGORY_##CAT, MSG, ##__VA_ARGS__); \
_Pragma("clang diagnostic pop"); \
Expand All @@ -40,8 +40,8 @@
#define ChipPlatformLogByteSpan(MOD, CAT, DATA) \
do \
{ \
chip::Logging::Platform::LogByteSpan(chip::Logging::kLogModule_##MOD, #MOD, \
static_cast<os_log_type_t>(chip::Logging::Platform::kOSLogCategory_##CAT), DATA); \
::chip::Logging::Platform::LogByteSpan(chip::Logging::kLogModule_##MOD, #MOD, \
static_cast<os_log_type_t>(chip::Logging::Platform::kOSLogCategory_##CAT), DATA); \
ChipInternalLogByteSpanImpl(MOD, CHIP_LOG_CATEGORY_##CAT, DATA); \
} while (0)

Expand All @@ -64,7 +64,15 @@ enum OSLogCategory
kOSLogCategory_AUTOMATION = OS_LOG_TYPE_DEFAULT,
};

os_log_t LoggerForModule(chip::Logging::LogModule moduleId, char const * moduleName);
// Note: A raw pointer is used here instead of os_log_t to avoid an unwanted retain/autorelease
// in the function itself, as well as unnecessary code to rescue the object from the ARP in callers.
struct os_log_s * LoggerForModule(chip::Logging::LogModule moduleId, char const * moduleName);
#ifdef __OBJC__
#define ChipPlatformLogger(log) ((__bridge os_log_t)(log))
#else
#define ChipPlatformLogger(log) (log)
#endif

void LogByteSpan(chip::Logging::LogModule moduleId, char const * moduleName, os_log_type_t type, const chip::ByteSpan & span);

// Helper constructs for compile-time validation of format strings for C++ / ObjC++ contexts.
Expand Down
12 changes: 6 additions & 6 deletions src/platform/Darwin/Logging.mm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
namespace Logging {
namespace Platform {

os_log_t LoggerForModule(chip::Logging::LogModule moduleID, char const * moduleName)
struct os_log_s * LoggerForModule(chip::Logging::LogModule moduleID, char const * moduleName)
{
if (moduleID <= kLogModule_NotSpecified || kLogModule_Max <= moduleID) {
moduleID = kLogModule_NotSpecified;
Expand All @@ -39,27 +39,27 @@ os_log_t LoggerForModule(chip::Logging::LogModule moduleID, char const * moduleN

static struct {
dispatch_once_t onceToken;
os_log_t logger;
struct os_log_s * logger;
} cache[kLogModule_Max];
auto & entry = cache[moduleID];
dispatch_once(&entry.onceToken, ^{
entry.logger = os_log_create("com.csa.matter", moduleName);
entry.logger = (__bridge_retained struct os_log_s *) os_log_create("com.csa.matter", moduleName);
});
return entry.logger;
}

void LogByteSpan(
chip::Logging::LogModule moduleId, char const * moduleName, os_log_type_t type, const chip::ByteSpan & span)
{
os_log_t logger = LoggerForModule(moduleId, moduleName);
if (os_log_type_enabled(logger, type)) {
auto * logger = LoggerForModule(moduleId, moduleName);
if (os_log_type_enabled(ChipPlatformLogger(logger), type)) {
auto size = span.size();
auto data = span.data();
NSMutableString * string = [[NSMutableString alloc] initWithCapacity:(size * 6)]; // 6 characters per byte
for (size_t i = 0; i < size; i++) {
[string appendFormat:((i % 8 != 7) ? @"0x%02x, " : @"0x%02x,\n"), data[i]];
}
os_log_with_type(logger, type, "%@", string);
os_log_with_type(ChipPlatformLogger(logger), type, "%@", string);
}
}

Expand Down
Loading