| // Copyright 2022 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #ifndef SRC_DEVELOPER_FORENSICS_FEEDBACK_DATA_LOG_SOURCE_H_ | |
| #define SRC_DEVELOPER_FORENSICS_FEEDBACK_DATA_LOG_SOURCE_H_ | |
| #include <fidl/fuchsia.diagnostics/cpp/fidl.h> | |
| #include <fidl/fuchsia.logger/cpp/fidl.h> | |
| #include <lib/async/dispatcher.h> | |
| #include <lib/fpromise/result.h> | |
| #include <lib/sys/cpp/service_directory.h> | |
| #include <memory> | |
| #include <string> | |
| #include "src/lib/diagnostics/fidl_log_message_parser/log_message.h" | |
| namespace forensics::feedback_data { | |
| // Receives logs emitted by the system. | |
| class LogSink { | |
| public: | |
| // A message is a FIDL LogMessage or an error string. | |
| using MessageOr = ::fpromise::result<fuchsia_logger::LogMessage, std::string>; | |
| // Adds |message| to the sink. | |
| // | |
| // Returns false if the write fails though callers are not expected to take action on failure. | |
| virtual bool Add(MessageOr message) = 0; | |
| }; | |
| // Receives log messages from the system's logging service and dispatches them to a sink. | |
| class LogSource : public fidl::AsyncEventHandler<fuchsia_diagnostics::ArchiveAccessor>, | |
| public fidl::AsyncEventHandler<fuchsia_diagnostics::BatchIterator> { | |
| public: | |
| LogSource(async_dispatcher_t* dispatcher, std::shared_ptr<sys::ServiceDirectory> services, | |
| LogSink* sink); | |
| // Starts log collection. | |
| void Start(); | |
| void on_fidl_error(fidl::UnbindInfo error) override; | |
| void handle_unknown_event( | |
| fidl::UnknownEventMetadata<fuchsia_diagnostics::ArchiveAccessor> metadata) override; | |
| void handle_unknown_event( | |
| fidl::UnknownEventMetadata<fuchsia_diagnostics::BatchIterator> metadata) override; | |
| private: | |
| void GetNext(); | |
| async_dispatcher_t* dispatcher_; | |
| std::shared_ptr<sys::ServiceDirectory> services_; | |
| LogSink* sink_; | |
| fidl::Client<fuchsia_diagnostics::ArchiveAccessor> archive_accessor_; | |
| std::unique_ptr<diagnostics::fidl_log_message_parser::LogBatchIterator> batch_iterator_; | |
| }; | |
| } // namespace forensics::feedback_data | |
| #endif // SRC_DEVELOPER_FORENSICS_FEEDBACK_DATA_LOG_SOURCE_H_ |