Create GNCLogger for apple platform

PiperOrigin-RevId: 766393388
This commit is contained in:
Guogang Li
2025-06-02 16:45:00 -07:00
committed by Copybara-Service
parent 3084cb79e9
commit d7a688f415
4 changed files with 141 additions and 0 deletions
+1
View File
@@ -424,6 +424,7 @@ let package = Package(
"internal/platform/flags/BUILD",
"internal/platform/implementation/shared/BUILD",
"internal/platform/implementation/apple/Flags/BUILD",
"internal/platform/implementation/apple/Log/BUILD",
"internal/platform/implementation/apple/Mediums/BUILD",
"internal/platform/implementation/apple/Mediums/Ble/Sockets/BUILD",
"internal/platform/implementation/apple/Tests/BUILD",
@@ -0,0 +1,33 @@
# 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.
licenses(["notice"])
package(default_visibility = [
"//connections:__subpackages__",
"//googlemac/iPhone/Nearby:__subpackages__",
"//internal/platform/implementation/apple:__subpackages__",
"//location/nearby:__subpackages__",
"//sharing:__subpackages__",
])
objc_library(
name = "GNCLogger",
srcs = ["GNCLogger.mm"],
hdrs = ["GNCLogger.h"],
deps = [
"//internal/platform:logging",
"//third_party/apple_frameworks:Foundation",
],
)
@@ -0,0 +1,60 @@
// 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.
#ifndef PLATFORM_IMPL_APPLE_LOG_GNC_LOGGER_H_
#define PLATFORM_IMPL_APPLE_LOG_GNC_LOGGER_H_
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
#ifdef __cplusplus
extern "C" {
#endif
// Definition of the LogSeverity enum.
typedef NS_ENUM(NSInteger, GNCLogSeverity) {
GNCLogSeverityDebug = 0, // Debug logs are mapping to VLOG(1) in ABSL logging.
GNCLogSeverityInfo = 1,
GNCLogSeverityWarning = 2,
GNCLogSeverityError = 3,
GNCLogSeverityFatal = 4,
};
/**
* Logs a message to Nearby Connections.
* @param fileName The name of the file where the log message is generated.
* @param lineNumber The line number of the log message.
* @param severity The severity of the log message.
* @param format The format string for the log message.
* @param ... The arguments to be passed to the format string.
* @note This function doesn't support private and public formatting in format string at the moment.
*/
void GNCLog(const char *fileName, int lineNumber, GNCLogSeverity severity, NSString *format,
...);
/// Macros to log a message to Nearby Connections.
#define GNCLoggerDebug(...) GNCLog(__FILE__, __LINE__, GNCLogSeverityDebug, ##__VA_ARGS__)
#define GNCLoggerInfo(...) GNCLog(__FILE__, __LINE__, GNCLogSeverityInfo, ##__VA_ARGS__)
#define GNCLoggerWarning(...) GNCLog(__FILE__, __LINE__, GNCLogSeverityWarning, ##__VA_ARGS__)
#define GNCLoggerError(...) GNCLog(__FILE__, __LINE__, GNCLogSeverityError, ##__VA_ARGS__)
#define GNCLoggerFatal(...) GNCLog(__FILE__, __LINE__, GNCLogSeverityFatal, ##__VA_ARGS__)
#ifdef __cplusplus
} // extern "C"
#endif
NS_ASSUME_NONNULL_END
#endif // PLATFORM_IMPL_APPLE_LOG_GNC_LOGGER_H_
@@ -0,0 +1,47 @@
// 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/Log/GNCLogger.h"
#import <Foundation/Foundation.h>
#include "internal/platform/logging.h"
NS_ASSUME_NONNULL_BEGIN
void GNCLog(const char *fileName, int lineNumber, GNCLogSeverity severity, NSString *format, ...) {
va_list arguments;
va_start(arguments, format);
NSString *messageString = [[NSString alloc] initWithFormat:format arguments:arguments];
switch (severity) {
case GNCLogSeverityDebug:
VLOG(1).AtLocation(fileName, lineNumber).WithVerbosity(1)
<< std::string([messageString UTF8String]);
break;
case GNCLogSeverityInfo:
LOG(INFO).AtLocation(fileName, lineNumber) << std::string([messageString UTF8String]);
break;
case GNCLogSeverityWarning:
LOG(WARNING).AtLocation(fileName, lineNumber) << std::string([messageString UTF8String]);
break;
case GNCLogSeverityError:
LOG(ERROR).AtLocation(fileName, lineNumber) << std::string([messageString UTF8String]);
break;
case GNCLogSeverityFatal:
LOG(FATAL).AtLocation(fileName, lineNumber) << std::string([messageString UTF8String]);
break;
}
}
NS_ASSUME_NONNULL_END