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 fdiagnostics::LogSettingsRequest::SetInterest { selectors, responder } => {
48 logs_repo.update_logs_interest(connection_id, selectors);
49 responder.send().ok();
50 }
51 fidl_fuchsia_diagnostics::LogSettingsRequest::SetComponentInterest {
52 payload,
53 responder,
54 } => {
55 if let Some(selectors) = payload.selectors {
56 let connection_id = if payload.persist.unwrap_or(false) {
57 STATIC_CONNECTION_ID
58 } else {
59 connection_id
60 };
61 logs_repo.update_logs_interest(connection_id, selectors);
62 }
63 responder.send().ok();
64 }
65 }
66 }
67 logs_repo.finish_interest_connection(connection_id);
68
69 Ok(())
70 }
71}