Clean up logging implementations

PiperOrigin-RevId: 670714192
This commit is contained in:
Guogang Li
2024-09-03 14:48:52 -07:00
committed by Copybara-Service
parent 3ee286092a
commit f8669cf16d
15 changed files with 60 additions and 455 deletions
+3 -2
View File
@@ -25,7 +25,6 @@ objc_library(
srcs = [
"crypto.mm",
"device_info.mm",
"log_message.mm",
"multi_thread_executor.mm",
"platform.mm",
"preferences_manager.mm",
@@ -35,7 +34,6 @@ objc_library(
],
hdrs = [
"device_info.h",
"log_message.h",
"multi_thread_executor.h",
"preferences_manager.h",
"scheduled_executor.h",
@@ -150,8 +148,10 @@ cc_library(
"mutex.h",
],
deps = [
"//internal/platform:base",
"//internal/platform/implementation:platform",
"//internal/platform/implementation:types",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/time",
@@ -170,6 +170,7 @@ cc_test(
shard_count = 16,
deps = [
":Platform_cc",
"//internal/platform/implementation/g3:crypto",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/time",
@@ -13,6 +13,9 @@
// limitations under the License.
#include "internal/platform/implementation/apple/count_down_latch.h"
#include "absl/synchronization/mutex.h"
#include "absl/time/time.h"
#include "internal/platform/exception.h"
namespace nearby {
namespace apple {
@@ -15,7 +15,10 @@
#ifndef PLATFORM_IMPL_APPLE_COUNT_DOWN_LATCH_H_
#define PLATFORM_IMPL_APPLE_COUNT_DOWN_LATCH_H_
#include "absl/base/thread_annotations.h"
#include "absl/synchronization/mutex.h"
#include "absl/time/time.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/count_down_latch.h"
namespace nearby {
@@ -14,7 +14,10 @@
#include "internal/platform/implementation/apple/count_down_latch.h"
#include <atomic>
#include "gtest/gtest.h"
#include "absl/time/time.h"
#include "thread/fiber/fiber.h"
namespace nearby {
@@ -56,11 +59,11 @@ TEST(CountDownLatchTest, LatchAwaitWithTimeoutCanExpire) {
auto response = latch.Await(absl::Milliseconds(100));
EXPECT_TRUE(response.ok());
EXPECT_FALSE(response.ok());
EXPECT_FALSE(response.result());
}
TEST(CountDownLatchTest, InitialCountZero_AwaitDoesNotBlock) {
TEST(CountDownLatchTest, InitialCountZeroAwaitDoesNotBlock) {
CountDownLatch latch(0);
auto response = latch.Await();
@@ -68,7 +71,7 @@ TEST(CountDownLatchTest, InitialCountZero_AwaitDoesNotBlock) {
EXPECT_TRUE(response.Ok());
}
TEST(CountDownLatchTest, InitialCountNegative_AwaitDoesNotBlock) {
TEST(CountDownLatchTest, InitialCountNegativeAwaitDoesNotBlock) {
CountDownLatch latch(-1);
auto response = latch.Await();
@@ -1,65 +0,0 @@
// Copyright 2020 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_MESSAGE_H_
#define PLATFORM_IMPL_APPLE_LOG_MESSAGE_H_
#include <ostream>
#include <sstream>
#include <string>
#include "absl/strings/string_view.h"
#include "internal/platform/implementation/log_message.h"
#include "GoogleToolboxForMac/GTMLogger.h"
namespace nearby {
namespace apple {
class LogStreamer final {
public:
explicit LogStreamer(GTMLoggerLevel severity, absl::string_view func);
~LogStreamer();
std::ostream& stream() { return stream_; }
private:
GTMLoggerLevel severity_;
std::string func_;
std::ostringstream stream_;
};
// Concrete LogMessage implementation
class LogMessage : public api::LogMessage {
public:
LogMessage(const char* file, int line, Severity severity);
~LogMessage() override = default;
LogMessage(const LogMessage&) = delete;
LogMessage& operator=(const LogMessage&) = delete;
void Print(const char* format, ...) override;
std::ostream& Stream() override;
private:
LogStreamer log_streamer_;
GTMLoggerLevel severity_;
std::string func_;
};
} // namespace apple
} // namespace nearby
#endif // PLATFORM_IMPL_APPLE_LOG_MESSAGE_H_
@@ -1,117 +0,0 @@
// Copyright 2020 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/log_message.h"
#include <ostream>
#include <string>
#include "internal/platform/implementation/log_message.h"
namespace nearby {
namespace apple {
api::LogMessage::Severity gMinLogSeverity = api::LogMessage::Severity::kInfo;
GTMLoggerLevel ConvertSeverity(api::LogMessage::Severity severity) {
switch (severity) {
case api::LogMessage::Severity::kVerbose:
return kGTMLoggerLevelDebug;
case api::LogMessage::Severity::kInfo:
return kGTMLoggerLevelInfo;
case api::LogMessage::Severity::kWarning:
return kGTMLoggerLevelInfo;
case api::LogMessage::Severity::kError:
return kGTMLoggerLevelError;
case api::LogMessage::Severity::kFatal:
return kGTMLoggerLevelAssert;
}
}
// GTMLogger expects a function name, but we only have file and line. So format the info as
// {basename(file)}:{line} and use that as the function name.
std::string ConvertFileAndLine(absl::string_view filepath, int line) {
size_t path = filepath.find_last_of('/');
if (path != filepath.npos) filepath.remove_prefix(path + 1);
return std::string(filepath) + ":" + std::to_string(line);
}
LogStreamer::LogStreamer(GTMLoggerLevel severity, absl::string_view func)
: severity_(severity), func_(func) {}
LogStreamer::~LogStreamer() {
switch (severity_) {
case kGTMLoggerLevelDebug:
[[GTMLogger sharedLogger] logFuncDebug:func_.c_str() msg:@"%@", @(stream_.str().c_str())];
break;
case kGTMLoggerLevelInfo:
[[GTMLogger sharedLogger] logFuncInfo:func_.c_str() msg:@"%@", @(stream_.str().c_str())];
break;
case kGTMLoggerLevelError:
[[GTMLogger sharedLogger] logFuncError:func_.c_str() msg:@"%@", @(stream_.str().c_str())];
break;
case kGTMLoggerLevelAssert:
[[GTMLogger sharedLogger] logFuncAssert:func_.c_str() msg:@"%@", @(stream_.str().c_str())];
break;
case kGTMLoggerLevelUnknown:
// no-op
break;
}
}
LogMessage::LogMessage(const char* file, int line, Severity severity)
: log_streamer_(ConvertSeverity(severity), ConvertFileAndLine(file, line)),
severity_(ConvertSeverity(severity)),
func_(ConvertFileAndLine(file, line)) {}
void LogMessage::Print(const char* format, ...) {
va_list ap;
va_start(ap, format);
NSString *msg = [[NSString alloc] initWithFormat:@(format) arguments:ap];
switch (severity_) {
case kGTMLoggerLevelDebug:
[[GTMLogger sharedLogger] logFuncDebug:func_.c_str() msg:@"%@", msg];
break;
case kGTMLoggerLevelInfo:
[[GTMLogger sharedLogger] logFuncInfo:func_.c_str() msg:@"%@", msg];
break;
case kGTMLoggerLevelError:
[[GTMLogger sharedLogger] logFuncError:func_.c_str() msg:@"%@", msg];
break;
case kGTMLoggerLevelAssert:
[[GTMLogger sharedLogger] logFuncAssert:func_.c_str() msg:@"%@", msg];
break;
case kGTMLoggerLevelUnknown:
// no-op
break;
}
va_end(ap);
}
std::ostream& LogMessage::Stream() { return log_streamer_.stream(); }
} // namespace apple
namespace api {
// static
void LogMessage::SetMinLogSeverity(Severity severity) { apple::gMinLogSeverity = severity; }
// static
bool LogMessage::ShouldCreateLogMessage(Severity severity) {
return severity >= apple::gMinLogSeverity;
}
} // namespace api
} // namespace nearby
@@ -26,7 +26,6 @@
#include "internal/platform/implementation/apple/condition_variable.h"
#include "internal/platform/implementation/apple/count_down_latch.h"
#include "internal/platform/implementation/apple/device_info.h"
#import "internal/platform/implementation/apple/log_message.h"
#import "internal/platform/implementation/apple/multi_thread_executor.h"
#include "internal/platform/implementation/apple/mutex.h"
#include "internal/platform/implementation/apple/preferences_manager.h"
@@ -135,11 +134,6 @@ std::unique_ptr<OutputFile> ImplementationPlatform::CreateOutputFile(const std::
return shared::IOFile::CreateOutputFile(file_path);
}
std::unique_ptr<LogMessage> ImplementationPlatform::CreateLogMessage(
const char* file, int line, LogMessage::Severity severity) {
return std::make_unique<apple::LogMessage>(file, line, severity);
}
// Java-like Executors
std::unique_ptr<SubmittableExecutor> ImplementationPlatform::CreateSingleThreadExecutor() {
return std::make_unique<apple::SingleThreadExecutor>();