archivist_lib/logs/servers/
log_settings.rs1use crate::logs::error::LogsError;
6use crate::logs::repository::{LogsRepository, STATIC_CONNECTION_ID};
7use fidl::endpoints::DiscoverableProtocolMarker;
8use futures::StreamExt;
9use log::warn;
10use std::sync::Arc;
11use {fidl_fuchsia_diagnostics as fdiagnostics, fuchsia_async as fasync};
12
13pub struct LogSettingsServer {
14 logs_repo: Arc<LogsRepository>,
16
17 scope: fasync::Scope,
19}
20
21impl LogSettingsServer {
22 pub fn new(logs_repo: Arc<LogsRepository>, scope: fasync::Scope) -> Self {
23 Self { logs_repo, scope }
24 }
25
26 pub fn spawn(&self, stream: fdiagnostics::LogSettingsRequestStream) {
28 let logs_repo = Arc::clone(&self.logs_repo);
29 self.scope.spawn(async move {
30 if let Err(e) = Self::handle_requests(logs_repo, stream).await {
31 warn!("error handling Log requests: {}", e);
32 }
33 });
34 }
35
36 pub async fn handle_requests(
37 logs_repo: Arc<LogsRepository>,
38 mut stream: fdiagnostics::LogSettingsRequestStream,
39 ) -> Result<(), LogsError> {
40 let connection_id = logs_repo.new_interest_connection();
41 while let Some(request) = stream.next().await {
42 let request = request.map_err(|source| LogsError::HandlingRequests {
43 protocol: fdiagnostics::LogSettingsMarker::PROTOCOL_NAME,
44 source,
45 })?;
46 match request {
47 fidl_fuchsia_diagnostics::LogSettingsRequest::SetComponentInterest {
48 payload,
49 responder,
50 } => {
51 if let Some(selectors) = payload.selectors {
52 let connection_id = if payload.persist.unwrap_or(false) {
53 STATIC_CONNECTION_ID
54 } else {
55 connection_id
56 };
57 logs_repo.update_logs_interest(connection_id, selectors);
58 }
59 responder.send().ok();
60 }
61 }
62 }
63 logs_repo.finish_interest_connection(connection_id);
64
65 Ok(())
66 }
67}