diff --git a/connections/c/BUILD b/connections/c/BUILD index e197a1a0..912ed2a9 100644 --- a/connections/c/BUILD +++ b/connections/c/BUILD @@ -54,7 +54,7 @@ cc_library( "@com_google_absl//absl/types:span", ] + select({ "@platforms//os:platform_ios": [ - "//internal/platform/implementation/apple:logger_writer", + "//internal/platform/implementation/apple:nearby_logger", ], "//conditions:default": [], }), diff --git a/connections/c/nc.cc b/connections/c/nc.cc index de1bde4f..a540399d 100644 --- a/connections/c/nc.cc +++ b/connections/c/nc.cc @@ -50,7 +50,7 @@ #include "internal/platform/file.h" #include "internal/platform/logging.h" #if TARGET_OS_IOS -#include "internal/platform/implementation/apple/nearby_logger_writer.h" +#include "internal/platform/implementation/apple/nearby_logger.h" #endif // TARGET_OS_IOS namespace nearby::connections { @@ -225,10 +225,7 @@ NcContext* GetContext(NC_INSTANCE instance) { NC_INSTANCE NcCreateService() { NcContext nc_context; #if TARGET_OS_IOS -#if DEBUG - absl::SetGlobalVLogLevel(1); -#endif // DEBUG - ::nearby::apple::EnableNearbyLoggerWriter(); + absl::SetGlobalVLogLevel(1); // OS_LOG_TYPE_DEBUG ::nearby::apple::EnableOsLog("com.google.nearby.connections"); #endif // TARGET_OS_IOS diff --git a/connections/swift/NearbyCoreAdapter/BUILD b/connections/swift/NearbyCoreAdapter/BUILD index a335b32b..ddded5b6 100644 --- a/connections/swift/NearbyCoreAdapter/BUILD +++ b/connections/swift/NearbyCoreAdapter/BUILD @@ -37,8 +37,8 @@ objc_library( "//internal/flags:nearby_flags", "//internal/platform:base", "//internal/platform/implementation/apple", # buildcleaner: keep + "//internal/platform/implementation/apple/Log:GNCLogger", "//third_party/apple_frameworks:Foundation", - "//third_party/objective_c/google_toolbox_for_mac:GTM_Logger", ], ) diff --git a/connections/swift/NearbyCoreAdapter/Sources/GNCAdvertisingOptions+CppConversions.mm b/connections/swift/NearbyCoreAdapter/Sources/GNCAdvertisingOptions+CppConversions.mm index 3644bf72..67c6a5f8 100644 --- a/connections/swift/NearbyCoreAdapter/Sources/GNCAdvertisingOptions+CppConversions.mm +++ b/connections/swift/NearbyCoreAdapter/Sources/GNCAdvertisingOptions+CppConversions.mm @@ -18,7 +18,7 @@ #import "connections/swift/NearbyCoreAdapter/Sources/GNCStrategy+Internal.h" #import "connections/swift/NearbyCoreAdapter/Sources/GNCSupportedMediums+CppConversions.h" -#import "GoogleToolboxForMac/GTMLogger.h" +#import "internal/platform/implementation/apple/Log/GNCLogger.h" using ::nearby::connections::AdvertisingOptions; using ::nearby::connections::CppStrategyFromGNCStrategy; @@ -35,7 +35,7 @@ using ::nearby::connections::CppStrategyFromGNCStrategy; advertising_options.low_power = self.lowPower; advertising_options.enforce_topology_constraints = self.enforceTopologyConstraints; if (self.enforceTopologyConstraints) { - GTMLoggerError(@"WARNING: Creating ConnectionOptions with enforceTopologyConstraints = true. " + GNCLoggerError(@"WARNING: Creating ConnectionOptions with enforceTopologyConstraints = true. " "Make sure you know what you're doing!"); } diff --git a/connections/swift/NearbyCoreAdapter/Sources/GNCConnectionOptions+CppConversions.mm b/connections/swift/NearbyCoreAdapter/Sources/GNCConnectionOptions+CppConversions.mm index 36d36300..a7260c26 100644 --- a/connections/swift/NearbyCoreAdapter/Sources/GNCConnectionOptions+CppConversions.mm +++ b/connections/swift/NearbyCoreAdapter/Sources/GNCConnectionOptions+CppConversions.mm @@ -18,7 +18,7 @@ #include "internal/platform/byte_array.h" #import "connections/swift/NearbyCoreAdapter/Sources/GNCSupportedMediums+CppConversions.h" -#import "GoogleToolboxForMac/GTMLogger.h" +#import "internal/platform/implementation/apple/Log/GNCLogger.h" using ::nearby::connections::ConnectionOptions; @@ -33,7 +33,7 @@ using ::nearby::connections::ConnectionOptions; connection_options.low_power = self.lowPower; connection_options.enforce_topology_constraints = self.enforceTopologyConstraints; if (self.enforceTopologyConstraints) { - GTMLoggerError(@"WARNING: Creating ConnectionOptions with enforceTopologyConstraints = true. " + GNCLoggerError(@"WARNING: Creating ConnectionOptions with enforceTopologyConstraints = true. " "Make sure you know what you're doing!"); } diff --git a/internal/platform/implementation/apple/BUILD b/internal/platform/implementation/apple/BUILD index ed8e912f..ebd31a29 100644 --- a/internal/platform/implementation/apple/BUILD +++ b/internal/platform/implementation/apple/BUILD @@ -181,19 +181,18 @@ objc_library( ) objc_library( - name = "logger_writer", + name = "nearby_logger", srcs = [ - "nearby_logger_writer.mm", + "nearby_logger.mm", ], hdrs = [ - "nearby_logger_writer.h", + "nearby_logger.h", ], # Prevent Objective-C++ headers from being pulled into swift. aspect_hints = ["//tools/build_defs/swift:no_module"], deps = [ ":os_log_sink", "//internal/platform:logging", - "//third_party/objective_c/google_toolbox_for_mac:GTM_Logger", ], ) diff --git a/internal/platform/implementation/apple/Tests/BUILD b/internal/platform/implementation/apple/Tests/BUILD index 4dadf2e8..d1d6cafa 100644 --- a/internal/platform/implementation/apple/Tests/BUILD +++ b/internal/platform/implementation/apple/Tests/BUILD @@ -70,27 +70,11 @@ objc_library( ], ) -objc_library( - name = "LoggerWriterTestslib", - testonly = True, - srcs = [ - "nearby_logger_writer_test.mm", - ], - deps = [ - "//internal/platform:logging", - "//internal/platform/implementation/apple:logger_writer", - "//third_party/apple_frameworks:Foundation", - "//third_party/apple_frameworks:XCTest", - "//third_party/objective_c/google_toolbox_for_mac:GTM_Logger", - ], -) - ios_unit_test( name = "PlatformTests", minimum_os_version = IOS_MINIMUM_OS, runner = IOS_LATEST_TEST_RUNNER, deps = [ - ":LoggerWriterTestslib", ":PlatformTestslib", ], ) diff --git a/internal/platform/implementation/apple/Tests/nearby_logger_writer_test.mm b/internal/platform/implementation/apple/Tests/nearby_logger_writer_test.mm deleted file mode 100644 index 1b976aa1..00000000 --- a/internal/platform/implementation/apple/Tests/nearby_logger_writer_test.mm +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright 2025 Google LLC -// -// 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 -// -// https://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 "GoogleToolboxForMac/GTMLogger.h" - -#include "internal/platform/implementation/apple/nearby_logger_writer.h" - -#include -#include - -#include "internal/platform/logging.h" - -class AbslFileLogSink : public absl::LogSink { - public: - AbslFileLogSink() = default; - ~AbslFileLogSink() override = default; - void Send(const absl::LogEntry& entry) override { - logMessage_ = std::string(entry.text_message()); - severity_ = entry.log_severity(); - } - - std::string GetLogMessage() const { return logMessage_; } - absl::LogSeverity GetSeverity() const { return severity_; } - - private: - std::string logMessage_; - absl::LogSeverity severity_ = absl::LogSeverity::kInfo; -}; - -@interface NearbyLoggerWriterTest : XCTestCase -@end - -@implementation NearbyLoggerWriterTest { - std::unique_ptr _logSink; -} - -- (void)setUp { - [super setUp]; - _logSink = std::make_unique(); - absl::SetGlobalVLogLevel(1); - absl::AddLogSink(_logSink.get()); - nearby::apple::EnableNearbyLoggerWriter(); -} - -- (void)tearDown { - absl::RemoveLogSink(_logSink.get()); - _logSink.reset(); - [super tearDown]; -} - -- (void)testInfoLogMessage { - GTMLoggerInfo(@"Hello, world!"); - XCTAssertEqual(_logSink->GetLogMessage(), "Hello, world!"); - XCTAssertEqual(_logSink->GetSeverity(), absl::LogSeverity::kInfo); -} - -- (void)testErrorLogMessage { - GTMLoggerError(@"Error!"); - XCTAssertEqual(_logSink->GetLogMessage(), "Error!"); - XCTAssertEqual(_logSink->GetSeverity(), absl::LogSeverity::kError); -} - -- (void)testVerboseLogMessage { - GTMLoggerDebug(@"Verbose!"); - XCTAssertEqual(_logSink->GetLogMessage(), "Verbose!"); - XCTAssertEqual(_logSink->GetSeverity(), absl::LogSeverity::kInfo); -} - -@end diff --git a/internal/platform/implementation/apple/nearby_logger_writer.h b/internal/platform/implementation/apple/nearby_logger.h similarity index 71% rename from internal/platform/implementation/apple/nearby_logger_writer.h rename to internal/platform/implementation/apple/nearby_logger.h index 6a7f55a5..32983f9b 100644 --- a/internal/platform/implementation/apple/nearby_logger_writer.h +++ b/internal/platform/implementation/apple/nearby_logger.h @@ -12,19 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef PLATFORM_IMPL_APPLE_NEARBY_LOGGER_WRITER_H_ -#define PLATFORM_IMPL_APPLE_NEARBY_LOGGER_WRITER_H_ +#ifndef PLATFORM_IMPL_APPLE_NEARBY_LOGGER_H_ +#define PLATFORM_IMPL_APPLE_NEARBY_LOGGER_H_ #include namespace nearby { namespace apple { -// NearbyLoggerWriter will handle GTM logs as ABSL logs. -// The SDK developer can setup ABSL log listener to receive all logs from Nearby -// connections. -void EnableNearbyLoggerWriter(); - // Enables the OS log output as the given subsystem. The method should be called // once during the application lifetime. void EnableOsLog(const std::string& subsystem); @@ -32,4 +27,4 @@ void EnableOsLog(const std::string& subsystem); } // namespace apple } // namespace nearby -#endif // PLATFORM_IMPL_APPLE_NEARBY_LOGGER_WRITER_H_ +#endif // PLATFORM_IMPL_APPLE_NEARBY_LOGGER_H_ diff --git a/internal/platform/implementation/apple/nearby_logger.mm b/internal/platform/implementation/apple/nearby_logger.mm new file mode 100644 index 00000000..8a881523 --- /dev/null +++ b/internal/platform/implementation/apple/nearby_logger.mm @@ -0,0 +1,37 @@ +// Copyright 2025 Google LLC +// +// 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 +// +// https://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 "internal/platform/implementation/apple/nearby_logger.h" + +#include +#include + +#include "internal/platform/implementation/apple/os_log_sink.h" +#include "internal/platform/logging.h" + +namespace nearby { +namespace apple { +namespace { + +OsLogSink *GetOsLogSink(const std::string &subsystem) { + static OsLogSink *sink = new OsLogSink(subsystem); + return sink; +} + +} // namespace + +void EnableOsLog(const std::string &subsystem) { absl::AddLogSink(GetOsLogSink(subsystem)); } + +} // namespace apple +} // namespace nearby diff --git a/internal/platform/implementation/apple/nearby_logger_writer.mm b/internal/platform/implementation/apple/nearby_logger_writer.mm deleted file mode 100644 index f3ce1d9e..00000000 --- a/internal/platform/implementation/apple/nearby_logger_writer.mm +++ /dev/null @@ -1,186 +0,0 @@ -// Copyright 2025 Google LLC -// -// 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 -// -// https://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 "internal/platform/implementation/apple/nearby_logger_writer.h" - -#include -#include - -#include "internal/platform/implementation/apple/os_log_sink.h" -#include "internal/platform/logging.h" - -#import "GoogleToolboxForMac/GTMLogger.h" - -NS_ASSUME_NONNULL_BEGIN - -/** - * Log message with function name and message content. - * The log message will be used to pass data from NearbyLoggerFormatter to NearbyLoggerWriter. Due - * to the limitation of GTMLogger, we cannot get the file name and line number. - */ -@interface NearbyLogMessage : NSObject - -@property(nonatomic, readonly) uint32_t messageIndex; -@property(nonatomic, readonly) NSString *functionName; -@property(nonatomic, readonly) NSString *logMessage; - -/** - * Initializes the log message with message index, function name and message content. - * - * @param messageIndex The message index. - * @param functionName The function name. - * @param logMessage The log message. - * @return The initialized log message. - */ -- (instancetype)initWithMessageIndex:(uint32_t)messageIndex - functionName:(NSString *)functionName - logMessage:(NSString *)logMessage; -@end - -@implementation NearbyLogMessage -- (instancetype)initWithMessageIndex:(uint32_t)messageIndex - functionName:(NSString *)functionName - logMessage:(NSString *)logMessage { - if (self = [super init]) { - _messageIndex = messageIndex; - _functionName = [functionName copy]; - _logMessage = [logMessage copy]; - } - return self; -} -@end - -/** - * The customised log writer for `GTMLogWriter` is used to write the GTM log message as ABSL log - * message. - */ -@interface NearbyLoggerWriter : NSObject -@end - -#pragma mark - NearbyLoggerWriter - -@implementation NearbyLoggerWriter { - NSMutableArray *_messages; -} - -- (instancetype)initWithMessages:(NSMutableArray *)logMessages { - if ((self = [super init])) { - _messages = logMessages; - } - return self; -} - -- (void)logMessage:(NSString *)message level:(GTMLoggerLevel)level { - @synchronized(_messages) { - if (_messages.count == 0) { - return; - } - - uint32_t messageIndex = [message intValue]; - NearbyLogMessage *message = [_messages firstObject]; - [_messages removeObjectAtIndex:0]; - if (messageIndex > message.messageIndex) { - return; - } - - switch (level) { - case kGTMLoggerLevelDebug: - VLOG(1).AtLocation([message.functionName UTF8String], 0) - << std::string([message.logMessage UTF8String]); - break; - case kGTMLoggerLevelInfo: - LOG(INFO).AtLocation([message.functionName UTF8String], 0) - << std::string([message.logMessage UTF8String]); - break; - case kGTMLoggerLevelError: - LOG(ERROR).AtLocation([message.functionName UTF8String], 0) - << std::string([message.logMessage UTF8String]); - break; - case kGTMLoggerLevelAssert: - LOG(FATAL).AtLocation([message.functionName UTF8String], 0) - << std::string([message.logMessage UTF8String]); - break; - case kGTMLoggerLevelUnknown: - LOG(INFO).AtLocation([message.functionName UTF8String], 0) - << std::string([message.logMessage UTF8String]); - break; - } - } -} - -@end // NearbyLoggerWriter - -/** - * The customised log formatter for `GTMLogBasicFormatter`. It is used to get function name and - * message content. - */ -@interface NearbyLoggerFormatter : GTMLogBasicFormatter { - uint32_t _messageIndex; - NSMutableArray *_messages; -} -@end - -#pragma mark - NearbyLoggerFormatter -@implementation NearbyLoggerFormatter - -- (instancetype)initWithMessages:(NSMutableArray *)logMessages { - if (self = [super init]) { - _messageIndex = 0; - _messages = logMessages; - } - return self; -} - -- (NSString *)stringForFunc:(nullable NSString *)func - withFormat:(NSString *)format - valist:(va_list)args - level:(GTMLoggerLevel)level { - @synchronized(_messages) { - NSString *prettyNameForFunc = [self prettyNameForFunc:func]; - NSString *logMessage = [super stringForFunc:func withFormat:format valist:args level:level]; - NearbyLogMessage *message = [[NearbyLogMessage alloc] initWithMessageIndex:_messageIndex - functionName:prettyNameForFunc - logMessage:logMessage]; - [_messages addObject:message]; - return [NSString stringWithFormat:@"%d", (int)_messageIndex++]; - } -} - -@end // NearbyLoggerFormatter - -NS_ASSUME_NONNULL_END - -namespace nearby { -namespace apple { -namespace { - -OsLogSink *GetOsLogSink(const std::string &subsystem) { - static OsLogSink *sink = new OsLogSink(subsystem); - return sink; -} - -} // namespace - -void EnableNearbyLoggerWriter() { - NSMutableArray *tupleArray = [[NSMutableArray alloc] init]; - GTMLogger.sharedLogger = - [GTMLogger loggerWithWriter:[[NearbyLoggerWriter alloc] initWithMessages:tupleArray] - formatter:[[NearbyLoggerFormatter alloc] initWithMessages:tupleArray] - filter:nil]; -} - -void EnableOsLog(const std::string &subsystem) { absl::AddLogSink(GetOsLogSink(subsystem)); } - -} // namespace apple -} // namespace nearby