archivist_lib/logs/servers/
log.rs

1// Copyright 2022 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::logs::error::LogsError;
6use crate::logs::listener::Listener;
7use crate::logs::repository::LogsRepository;
8use fidl::endpoints::DiscoverableProtocolMarker;
9use fidl_fuchsia_diagnostics::StreamMode;
10use futures::StreamExt;
11use log::warn;
12use std::sync::Arc;
13use {fidl_fuchsia_logger as flogger, fuchsia_async as fasync, fuchsia_trace as ftrace};
14
15pub struct LogServer {
16    /// The repository holding the logs.
17    logs_repo: Arc<LogsRepository>,
18
19    /// Scope in which we spawn all of the server tasks.
20    scope: fasync::Scope,
21}
22
23impl LogServer {
24    pub fn new(logs_repo: Arc<LogsRepository>, scope: fasync::Scope) -> Self {
25        Self { logs_repo, scope }
26    }
27
28    /// Spawn a task to handle requests from components reading the shared log.
29    pub fn spawn(&self, stream: flogger::LogRequestStream) {
30        let logs_repo = Arc::clone(&self.logs_repo);
31        let scope = self.scope.to_handle();
32        self.scope.spawn(async move {
33            if let Err(e) = Self::handle_requests(logs_repo, stream, scope).await {
34                warn!("error handling Log requests: {}", e);
35            }
36        });
37    }
38
39    /// Handle requests to `fuchsia.logger.Log`. All request types read the
40    /// whole backlog from memory, `DumpLogs(Safe)` stops listening after that.
41    async fn handle_requests(
42        logs_repo: Arc<LogsRepository>,
43        mut stream: flogger::LogRequestStream,
44        scope: fasync::ScopeHandle,
45    ) -> Result<(), LogsError> {
46        let connection_id = logs_repo.new_interest_connection();
47        while let Some(request) = stream.next().await {
48            let request = request.map_err(|source| LogsError::HandlingRequests {
49                protocol: flogger::LogMarker::PROTOCOL_NAME,
50                source,
51            })?;
52
53            let (listener, options, dump_logs, selectors) = match request {
54                flogger::LogRequest::ListenSafe { log_listener, options, .. } => {
55                    (log_listener, options, false, None)
56                }
57                flogger::LogRequest::DumpLogsSafe { log_listener, options, .. } => {
58                    (log_listener, options, true, None)
59                }
60                flogger::LogRequest::ListenSafeWithSelectors {
61                    log_listener,
62                    options,
63                    selectors,
64                    ..
65                } => (log_listener, options, false, Some(selectors)),
66            };
67
68            let listener = Listener::new(listener, options)?;
69            let mode =
70                if dump_logs { StreamMode::Snapshot } else { StreamMode::SnapshotThenSubscribe };
71            // NOTE: The LogListener code path isn't instrumented for tracing at the moment.
72            let logs = logs_repo.logs_cursor(mode, None, ftrace::Id::random());
73            if let Some(s) = selectors {
74                logs_repo.update_logs_interest(connection_id, s);
75            }
76
77            scope.spawn(listener.run(logs, dump_logs));
78        }
79        logs_repo.finish_interest_connection(connection_id);
80        Ok(())
81    }
82}