archivist_lib/logs/servers/
log_settings.rs1use crate::logs::error::LogsError;
6use crate::logs::repository::{LogsRepository, STATIC_CONNECTION_ID};
7use fidl::endpoints::{ControlHandle, 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::RegisterInterest { control_handle, .. } => {
48 warn!("fuchsia.diagnostics/LogSettings.RegisterInterest is not supported; closing the channel");
49 control_handle.shutdown();
50 }
51 fdiagnostics::LogSettingsRequest::SetInterest { selectors, responder } => {
52 logs_repo.update_logs_interest(connection_id, selectors);
53 responder.send().ok();
54 }
55 fidl_fuchsia_diagnostics::LogSettingsRequest::SetComponentInterest {
56 payload,
57 responder,
58 } => {
59 if let Some(selectors) = payload.selectors {
60 let connection_id = if payload.persist.unwrap_or(false) {
61 STATIC_CONNECTION_ID
62 } else {
63 connection_id
64 };
65 logs_repo.update_logs_interest(connection_id, selectors);
66 }
67 responder.send().ok();
68 }
69 }
70 }
71 logs_repo.finish_interest_connection(connection_id);
72
73 Ok(())
74 }
75}