From 3d9371afe1c423c952d29514826380317ec46afa Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Fri, 23 May 2025 09:01:22 -0700 Subject: [PATCH] Remove NSLog from apple platform implementation PiperOrigin-RevId: 762444541 --- .../Source/Central/GNSCentralManager.h | 2 +- .../Ble/Sockets/Source/Shared/GNSSocket.h | 12 ++--- .../Sockets/Source/Shared/GNSWeavePacket.h | 6 +-- .../GNSPeripheralServiceManagerTest.m | 49 +++++++++---------- .../implementation/apple/Mediums/GNCLeaks.m | 3 +- .../implementation/apple/ble_socket.mm | 3 -- 6 files changed, 35 insertions(+), 40 deletions(-) diff --git a/internal/platform/implementation/apple/Mediums/Ble/Sockets/Source/Central/GNSCentralManager.h b/internal/platform/implementation/apple/Mediums/Ble/Sockets/Source/Central/GNSCentralManager.h index 5ebdbf2c..fbd267a3 100644 --- a/internal/platform/implementation/apple/Mediums/Ble/Sockets/Source/Central/GNSCentralManager.h +++ b/internal/platform/implementation/apple/Mediums/Ble/Sockets/Source/Central/GNSCentralManager.h @@ -70,7 +70,7 @@ NS_ASSUME_NONNULL_BEGIN * [centralPeer socketWithPairingCharacteristic:shouldAddPairingCharacteristics * completion:^(GNSSocket *mySocket, NSError *error) { * if (error) { - * NSLog(@"Error to get the socket %@", error); + * GTMLoggerInfo(@"Error to get the socket %@", error); * return; * } * mySocket.delegate = mySocketDelegate; diff --git a/internal/platform/implementation/apple/Mediums/Ble/Sockets/Source/Shared/GNSSocket.h b/internal/platform/implementation/apple/Mediums/Ble/Sockets/Source/Shared/GNSSocket.h index 875181a7..12342277 100644 --- a/internal/platform/implementation/apple/Mediums/Ble/Sockets/Source/Shared/GNSSocket.h +++ b/internal/platform/implementation/apple/Mediums/Ble/Sockets/Source/Shared/GNSSocket.h @@ -26,7 +26,7 @@ typedef void (^GNSProgressHandler)(float progress); * (when used with GNSPeripheralServiceManager) or when the socket is created (when used with * GNSCentralPeer). */ -@protocol GNSSocketDelegate +@protocol GNSSocketDelegate /** * Called when the socket is ready to send or receive data. @@ -69,16 +69,16 @@ typedef void (^GNSProgressHandler)(float progress); * * * To receive data: * - (void)socket:(GNSSocket *)socket didReceiveData:(NSData *)data { - * NSLog(@"Data received from central %@", data); + * GTMLoggerInfo(@"Data received from central %@", data); * ... * } * * * To send data: * GNSErrorHandler completionHandler = ^(NSError *error) { * if (error) { - * NSLog(@"Failed to send data") + * GTMLoggerInfo(@"Failed to send data") * } else { - * NSLog(@"data has been sent"); + * GTMLoggerInfo(@"data has been sent"); * } * } * [socket sendData:dataToSend @@ -89,10 +89,10 @@ typedef void (^GNSProgressHandler)(float progress); * * * Once the socket is disconnected (by -[GNSSocket disconnect] or by the peer): * - (void)socket:(GNSSocket *)socket didDisconnectWithError:(NSError *)error { - * NSLog(@"Socket disconnected, by peer"); + * GTMLoggerInfo(@"Socket disconnected, by peer"); * ... * } -*/ + */ @interface GNSSocket : NSObject /** diff --git a/internal/platform/implementation/apple/Mediums/Ble/Sockets/Source/Shared/GNSWeavePacket.h b/internal/platform/implementation/apple/Mediums/Ble/Sockets/Source/Shared/GNSWeavePacket.h index 2138c774..9bd65826 100644 --- a/internal/platform/implementation/apple/Mediums/Ble/Sockets/Source/Shared/GNSWeavePacket.h +++ b/internal/platform/implementation/apple/Mediums/Ble/Sockets/Source/Shared/GNSWeavePacket.h @@ -47,15 +47,15 @@ typedef NS_ENUM(UInt8, GNSWeaveControlCommand) { * - (void)didReceivedData:(NSData *)data { * GNSWeavePacket *packet = [GNSWeavePacket parseData:data error:nil]; * if ([packet visitWithHandler:self context:nil]) { - * NSLog(@"This class can handle this packet."); + * GTMLoggerInfo(@"This class can handle this packet."); * } else { - * NSLog(@"Unexpected packet received."); + * GTMLoggerInfo(@"Unexpected packet received."); * } * } * * - (void)handleConnectionRequestPacket:(GNSWeaveConnectionRequestPacket *)packet * context:(nullable id)context { - * NSLog(@"Connection request packet received."); + * GTMLoggerInfo(@"Connection request packet received."); * ... * } * @end diff --git a/internal/platform/implementation/apple/Mediums/Ble/Sockets/Tests/Peripheral/GNSPeripheralServiceManagerTest.m b/internal/platform/implementation/apple/Mediums/Ble/Sockets/Tests/Peripheral/GNSPeripheralServiceManagerTest.m index ba0fdf3d..295ad4c2 100644 --- a/internal/platform/implementation/apple/Mediums/Ble/Sockets/Tests/Peripheral/GNSPeripheralServiceManagerTest.m +++ b/internal/platform/implementation/apple/Mediums/Ble/Sockets/Tests/Peripheral/GNSPeripheralServiceManagerTest.m @@ -19,6 +19,7 @@ #import "internal/platform/implementation/apple/Mediums/ble/Sockets/Source/Shared/GNSSocket+Private.h" #import "internal/platform/implementation/apple/Mediums/ble/Sockets/Source/Shared/GNSUtils.h" #import "internal/platform/implementation/apple/Mediums/ble/Sockets/Source/Shared/GNSWeavePacket.h" +#import "GoogleToolboxForMac/GTMLogger.h" #import "third_party/objective_c/ocmock/v3/Source/OCMock/OCMock.h" @interface GNSPeripheralServiceManagerTest : XCTestCase { @@ -39,21 +40,21 @@ - (void)setUp { _mocksToVerify = [NSMutableArray array]; - _serviceUUID = [CBUUID UUIDWithString:@"3C672799-2B3F-4D93-9E57-29D5C5B01092"];; + _serviceUUID = [CBUUID UUIDWithString:@"3C672799-2B3F-4D93-9E57-29D5C5B01092"]; _peripheralManagerMock = OCMStrictClassMock([GNSPeripheralManager class]); _cbPeripheralManagerMock = OCMStrictClassMock([CBPeripheralManager class]); _socketDelegateMock = OCMStrictProtocolMock(@protocol(GNSSocketDelegate)); _centralMaximumUpdateValueLength = 100; _packetSize = 100; OCMStub([_peripheralManagerMock cbPeripheralManager]).andReturn(_cbPeripheralManagerMock); - _peripheralServiceManager = [[GNSPeripheralServiceManager alloc] - initWithBleServiceUUID:_serviceUUID - addPairingCharacteristic:NO - shouldAcceptSocketHandler:^BOOL(GNSSocket *socket) { - _receivedSocket = socket; - socket.delegate = _socketDelegateMock; - return self->_shouldAcceptSocket; - }]; + _peripheralServiceManager = + [[GNSPeripheralServiceManager alloc] initWithBleServiceUUID:_serviceUUID + addPairingCharacteristic:NO + shouldAcceptSocketHandler:^BOOL(GNSSocket *socket) { + _receivedSocket = socket; + socket.delegate = _socketDelegateMock; + return self->_shouldAcceptSocket; + }]; XCTAssertEqualObjects(_peripheralServiceManager.serviceUUID, _serviceUUID); [_peripheralServiceManager addedToPeripheralManager:_peripheralManagerMock bleServiceAddedCompletion:nil]; @@ -76,13 +77,13 @@ } - (void)testServiceManagerAdded { - GNSPeripheralServiceManager *peripheralServiceManager = [[GNSPeripheralServiceManager alloc] - initWithBleServiceUUID:_serviceUUID - addPairingCharacteristic:NO - shouldAcceptSocketHandler:^BOOL(GNSSocket *socket) { - _receivedSocket = socket; - return _shouldAcceptSocket; - }]; + GNSPeripheralServiceManager *peripheralServiceManager = + [[GNSPeripheralServiceManager alloc] initWithBleServiceUUID:_serviceUUID + addPairingCharacteristic:NO + shouldAcceptSocketHandler:^BOOL(GNSSocket *socket) { + _receivedSocket = socket; + return _shouldAcceptSocket; + }]; __block BOOL completionCalled = NO; [peripheralServiceManager addedToPeripheralManager:_peripheralManagerMock bleServiceAddedCompletion:^(NSError *error) { @@ -288,8 +289,7 @@ [self checkOpenSocketWithShouldAccept:shouldAccept central:nil]; } -- (void)checkOpenSocketWithShouldAccept:(BOOL)shouldAccept - central:(CBCentral *)central { +- (void)checkOpenSocketWithShouldAccept:(BOOL)shouldAccept central:(CBCentral *)central { CBMutableCharacteristic *characteristic; NSMutableData *data = [NSMutableData data]; _shouldAcceptSocket = shouldAccept; @@ -307,9 +307,7 @@ XCTAssertNotNil(characteristic); CBATTRequest *request = OCMStrictClassMock([CBATTRequest class]); OCMStub([request value]).andReturn(data); - [self setupRequest:request - withCharacteristic:characteristic - central:central]; + [self setupRequest:request withCharacteristic:characteristic central:central]; __block GNSUpdateValueHandler updateValueHandler = nil; // Nothing is sent when the socket is refused in the Weave protocol. if (!shouldAccept) { @@ -333,8 +331,8 @@ GNSWeaveConnectionConfirmPacket *connectionConfirm = [[GNSWeaveConnectionConfirmPacket alloc] initWithVersion:1 packetSize:_packetSize data:nil]; [expectedData appendData:[connectionConfirm serialize]]; - OCMExpect( - [_peripheralManagerMock updateOutgoingCharacteristic:expectedData onSocket:_receivedSocket]) + OCMExpect([_peripheralManagerMock updateOutgoingCharacteristic:expectedData + onSocket:_receivedSocket]) .andReturn(YES); XCTAssertNotNil(_receivedSocket); XCTAssertFalse(_receivedSocket.isConnected); @@ -363,8 +361,7 @@ error.code == GNSErrorNewInviteToConnectReceived; }]]); OCMExpect([_peripheralManagerMock socketDidDisconnect:firstSocket]); - [self checkOpenSocketWithShouldAccept:YES - central:firstSocket.peerAsCentral]; + [self checkOpenSocketWithShouldAccept:YES central:firstSocket.peerAsCentral]; XCTAssertNotEqual(firstSocket, _receivedSocket); XCTAssertFalse(firstSocket.isConnected); } @@ -521,7 +518,7 @@ XCTAssertNotNil(packet); XCTAssertTrue([packet isKindOfClass:[GNSWeaveDataPacket class]]); GNSWeaveDataPacket *dataPacket = (GNSWeaveDataPacket *)packet; - NSLog(@"packetCounter = %d", dataPacket.packetCounter); + GTMLoggerInfo(@"packetCounter = %d", dataPacket.packetCounter); XCTAssertEqual(dataPacket.packetCounter, sendPacketCounter); sendPacketCounter = (sendPacketCounter + 1) % kGNSMaxPacketCounterValue; if (i == 0) { diff --git a/internal/platform/implementation/apple/Mediums/GNCLeaks.m b/internal/platform/implementation/apple/Mediums/GNCLeaks.m index e06f753b..b7d2a692 100644 --- a/internal/platform/implementation/apple/Mediums/GNCLeaks.m +++ b/internal/platform/implementation/apple/Mediums/GNCLeaks.m @@ -13,12 +13,13 @@ // limitations under the License. #import "internal/platform/implementation/apple/Mediums/GNCLeaks.h" +#import "GoogleToolboxForMac/GTMLogger.h" void GNCVerifyDealloc(id object, NSTimeInterval timeInterval) { #if DEBUG __weak id weakObj = object; NSCAssert(weakObj != nil, @"Pointer to %@ is already nil", weakObj); - NSLog(@"Verifying deallocation of %@", NSStringFromClass([weakObj class])); + GTMLoggerInfo(@"Verifying deallocation of %@", NSStringFromClass([weakObj class])); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeInterval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ NSCAssert(weakObj == nil, @"%@ not deallocated.", weakObj); diff --git a/internal/platform/implementation/apple/ble_socket.mm b/internal/platform/implementation/apple/ble_socket.mm index 88161374..b4d00506 100644 --- a/internal/platform/implementation/apple/ble_socket.mm +++ b/internal/platform/implementation/apple/ble_socket.mm @@ -91,7 +91,6 @@ ExceptionOr BleInputStream::Read(std::int64_t size) { [condition_ unlock]; if (dataToReturn) { - NSLog(@"[NEARBY] Input stream: Received data of size: %lu", (unsigned long)dataToReturn.length); return ExceptionOr(ByteArrayFromNSData(dataToReturn)); } else { return ExceptionOr{Exception::kIo}; @@ -115,8 +114,6 @@ BleOutputStream::~BleOutputStream() { Exception BleOutputStream::Write(const ByteArray &data) { [condition_ lock]; - NSLog(@"[NEARBY] Sending data of size: %lu", NSDataFromByteArray(data).length); - if (!connection_) { [condition_ unlock]; return {Exception::kIo};