Skip to content

Commit af1b0ee

Browse files
Darwin: Avoid autorelease handling around LoggerForModule (project-chip#35884)
We don't want the LoggerForModule() implementation to call retain/autorelease on the logger, because its unnecessary, and because LoggerForModule() can be called from a C++ context without an auto-release pool in place. To avoid this, we can have LoggerForModule() return a raw pointer, and handle the bridge cast to os_log_t on the caller side. This also avoids unnecessary code on the caller side that would otherwise attempt to rescue the presumed-autoreleased logger object from the ARP.
1 parent 0320653 commit af1b0ee

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/platform/Darwin/Logging.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
ChipPlatformValidateLogFormat(MSG, ##__VA_ARGS__); /* validate once and ignore warnings from os_log() / Log() */ \
3232
_Pragma("clang diagnostic push"); \
3333
_Pragma("clang diagnostic ignored \"-Wformat\""); \
34-
os_log_with_type(chip::Logging::Platform::LoggerForModule(chip::Logging::kLogModule_##MOD, #MOD), \
34+
os_log_with_type(ChipPlatformLogger(::chip::Logging::Platform::LoggerForModule(chip::Logging::kLogModule_##MOD, #MOD)), \
3535
static_cast<os_log_type_t>(chip::Logging::Platform::kOSLogCategory_##CAT), MSG, ##__VA_ARGS__); \
3636
ChipInternalLogImpl(MOD, CHIP_LOG_CATEGORY_##CAT, MSG, ##__VA_ARGS__); \
3737
_Pragma("clang diagnostic pop"); \
@@ -40,8 +40,8 @@
4040
#define ChipPlatformLogByteSpan(MOD, CAT, DATA) \
4141
do \
4242
{ \
43-
chip::Logging::Platform::LogByteSpan(chip::Logging::kLogModule_##MOD, #MOD, \
44-
static_cast<os_log_type_t>(chip::Logging::Platform::kOSLogCategory_##CAT), DATA); \
43+
::chip::Logging::Platform::LogByteSpan(chip::Logging::kLogModule_##MOD, #MOD, \
44+
static_cast<os_log_type_t>(chip::Logging::Platform::kOSLogCategory_##CAT), DATA); \
4545
ChipInternalLogByteSpanImpl(MOD, CHIP_LOG_CATEGORY_##CAT, DATA); \
4646
} while (0)
4747

@@ -64,7 +64,15 @@ enum OSLogCategory
6464
kOSLogCategory_AUTOMATION = OS_LOG_TYPE_DEFAULT,
6565
};
6666

67-
os_log_t LoggerForModule(chip::Logging::LogModule moduleId, char const * moduleName);
67+
// Note: A raw pointer is used here instead of os_log_t to avoid an unwanted retain/autorelease
68+
// in the function itself, as well as unnecessary code to rescue the object from the ARP in callers.
69+
struct os_log_s * LoggerForModule(chip::Logging::LogModule moduleId, char const * moduleName);
70+
#ifdef __OBJC__
71+
#define ChipPlatformLogger(log) ((__bridge os_log_t)(log))
72+
#else
73+
#define ChipPlatformLogger(log) (log)
74+
#endif
75+
6876
void LogByteSpan(chip::Logging::LogModule moduleId, char const * moduleName, os_log_type_t type, const chip::ByteSpan & span);
6977

7078
// Helper constructs for compile-time validation of format strings for C++ / ObjC++ contexts.

src/platform/Darwin/Logging.mm

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
namespace Logging {
3131
namespace Platform {
3232

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

4040
static struct {
4141
dispatch_once_t onceToken;
42-
os_log_t logger;
42+
struct os_log_s * logger;
4343
} cache[kLogModule_Max];
4444
auto & entry = cache[moduleID];
4545
dispatch_once(&entry.onceToken, ^{
46-
entry.logger = os_log_create("com.csa.matter", moduleName);
46+
entry.logger = (__bridge_retained struct os_log_s *) os_log_create("com.csa.matter", moduleName);
4747
});
4848
return entry.logger;
4949
}
5050

5151
void LogByteSpan(
5252
chip::Logging::LogModule moduleId, char const * moduleName, os_log_type_t type, const chip::ByteSpan & span)
5353
{
54-
os_log_t logger = LoggerForModule(moduleId, moduleName);
55-
if (os_log_type_enabled(logger, type)) {
54+
auto * logger = LoggerForModule(moduleId, moduleName);
55+
if (os_log_type_enabled(ChipPlatformLogger(logger), type)) {
5656
auto size = span.size();
5757
auto data = span.data();
5858
NSMutableString * string = [[NSMutableString alloc] initWithCapacity:(size * 6)]; // 6 characters per byte
5959
for (size_t i = 0; i < size; i++) {
6060
[string appendFormat:((i % 8 != 7) ? @"0x%02x, " : @"0x%02x,\n"), data[i]];
6161
}
62-
os_log_with_type(logger, type, "%@", string);
62+
os_log_with_type(ChipPlatformLogger(logger), type, "%@", string);
6363
}
6464
}
6565

0 commit comments

Comments
 (0)