From 7894c92ab7edded4810665958cc904b4e768e29a Mon Sep 17 00:00:00 2001 From: Camillo Lugaresi Date: Tue, 17 Jan 2023 15:45:15 -0800 Subject: [PATCH] Internal change PiperOrigin-RevId: 502709070 --- mediapipe/util/log_fatal_to_breakpad.cc | 50 +++++++++++++++++++++++++ mediapipe/util/log_fatal_to_breakpad.h | 15 ++++++++ 2 files changed, 65 insertions(+) create mode 100644 mediapipe/util/log_fatal_to_breakpad.cc create mode 100644 mediapipe/util/log_fatal_to_breakpad.h diff --git a/mediapipe/util/log_fatal_to_breakpad.cc b/mediapipe/util/log_fatal_to_breakpad.cc new file mode 100644 index 000000000..45087f2e3 --- /dev/null +++ b/mediapipe/util/log_fatal_to_breakpad.cc @@ -0,0 +1,50 @@ +#include "mediapipe/util/log_fatal_to_breakpad.h" + +#import + +#include "absl/log/log.h" +#include "absl/log/log_sink.h" +#include "absl/log/log_sink_registry.h" +#import "googlemac/iPhone/Shared/GoogleIOSBreakpad/Classes/GoogleBreakpadController.h" + +namespace mediapipe { +namespace { +NSString* MakeNSString(absl::string_view str) { + return [[NSString alloc] initWithBytes:str.data() + length:str.length() + encoding:NSUTF8StringEncoding]; +} +} // namespace + +static NSString* const kFatalLogMessageKey = @"fatal_log_message"; + +class BreakpadFatalLogSink : public absl::LogSink { + public: + BreakpadFatalLogSink() + : breakpad_controller_([GoogleBreakpadController sharedInstance]) {} + void Send(const absl::LogEntry& entry) override { + if (entry.log_severity() != absl::LogSeverity::kFatal) return; + __block NSString* message = MakeNSString(entry.text_message_with_prefix()); + [breakpad_controller_ withBreakpadRef:^(BreakpadRef breakpad) { + // NOTE: This block runs on Breakpad's background queue. + if (!breakpad) return; + BreakpadAddUploadParameter(breakpad, kFatalLogMessageKey, message); + }]; + } + + private: + GoogleBreakpadController* breakpad_controller_; +}; + +absl::LogSink* GetBreakpadFatalLogSink() { + static BreakpadFatalLogSink sink; + return &sink; +} + +// This log sink is automatically enabled when including this library. +static const auto kRegisterLogSink = [] { + absl::AddLogSink(GetBreakpadFatalLogSink()); + return true; +}(); + +} // namespace mediapipe diff --git a/mediapipe/util/log_fatal_to_breakpad.h b/mediapipe/util/log_fatal_to_breakpad.h new file mode 100644 index 000000000..1712a9af8 --- /dev/null +++ b/mediapipe/util/log_fatal_to_breakpad.h @@ -0,0 +1,15 @@ +#ifndef MEDIAPIPE_UTIL_LOG_FATAL_TO_BREAKPAD_H_ +#define MEDIAPIPE_UTIL_LOG_FATAL_TO_BREAKPAD_H_ + +#include "absl/log/log_sink.h" + +namespace mediapipe { + +// Returns a singleton instance of a log sink that sends FATAL log messages to +// Breakpad. This log sink is enabled by default when this library is included +// in your binary. +absl::LogSink* GetBreakpadFatalLogSink(); + +} // namespace mediapipe + +#endif // MEDIAPIPE_UTIL_LOG_FATAL_TO_BREAKPAD_H_