blob: ee6a6a96b1167bc023d0ca766a6391d87f34314e [file] [log] [blame]
/*
*
* Copyright (c) 2021 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.
*/
#if !__has_feature(objc_arc)
#error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
#include "Logging.h"
#include <lib/support/Span.h>
#import <Foundation/Foundation.h>
#import <dispatch/dispatch.h>
namespace chip {
namespace Logging {
namespace Platform {
struct os_log_s * LoggerForModule(chip::Logging::LogModule moduleID, char const * moduleName)
{
if (moduleID <= kLogModule_NotSpecified || kLogModule_Max <= moduleID) {
moduleID = kLogModule_NotSpecified;
moduleName = "Default";
}
static struct {
dispatch_once_t onceToken;
struct os_log_s * logger;
} cache[kLogModule_Max];
auto & entry = cache[moduleID];
dispatch_once(&entry.onceToken, ^{
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)
{
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(ChipPlatformLogger(logger), type, "%@", string);
}
}
} // namespace Platform
} // namespace Logging
} // namespace chip