Remove dependents on GTMLogger

PiperOrigin-RevId: 766849674
This commit is contained in:
Guogang Li
2025-06-03 15:42:07 -07:00
committed by Copybara-Service
parent a45c18eb14
commit 2f07f1ceee
11 changed files with 51 additions and 306 deletions
+1 -1
View File
@@ -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": [],
}),
+2 -5
View File
@@ -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
+1 -1
View File
@@ -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",
],
)
@@ -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!");
}
@@ -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!");
}
+3 -4
View File
@@ -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",
],
)
@@ -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",
],
)
@@ -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 <XCTest/XCTest.h>
#import "GoogleToolboxForMac/GTMLogger.h"
#include "internal/platform/implementation/apple/nearby_logger_writer.h"
#include <string>
#include <utility>
#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<AbslFileLogSink> _logSink;
}
- (void)setUp {
[super setUp];
_logSink = std::make_unique<AbslFileLogSink>();
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
@@ -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 <string>
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_
@@ -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 <cstdint>
#include <string>
#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
@@ -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 <cstdint>
#include <string>
#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 <GTMLogWriter>
@end
#pragma mark - NearbyLoggerWriter
@implementation NearbyLoggerWriter {
NSMutableArray<NearbyLogMessage *> *_messages;
}
- (instancetype)initWithMessages:(NSMutableArray<NearbyLogMessage *> *)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<NearbyLogMessage *> *_messages;
}
@end
#pragma mark - NearbyLoggerFormatter
@implementation NearbyLoggerFormatter
- (instancetype)initWithMessages:(NSMutableArray<NearbyLogMessage *> *)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<NearbyLogMessage *> *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