blob: a596ee0eae2a9cb874d8ea2af52f69f80eae08fe [file] [log] [blame]
Vivien Nicolasa92cbf02022-12-17 04:55:10 +01001/*
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 Zbarskyf8aa1df2023-01-03 09:52:49 -050023#include <algorithm>
Vivien Nicolasa92cbf02022-12-17 04:55:10 +010024#include <cstdio>
Boris Zbarskyf8aa1df2023-01-03 09:52:49 -050025#include <lib/support/logging/CHIPLogging.h>
Vivien Nicolasa92cbf02022-12-17 04:55:10 +010026#include <pthread.h>
27
28namespace dft {
29namespace logging {
30
Boris Zbarskyf8aa1df2023-01-03 09:52:49 -050031 void LogMessage(MTRLogType type, NSString * component, NSString * message)
Vivien Nicolasa92cbf02022-12-17 04:55:10 +010032 {
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 Zbarskyf8aa1df2023-01-03 09:52:49 -050038 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 Nicolasa92cbf02022-12-17 04:55:10 +010078 MTRSetLogCallback(MTRLogTypeDetail, ^(MTRLogType type, NSString * component, NSString * message) {
Boris Zbarskyf8aa1df2023-01-03 09:52:49 -050079 LogMessage(type, component, message);
Vivien Nicolasa92cbf02022-12-17 04:55:10 +010080 });
Boris Zbarskyf8aa1df2023-01-03 09:52:49 -050081
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 Nicolasa92cbf02022-12-17 04:55:10 +010085 }
86
87}
88}