From 154457618263c1a2d0621197a60e7f9283889a66 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Sat, 17 Dec 2022 04:55:10 +0100 Subject: [PATCH] [darwin-framework-tool] Add back colored logging (#24110) --- examples/darwin-framework-tool/BUILD.gn | 1 + .../darwin-framework-tool/logging/logging.h | 25 +++++++ .../darwin-framework-tool/logging/logging.mm | 65 +++++++++++++++++++ examples/darwin-framework-tool/main.mm | 8 +-- 4 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 examples/darwin-framework-tool/logging/logging.h create mode 100644 examples/darwin-framework-tool/logging/logging.mm diff --git a/examples/darwin-framework-tool/BUILD.gn b/examples/darwin-framework-tool/BUILD.gn index c11da0692038df..76bfeead5a6e2b 100644 --- a/examples/darwin-framework-tool/BUILD.gn +++ b/examples/darwin-framework-tool/BUILD.gn @@ -147,6 +147,7 @@ executable("darwin-framework-tool") { "commands/provider/OTASoftwareUpdateInteractive.mm", "commands/storage/Commands.h", "commands/storage/StorageManagementCommand.mm", + "logging/logging.mm", "main.mm", ] diff --git a/examples/darwin-framework-tool/logging/logging.h b/examples/darwin-framework-tool/logging/logging.h new file mode 100644 index 00000000000000..0380286dda4f7c --- /dev/null +++ b/examples/darwin-framework-tool/logging/logging.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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. + * + */ + +namespace dft { +namespace logging { + +void Setup(); + +} +} // namespace dft diff --git a/examples/darwin-framework-tool/logging/logging.mm b/examples/darwin-framework-tool/logging/logging.mm new file mode 100644 index 00000000000000..d57a5190be7216 --- /dev/null +++ b/examples/darwin-framework-tool/logging/logging.mm @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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. + * + */ + +#import + +#import "logging.h" + +#include +#include + +namespace dft { +namespace logging { + + void Setup() + { + auto kLoggingColorError = @"\033[1;31m"; + auto kLoggingColorProgress = @"\033[0;32m"; + auto kLoggingColorDetail = @"\033[0;34m"; + auto kLoggingColorEnd = @"\033[0m"; + + MTRSetLogCallback(MTRLogTypeDetail, ^(MTRLogType type, NSString * component, NSString * message) { + NSString * loggingColor = nil; + + switch (type) { + case MTRLogTypeError: + loggingColor = kLoggingColorError; + break; + case MTRLogTypeProgress: + loggingColor = kLoggingColorProgress; + break; + case MTRLogTypeDetail: + loggingColor = kLoggingColorDetail; + break; + } + + auto formatter = [[NSDateFormatter alloc] init]; + formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSS"; + auto formattedDate = [formatter stringFromDate:[NSDate date]]; + + int pid = [[NSProcessInfo processInfo] processIdentifier]; + + auto tid = pthread_mach_thread_np(pthread_self()); + + fprintf(stdout, "%s%s [%d:%u] [%s]: %s%s\n", loggingColor.UTF8String, formattedDate.UTF8String, pid, tid, + component.UTF8String, message.UTF8String, kLoggingColorEnd.UTF8String); + }); + } + +} +} diff --git a/examples/darwin-framework-tool/main.mm b/examples/darwin-framework-tool/main.mm index 58a37ebb2ce42b..dd191d39e441e1 100644 --- a/examples/darwin-framework-tool/main.mm +++ b/examples/darwin-framework-tool/main.mm @@ -18,6 +18,8 @@ #import +#import "logging/logging.h" + #include "commands/common/Commands.h" #include "commands/interactive/Commands.h" #include "commands/pairing/Commands.h" @@ -28,14 +30,10 @@ #include #include -#include - int main(int argc, const char * argv[]) { @autoreleasepool { - MTRSetLogCallback(MTRLogTypeDetail, ^(MTRLogType type, NSString * component, NSString * message) { - fprintf(stdout, "CHIP:%s: %s\n", component.UTF8String, message.UTF8String); - }); + dft::logging::Setup(); Commands commands; registerCommandsPairing(commands);