Skip to content

Commit

Permalink
[macOS standalone apps] Add missing autoreleasepool for logging when …
Browse files Browse the repository at this point in the history
…standalone apps are runned on macOS
  • Loading branch information
vivien-apple committed Oct 2, 2024
1 parent 20200ee commit d86f053
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/platform/Darwin/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
#define ChipPlatformLog(MOD, CAT, MSG, ...) \
do \
{ \
ChipPlatformValidateLogFormat(MSG, ##__VA_ARGS__); /* validate once and ignore warnings from os_log() / Log() */ \
ChipPlatformValidateLogFormat(MSG, ##__VA_ARGS__); /* validate once and ignore warnings from Log() */ \
chip::Logging::Platform::LogAny(chip::Logging::kLogModule_##MOD, #MOD, \
static_cast<os_log_type_t>(chip::Logging::Platform::kOSLogCategory_##CAT), MSG, \
##__VA_ARGS__); \
_Pragma("clang diagnostic push"); \
_Pragma("clang diagnostic ignored \"-Wformat\""); \
os_log_with_type(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"); \
} while (0)
Expand Down Expand Up @@ -66,6 +67,7 @@ enum OSLogCategory

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

// Helper constructs for compile-time validation of format strings for C++ / ObjC++ contexts.
// Note that ObjC++ contexts are restricted to NSString style specifiers. Supporting os_log()
Expand Down
37 changes: 29 additions & 8 deletions src/platform/Darwin/Logging.mm
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,36 @@ os_log_t LoggerForModule(chip::Logging::LogModule moduleID, char const * moduleN
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 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]];
@autoreleasepool {
os_log_t logger = LoggerForModule(moduleId, moduleName);
if (os_log_type_enabled(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);
}
}
}

void LogAny(
chip::Logging::LogModule moduleId, char const * moduleName, os_log_type_t type, const char * msg, ...)
{
@autoreleasepool {
os_log_t logger = LoggerForModule(moduleId, moduleName);
if (os_log_type_enabled(logger, type)) {
va_list v;
va_start(v, msg);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral"
NSString * string = [[NSString alloc] initWithFormat:@(msg) arguments:v];
#pragma clang diagnostic pop
va_end(v);

os_log_with_type(logger, type, "%@", string);
}
os_log_with_type(logger, type, "%@", string);
}
}

Expand Down

0 comments on commit d86f053

Please sign in to comment.