Add OS Log sink for iOS

PiperOrigin-RevId: 764981349
This commit is contained in:
Guogang Li
2025-05-29 19:29:59 -07:00
committed by Copybara-Service
parent 431b62aa3b
commit 7ac30b7624
6 changed files with 133 additions and 0 deletions
+1
View File
@@ -229,6 +229,7 @@ NC_INSTANCE NcCreateService() {
absl::SetGlobalVLogLevel(1);
#endif // DEBUG
::nearby::apple::EnableNearbyLoggerWriter();
::nearby::apple::EnableOsLog("com.google.nearby.connections");
#endif // TARGET_OS_IOS
#if defined(NC_IOS_SDK)
@@ -164,6 +164,22 @@ objc_library(
],
)
objc_library(
name = "os_log_sink",
srcs = [
"os_log_sink.mm",
],
hdrs = [
"os_log_sink.h",
],
# Prevent Objective-C++ headers from being pulled into swift.
aspect_hints = ["//tools/build_defs/swift:no_module"],
deps = [
"//internal/platform:logging",
"@com_google_absl//absl/strings",
],
)
objc_library(
name = "logger_writer",
srcs = [
@@ -175,6 +191,7 @@ objc_library(
# 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",
],
@@ -15,6 +15,8 @@
#ifndef PLATFORM_IMPL_APPLE_NEARBY_LOGGER_WRITER_H_
#define PLATFORM_IMPL_APPLE_NEARBY_LOGGER_WRITER_H_
#include <string>
namespace nearby {
namespace apple {
@@ -23,6 +25,10 @@ namespace apple {
// 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);
} // namespace apple
} // namespace nearby
@@ -17,6 +17,7 @@
#include <cstdint>
#include <string>
#include "internal/platform/implementation/apple/os_log_sink.h"
#include "internal/platform/logging.h"
#import "GoogleToolboxForMac/GTMLogger.h"
@@ -162,6 +163,14 @@ 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];
@@ -171,5 +180,7 @@ void EnableNearbyLoggerWriter() {
filter:nil];
}
void EnableOsLog(const std::string &subsystem) { absl::AddLogSink(GetOsLogSink(subsystem)); }
} // namespace apple
} // namespace nearby
@@ -0,0 +1,41 @@
// 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_OS_LOG_SINK_H_
#define PLATFORM_IMPL_APPLE_OS_LOG_SINK_H_
#include <os/log.h>
#include "internal/platform/logging.h"
namespace nearby {
namespace apple {
// OsLogSink is a LogSink that sends logs to the OS log.
class OsLogSink : public absl::LogSink {
public:
explicit OsLogSink(const std::string& subsystem);
~OsLogSink() override = default;
// Sends the log entry to the OS log.
void Send(const absl::LogEntry& entry) override;
private:
os_log_t log_;
};
} // namespace apple
} // namespace nearby
#endif // PLATFORM_IMPL_APPLE_OS_LOG_SINK_H_
@@ -0,0 +1,57 @@
// 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.
#include "internal/platform/implementation/apple/os_log_sink.h"
#include <os/log.h>
#include "absl/strings/str_cat.h"
#include "internal/platform/logging.h"
namespace nearby {
namespace apple {
OsLogSink::OsLogSink(const std::string& subsystem)
: log_(os_log_create(subsystem.c_str(), "default")) {}
void OsLogSink::Send(const absl::LogEntry& entry) {
os_log_type_t type = OS_LOG_TYPE_DEFAULT;
switch (entry.log_severity()) {
case absl::LogSeverity::kInfo:
type = OS_LOG_TYPE_INFO;
break;
case absl::LogSeverity::kWarning:
type = OS_LOG_TYPE_DEFAULT; // OSLog doesn't have a distinct 'Warning' level
break;
case absl::LogSeverity::kError:
type = OS_LOG_TYPE_ERROR;
break;
case absl::LogSeverity::kFatal:
type = OS_LOG_TYPE_FAULT;
break;
}
if (entry.verbosity() == 1) {
type = OS_LOG_TYPE_DEBUG;
}
// Format and send the log message to OSLog.
std::string message = absl::StrCat("[", entry.source_basename(), ":", entry.source_line(), "] ",
entry.text_message());
os_log_with_type(log_, type, "%s", message.c_str());
}
} // namespace apple
} // namespace nearby