Vivien Nicolas | a92cbf0 | 2022-12-17 04:55:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2022 Project CHIP Authors |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #import <Matter/Matter.h> |
| 20 | |
| 21 | #import "logging.h" |
| 22 | |
Boris Zbarsky | f8aa1df | 2023-01-03 09:52:49 -0500 | [diff] [blame] | 23 | #include <algorithm> |
Vivien Nicolas | a92cbf0 | 2022-12-17 04:55:10 +0100 | [diff] [blame] | 24 | #include <cstdio> |
Boris Zbarsky | f8aa1df | 2023-01-03 09:52:49 -0500 | [diff] [blame] | 25 | #include <lib/support/logging/CHIPLogging.h> |
Vivien Nicolas | a92cbf0 | 2022-12-17 04:55:10 +0100 | [diff] [blame] | 26 | #include <pthread.h> |
| 27 | |
| 28 | namespace dft { |
| 29 | namespace logging { |
| 30 | |
Boris Zbarsky | f8aa1df | 2023-01-03 09:52:49 -0500 | [diff] [blame] | 31 | void LogMessage(MTRLogType type, NSString * component, NSString * message) |
Vivien Nicolas | a92cbf0 | 2022-12-17 04:55:10 +0100 | [diff] [blame] | 32 | { |
| 33 | auto kLoggingColorError = @"\033[1;31m"; |
| 34 | auto kLoggingColorProgress = @"\033[0;32m"; |
| 35 | auto kLoggingColorDetail = @"\033[0;34m"; |
| 36 | auto kLoggingColorEnd = @"\033[0m"; |
| 37 | |
Boris Zbarsky | f8aa1df | 2023-01-03 09:52:49 -0500 | [diff] [blame] | 38 | NSString * loggingColor = nil; |
| 39 | |
| 40 | switch (type) { |
| 41 | case MTRLogTypeError: |
| 42 | loggingColor = kLoggingColorError; |
| 43 | break; |
| 44 | case MTRLogTypeProgress: |
| 45 | loggingColor = kLoggingColorProgress; |
| 46 | break; |
| 47 | case MTRLogTypeDetail: |
| 48 | loggingColor = kLoggingColorDetail; |
| 49 | break; |
| 50 | } |
| 51 | |
| 52 | auto formatter = [[NSDateFormatter alloc] init]; |
| 53 | formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSS"; |
| 54 | auto formattedDate = [formatter stringFromDate:[NSDate date]]; |
| 55 | |
| 56 | int pid = [[NSProcessInfo processInfo] processIdentifier]; |
| 57 | |
| 58 | auto tid = pthread_mach_thread_np(pthread_self()); |
| 59 | |
| 60 | fprintf(stdout, "%s%s [%d:%u] [%s]: %s%s\n", loggingColor.UTF8String, formattedDate.UTF8String, pid, tid, |
| 61 | component.UTF8String, message.UTF8String, kLoggingColorEnd.UTF8String); |
| 62 | } |
| 63 | |
| 64 | void LogRedirectCallback(const char * moduleName, uint8_t category, const char * format, va_list args) |
| 65 | { |
| 66 | #pragma clang diagnostic push |
| 67 | #pragma clang diagnostic ignored "-Wformat-nonliteral" |
| 68 | // Note: Format using NSString so that '%@' placeholders are supported |
| 69 | NSString * message = [[NSString alloc] initWithFormat:@(format) arguments:args]; |
| 70 | #pragma clang diagnostic pop |
| 71 | |
| 72 | auto type = std::min(static_cast<MTRLogType>(category), MTRLogTypeDetail); |
| 73 | LogMessage(type, @(moduleName), message); |
| 74 | } |
| 75 | |
| 76 | void Setup() |
| 77 | { |
Vivien Nicolas | a92cbf0 | 2022-12-17 04:55:10 +0100 | [diff] [blame] | 78 | MTRSetLogCallback(MTRLogTypeDetail, ^(MTRLogType type, NSString * component, NSString * message) { |
Boris Zbarsky | f8aa1df | 2023-01-03 09:52:49 -0500 | [diff] [blame] | 79 | LogMessage(type, component, message); |
Vivien Nicolas | a92cbf0 | 2022-12-17 04:55:10 +0100 | [diff] [blame] | 80 | }); |
Boris Zbarsky | f8aa1df | 2023-01-03 09:52:49 -0500 | [diff] [blame] | 81 | |
| 82 | // We also have a second copy of the logging core floating around, so |
| 83 | // need to set up a logging redirect on that too. |
| 84 | chip::Logging::SetLogRedirectCallback(&LogRedirectCallback); |
Vivien Nicolas | a92cbf0 | 2022-12-17 04:55:10 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | } |
| 88 | } |