fuchsia_fuzzctl_test/diagnostics.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 anyhow::Result;
6use diagnostics_data::{BuilderArgs, LogsDataBuilder, Severity, Timestamp};
7use futures::AsyncWriteExt;
8
9/// Generates a system log entry from the given `msg` and sends it to the given `socket`.
10///
11/// This mimics logs generated using //src/lib/diagnostics/data. These logs include:
12/// * A timestamp in nanoseconds when the log was generated.
13/// * The URL of the component generating the log.
14/// * The component moniker (https://fuchsia.dev/fuchsia-src/reference/components/moniker)
15/// * The log severity, e.g. fatal, error, warning, etc.
16/// * The log message.
17///
18/// See also `writer::Writer::log` which formats these entries for display.
19pub async fn send_log_entry<S: AsRef<str>>(socket: &mut fidl::AsyncSocket, msg: S) -> Result<()> {
20 let builder_args = BuilderArgs {
21 timestamp: Timestamp::from_nanos(0),
22 component_url: Some("".into()),
23 moniker: "moniker".try_into().unwrap(),
24 severity: Severity::Info,
25 };
26 let builder = LogsDataBuilder::new(builder_args);
27 let logs_data = vec![builder.set_message(msg.as_ref()).build()];
28 let bytes = serde_json::to_vec(&logs_data)?;
29 socket.write_all(&bytes).await?;
30 Ok(())
31}