diagnostics_data/
lib.rs

1// Copyright 2020 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
5//! # Diagnostics data
6//!
7//! This library contains the Diagnostics data schema used for inspect and logs . This is
8//! the data that the Archive returns on `fuchsia.diagnostics.ArchiveAccessor` reads.
9
10use chrono::{Local, TimeZone, Utc};
11use diagnostics_hierarchy::HierarchyMatcher;
12use fidl_fuchsia_diagnostics::{DataType, Selector};
13use fidl_fuchsia_inspect as finspect;
14use flyweights::FlyStr;
15use itertools::Itertools;
16use moniker::EXTENDED_MONIKER_COMPONENT_MANAGER_STR;
17use selectors::SelectorExt;
18use serde::de::{DeserializeOwned, Deserializer};
19use serde::{Deserialize, Serialize, Serializer};
20use std::borrow::{Borrow, Cow};
21use std::cmp::Ordering;
22use std::fmt;
23use std::hash::Hash;
24use std::ops::Deref;
25use std::str::FromStr;
26use std::sync::LazyLock;
27use std::time::Duration;
28use termion::{color, style};
29use thiserror::Error;
30
31pub use diagnostics_hierarchy::{DiagnosticsHierarchy, Property, hierarchy};
32pub use diagnostics_log_types_serde::Severity;
33pub use moniker::ExtendedMoniker;
34
35#[cfg(target_os = "fuchsia")]
36#[doc(hidden)]
37pub mod logs_legacy;
38
39#[cfg(feature = "json_schema")]
40use schemars::JsonSchema;
41
42const SCHEMA_VERSION: u64 = 1;
43const MICROS_IN_SEC: u128 = 1000000;
44const ROOT_MONIKER_REPR: &str = "<root>";
45
46static DEFAULT_TREE_NAME: LazyLock<FlyStr> =
47    LazyLock::new(|| FlyStr::new(finspect::DEFAULT_TREE_NAME));
48
49/// The possible name for a handle to inspect data. It could be a filename (being deprecated) or a
50/// name published using `fuchsia.inspect.InspectSink`.
51#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Hash, Eq)]
52#[serde(rename_all = "lowercase")]
53pub enum InspectHandleName {
54    /// The name of an `InspectHandle`. This comes from the `name` argument
55    /// in `InspectSink`.
56    Name(FlyStr),
57
58    /// The name of the file source when reading a file source of Inspect
59    /// (eg an inspect VMO file or fuchsia.inspect.Tree in out/diagnostics)
60    Filename(FlyStr),
61}
62
63impl std::fmt::Display for InspectHandleName {
64    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65        write!(f, "{}", self.as_ref())
66    }
67}
68
69impl InspectHandleName {
70    /// Construct an InspectHandleName::Name
71    pub fn name(n: impl Into<FlyStr>) -> Self {
72        Self::Name(n.into())
73    }
74
75    /// Construct an InspectHandleName::Filename
76    pub fn filename(n: impl Into<FlyStr>) -> Self {
77        Self::Filename(n.into())
78    }
79
80    /// If variant is Name, get the underlying value.
81    pub fn as_name(&self) -> Option<&str> {
82        if let Self::Name(n) = self { Some(n.as_str()) } else { None }
83    }
84
85    /// If variant is Filename, get the underlying value
86    pub fn as_filename(&self) -> Option<&str> {
87        if let Self::Filename(f) = self { Some(f.as_str()) } else { None }
88    }
89}
90
91impl AsRef<str> for InspectHandleName {
92    fn as_ref(&self) -> &str {
93        match self {
94            Self::Filename(f) => f.as_str(),
95            Self::Name(n) => n.as_str(),
96        }
97    }
98}
99
100/// The source of diagnostics data
101#[cfg_attr(feature = "json_schema", derive(JsonSchema))]
102#[derive(Default, Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
103pub enum DataSource {
104    #[default]
105    Unknown,
106    Inspect,
107    Logs,
108}
109
110pub trait MetadataError {
111    fn dropped_payload() -> Self;
112    fn message(&self) -> Option<&str>;
113}
114
115pub trait Metadata: DeserializeOwned + Serialize + Clone + Send {
116    /// The type of error returned in this metadata.
117    type Error: Clone + MetadataError;
118
119    /// Returns the timestamp at which this value was recorded.
120    fn timestamp(&self) -> Timestamp;
121
122    /// Overrides the timestamp at which this value was recorded.
123    fn set_timestamp(&mut self, timestamp: Timestamp);
124
125    /// Returns the errors recorded with this value, if any.
126    fn errors(&self) -> Option<&[Self::Error]>;
127
128    /// Overrides the errors associated with this value.
129    fn set_errors(&mut self, errors: Vec<Self::Error>);
130
131    /// Returns whether any errors are recorded on this value.
132    fn has_errors(&self) -> bool {
133        self.errors().map(|e| !e.is_empty()).unwrap_or_default()
134    }
135
136    /// Merge with another Metadata, taking latest timestamps and combining
137    /// errors.
138    fn merge(&mut self, other: Self) {
139        if self.timestamp() < other.timestamp() {
140            self.set_timestamp(other.timestamp());
141        }
142
143        if let Some(more) = other.errors() {
144            let mut errs = Vec::from(self.errors().unwrap_or_default());
145            errs.extend_from_slice(more);
146            self.set_errors(errs);
147        }
148    }
149}
150
151/// A trait implemented by marker types which denote "kinds" of diagnostics data.
152pub trait DiagnosticsData {
153    /// The type of metadata included in results of this type.
154    type Metadata: Metadata;
155
156    /// The type of key used for indexing node hierarchies in the payload.
157    type Key: AsRef<str> + Clone + DeserializeOwned + Eq + FromStr + Hash + Send + 'static;
158
159    /// Used to query for this kind of metadata in the ArchiveAccessor.
160    const DATA_TYPE: DataType;
161}
162
163/// Inspect carries snapshots of data trees hosted by components.
164#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
165pub struct Inspect;
166
167impl DiagnosticsData for Inspect {
168    type Metadata = InspectMetadata;
169    type Key = String;
170    const DATA_TYPE: DataType = DataType::Inspect;
171}
172
173impl Metadata for InspectMetadata {
174    type Error = InspectError;
175
176    fn timestamp(&self) -> Timestamp {
177        self.timestamp
178    }
179
180    fn set_timestamp(&mut self, timestamp: Timestamp) {
181        self.timestamp = timestamp;
182    }
183
184    fn errors(&self) -> Option<&[Self::Error]> {
185        self.errors.as_deref()
186    }
187
188    fn set_errors(&mut self, errors: Vec<Self::Error>) {
189        self.errors = Some(errors);
190    }
191}
192
193/// Logs carry streams of structured events from components.
194#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
195pub struct Logs;
196
197impl DiagnosticsData for Logs {
198    type Metadata = LogsMetadata;
199    type Key = LogsField;
200    const DATA_TYPE: DataType = DataType::Logs;
201}
202
203impl Metadata for LogsMetadata {
204    type Error = LogError;
205
206    fn timestamp(&self) -> Timestamp {
207        self.timestamp
208    }
209
210    fn set_timestamp(&mut self, timestamp: Timestamp) {
211        self.timestamp = timestamp;
212    }
213
214    fn errors(&self) -> Option<&[Self::Error]> {
215        self.errors.as_deref()
216    }
217
218    fn set_errors(&mut self, errors: Vec<Self::Error>) {
219        self.errors = Some(errors);
220    }
221}
222
223pub fn serialize_timestamp<S>(timestamp: &Timestamp, serializer: S) -> Result<S::Ok, S::Error>
224where
225    S: Serializer,
226{
227    serializer.serialize_i64(timestamp.into_nanos())
228}
229
230pub fn deserialize_timestamp<'de, D>(deserializer: D) -> Result<Timestamp, D::Error>
231where
232    D: Deserializer<'de>,
233{
234    let nanos = i64::deserialize(deserializer)?;
235    Ok(Timestamp::from_nanos(nanos))
236}
237
238#[cfg(target_os = "fuchsia")]
239mod zircon {
240    pub type Timestamp = zx::BootInstant;
241
242    /// De-applies the mono-to-boot offset on this timestamp.
243    ///
244    /// This works only if called soon after `self` is produced, otherwise
245    /// the timestamp will be placed further back in time.
246    pub fn unapply_mono_to_boot_offset(timestamp: Timestamp) -> zx::MonotonicInstant {
247        let mono_now = zx::MonotonicInstant::get();
248        let boot_now = zx::BootInstant::get();
249
250        let mono_to_boot_offset_nanos = boot_now.into_nanos() - mono_now.into_nanos();
251        zx::MonotonicInstant::from_nanos(timestamp.into_nanos() - mono_to_boot_offset_nanos)
252    }
253}
254
255#[cfg(target_os = "fuchsia")]
256pub use zircon::Timestamp;
257#[cfg(target_os = "fuchsia")]
258pub use zircon::unapply_mono_to_boot_offset;
259
260#[cfg(not(target_os = "fuchsia"))]
261mod host {
262    use serde::{Deserialize, Serialize};
263    use std::fmt;
264    use std::ops::Add;
265    use std::time::Duration;
266
267    #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
268    pub struct Timestamp(i64);
269
270    impl Timestamp {
271        /// Returns the number of nanoseconds associated with this timestamp.
272        pub fn into_nanos(self) -> i64 {
273            self.0
274        }
275
276        /// Constructs a timestamp from the given nanoseconds.
277        pub fn from_nanos(nanos: i64) -> Self {
278            Self(nanos)
279        }
280    }
281
282    impl Add<Duration> for Timestamp {
283        type Output = Timestamp;
284        fn add(self, rhs: Duration) -> Self::Output {
285            Timestamp(self.0 + rhs.as_nanos() as i64)
286        }
287    }
288
289    impl fmt::Display for Timestamp {
290        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
291            write!(f, "{}", self.0)
292        }
293    }
294}
295
296#[cfg(not(target_os = "fuchsia"))]
297pub use host::Timestamp;
298
299#[cfg(feature = "json_schema")]
300impl JsonSchema for Timestamp {
301    fn schema_name() -> String {
302        "integer".to_owned()
303    }
304
305    fn json_schema(generator: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema {
306        i64::json_schema(generator)
307    }
308}
309
310/// The metadata contained in a `DiagnosticsData` object where the data source is
311/// `DataSource::Inspect`.
312#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
313pub struct InspectMetadata {
314    /// Optional vector of errors encountered by platform.
315    #[serde(skip_serializing_if = "Option::is_none")]
316    pub errors: Option<Vec<InspectError>>,
317
318    /// Name of diagnostics source producing data.
319    #[serde(flatten)]
320    pub name: InspectHandleName,
321
322    /// The url with which the component was launched.
323    pub component_url: FlyStr,
324
325    /// Boot time in nanos.
326    #[serde(serialize_with = "serialize_timestamp", deserialize_with = "deserialize_timestamp")]
327    pub timestamp: Timestamp,
328
329    /// When set to true, the data was escrowed. Otherwise, the data was fetched live from the
330    /// source component at runtime. When absent, it means the value is false.
331    #[serde(skip_serializing_if = "std::ops::Not::not")]
332    #[serde(default)]
333    pub escrowed: bool,
334}
335
336impl InspectMetadata {
337    /// Returns the component URL with which the component that emitted the associated Inspect data
338    /// was launched.
339    pub fn component_url(&self) -> &str {
340        self.component_url.as_str()
341    }
342}
343
344/// The metadata contained in a `DiagnosticsData` object where the data source is
345/// `DataSource::Logs`.
346#[cfg_attr(feature = "json_schema", derive(JsonSchema))]
347#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
348pub struct LogsMetadata {
349    // TODO(https://fxbug.dev/42136318) figure out exact spelling of pid/tid context and severity
350    /// Optional vector of errors encountered by platform.
351    #[serde(skip_serializing_if = "Option::is_none")]
352    pub errors: Option<Vec<LogError>>,
353
354    /// The url with which the component was launched.
355    #[serde(skip_serializing_if = "Option::is_none")]
356    pub component_url: Option<FlyStr>,
357
358    /// Boot time in nanos.
359    #[serde(serialize_with = "serialize_timestamp", deserialize_with = "deserialize_timestamp")]
360    pub timestamp: Timestamp,
361
362    /// Severity of the message.
363    // For some reason using the `with` field was causing clippy errors, so this manually uses
364    // `serialize_with` and `deserialize_with`
365    #[serde(
366        serialize_with = "diagnostics_log_types_serde::severity::serialize",
367        deserialize_with = "diagnostics_log_types_serde::severity::deserialize"
368    )]
369    pub severity: Severity,
370
371    /// Raw severity if any. This will typically be unset unless the log message carries a severity
372    /// that differs from the standard values of each severity.
373    #[serde(skip_serializing_if = "Option::is_none")]
374    raw_severity: Option<u8>,
375
376    /// Tags to add at the beginning of the message
377    #[serde(skip_serializing_if = "Option::is_none")]
378    pub tags: Option<Vec<String>>,
379
380    /// The process ID
381    #[serde(skip_serializing_if = "Option::is_none")]
382    pub pid: Option<u64>,
383
384    /// The thread ID
385    #[serde(skip_serializing_if = "Option::is_none")]
386    pub tid: Option<u64>,
387
388    /// The file name
389    #[serde(skip_serializing_if = "Option::is_none")]
390    pub file: Option<String>,
391
392    /// The line number
393    #[serde(skip_serializing_if = "Option::is_none")]
394    pub line: Option<u64>,
395
396    /// Number of dropped messages
397    /// DEPRECATED: do not set. Left for backwards compatibility with older serialized metadatas
398    /// that contain this field.
399    #[serde(skip)]
400    dropped: Option<u64>,
401
402    /// Size of the original message on the wire, in bytes.
403    /// DEPRECATED: do not set. Left for backwards compatibility with older serialized metadatas
404    /// that contain this field.
405    #[serde(skip)]
406    size_bytes: Option<usize>,
407}
408
409impl LogsMetadata {
410    /// Returns the component URL which generated this value.
411    pub fn component_url(&self) -> Option<&str> {
412        self.component_url.as_ref().map(|s| s.as_str())
413    }
414
415    /// Returns the raw severity of this log.
416    pub fn raw_severity(&self) -> u8 {
417        match self.raw_severity {
418            Some(s) => s,
419            None => self.severity as u8,
420        }
421    }
422}
423
424/// An instance of diagnostics data with typed metadata and an optional nested payload.
425#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
426pub struct Data<D: DiagnosticsData> {
427    /// The source of the data.
428    #[serde(default)]
429    // TODO(https://fxbug.dev/42135946) remove this once the Metadata enum is gone everywhere
430    pub data_source: DataSource,
431
432    /// The metadata for the diagnostics payload.
433    #[serde(bound(
434        deserialize = "D::Metadata: DeserializeOwned",
435        serialize = "D::Metadata: Serialize"
436    ))]
437    pub metadata: D::Metadata,
438
439    /// Moniker of the component that generated the payload.
440    #[serde(deserialize_with = "moniker_deserialize", serialize_with = "moniker_serialize")]
441    pub moniker: ExtendedMoniker,
442
443    /// Payload containing diagnostics data, if the payload exists, else None.
444    pub payload: Option<DiagnosticsHierarchy<D::Key>>,
445
446    /// Schema version.
447    #[serde(default)]
448    pub version: u64,
449}
450
451fn moniker_deserialize<'de, D>(deserializer: D) -> Result<ExtendedMoniker, D::Error>
452where
453    D: serde::Deserializer<'de>,
454{
455    let moniker_str = String::deserialize(deserializer)?;
456    ExtendedMoniker::parse_str(&moniker_str).map_err(serde::de::Error::custom)
457}
458
459fn moniker_serialize<S>(moniker: &ExtendedMoniker, s: S) -> Result<S::Ok, S::Error>
460where
461    S: Serializer,
462{
463    s.collect_str(moniker)
464}
465
466impl<D> Data<D>
467where
468    D: DiagnosticsData,
469{
470    /// Returns a [`Data`] with an error indicating that the payload was dropped.
471    pub fn drop_payload(&mut self) {
472        self.metadata.set_errors(vec![
473            <<D as DiagnosticsData>::Metadata as Metadata>::Error::dropped_payload(),
474        ]);
475        self.payload = None;
476    }
477
478    /// Sorts this [`Data`]'s payload if one is present.
479    pub fn sort_payload(&mut self) {
480        if let Some(payload) = &mut self.payload {
481            payload.sort();
482        }
483    }
484
485    /// Merge from another Data, combining data.
486    pub fn merge(&mut self, other: Self) {
487        let Data { data_source, metadata, moniker, payload, version } = other;
488
489        if self.data_source != data_source || self.moniker != moniker || self.version != version {
490            // other does not represent the same data.
491            return;
492        }
493
494        self.metadata.merge(metadata);
495
496        match (&mut self.payload, payload) {
497            (Some(existing), Some(more)) => {
498                existing.merge(more);
499            }
500            (None, Some(payload)) => {
501                self.payload = Some(payload);
502            }
503            _ => {}
504        }
505    }
506
507    /// Uses a set of Selectors to filter self's payload and returns the resulting
508    /// Data. If the resulting payload is empty, it returns Ok(None).
509    pub fn filter(mut self, selectors: &[Selector]) -> Result<Option<Self>, Error> {
510        let Some(hierarchy) = self.payload else {
511            return Ok(None);
512        };
513        let matching_selectors =
514            match self.moniker.match_against_selectors(selectors).collect::<Result<Vec<_>, _>>() {
515                Ok(selectors) if selectors.is_empty() => return Ok(None),
516                Ok(selectors) => selectors,
517                Err(e) => {
518                    return Err(Error::Internal(e));
519                }
520            };
521
522        // TODO(https://fxbug.dev/300319116): Cache the `HierarchyMatcher`s
523        let matcher: HierarchyMatcher = match matching_selectors.try_into() {
524            Ok(hierarchy_matcher) => hierarchy_matcher,
525            Err(e) => {
526                return Err(Error::Internal(e.into()));
527            }
528        };
529
530        self.payload = match diagnostics_hierarchy::filter_hierarchy(hierarchy, &matcher) {
531            Some(hierarchy) => Some(hierarchy),
532            None => return Ok(None),
533        };
534        Ok(Some(self))
535    }
536}
537
538/// Errors that can happen in this library.
539#[derive(Debug, Error)]
540pub enum Error {
541    #[error(transparent)]
542    Internal(#[from] anyhow::Error),
543}
544
545/// A diagnostics data object containing inspect data.
546pub type InspectData = Data<Inspect>;
547
548/// A diagnostics data object containing logs data.
549pub type LogsData = Data<Logs>;
550
551/// A diagnostics data payload containing logs data.
552pub type LogsHierarchy = DiagnosticsHierarchy<LogsField>;
553
554/// A diagnostics hierarchy property keyed by `LogsField`.
555pub type LogsProperty = Property<LogsField>;
556
557impl Data<Inspect> {
558    /// Access the name or filename within `self.metadata`.
559    pub fn name(&self) -> &str {
560        self.metadata.name.as_ref()
561    }
562}
563
564pub struct InspectDataBuilder {
565    data: Data<Inspect>,
566}
567
568impl InspectDataBuilder {
569    pub fn new(
570        moniker: ExtendedMoniker,
571        component_url: impl Into<FlyStr>,
572        timestamp: impl Into<Timestamp>,
573    ) -> Self {
574        Self {
575            data: Data {
576                data_source: DataSource::Inspect,
577                moniker,
578                payload: None,
579                version: 1,
580                metadata: InspectMetadata {
581                    errors: None,
582                    name: InspectHandleName::name(DEFAULT_TREE_NAME.clone()),
583                    component_url: component_url.into(),
584                    timestamp: timestamp.into(),
585                    escrowed: false,
586                },
587            },
588        }
589    }
590
591    pub fn escrowed(mut self, escrowed: bool) -> Self {
592        self.data.metadata.escrowed = escrowed;
593        self
594    }
595
596    pub fn with_hierarchy(
597        mut self,
598        hierarchy: DiagnosticsHierarchy<<Inspect as DiagnosticsData>::Key>,
599    ) -> Self {
600        self.data.payload = Some(hierarchy);
601        self
602    }
603
604    pub fn with_errors(mut self, errors: Vec<InspectError>) -> Self {
605        self.data.metadata.errors = Some(errors);
606        self
607    }
608
609    pub fn with_name(mut self, name: InspectHandleName) -> Self {
610        self.data.metadata.name = name;
611        self
612    }
613
614    pub fn build(self) -> Data<Inspect> {
615        self.data
616    }
617}
618
619/// Internal state of the LogsDataBuilder impl
620/// External customers should not directly access these fields.
621pub struct LogsDataBuilder {
622    /// List of errors
623    errors: Vec<LogError>,
624    /// Message in log
625    msg: Option<String>,
626    /// List of tags
627    tags: Vec<String>,
628    /// Process ID
629    pid: Option<u64>,
630    /// Thread ID
631    tid: Option<u64>,
632    /// File name
633    file: Option<String>,
634    /// Line number
635    line: Option<u64>,
636    /// BuilderArgs that was passed in at construction time
637    args: BuilderArgs,
638    /// List of KVPs from the user
639    keys: Vec<Property<LogsField>>,
640    /// Raw severity.
641    raw_severity: Option<u8>,
642}
643
644/// Arguments used to create a new [`LogsDataBuilder`].
645pub struct BuilderArgs {
646    /// The moniker for the component
647    pub moniker: ExtendedMoniker,
648    /// The timestamp of the message in nanoseconds
649    pub timestamp: Timestamp,
650    /// The component URL
651    pub component_url: Option<FlyStr>,
652    /// The message severity
653    pub severity: Severity,
654}
655
656impl LogsDataBuilder {
657    /// Constructs a new LogsDataBuilder
658    pub fn new(args: BuilderArgs) -> Self {
659        LogsDataBuilder {
660            args,
661            errors: vec![],
662            msg: None,
663            file: None,
664            line: None,
665            pid: None,
666            tags: vec![],
667            tid: None,
668            keys: vec![],
669            raw_severity: None,
670        }
671    }
672
673    /// Sets the moniker of the message.
674    #[must_use = "You must call build on your builder to consume its result"]
675    pub fn set_moniker(mut self, value: ExtendedMoniker) -> Self {
676        self.args.moniker = value;
677        self
678    }
679
680    /// Sets the URL of the message.
681    #[must_use = "You must call build on your builder to consume its result"]
682    pub fn set_url(mut self, value: Option<FlyStr>) -> Self {
683        self.args.component_url = value;
684        self
685    }
686
687    /// Sets the number of dropped messages.
688    /// If value is greater than zero, a DroppedLogs error
689    /// will also be added to the list of errors or updated if
690    /// already present.
691    #[must_use = "You must call build on your builder to consume its result"]
692    pub fn set_dropped(mut self, value: u64) -> Self {
693        if value == 0 {
694            return self;
695        }
696        let val = self.errors.iter_mut().find_map(|error| {
697            if let LogError::DroppedLogs { count } = error { Some(count) } else { None }
698        });
699        if let Some(v) = val {
700            *v = value;
701        } else {
702            self.errors.push(LogError::DroppedLogs { count: value });
703        }
704        self
705    }
706
707    /// Overrides the severity set through the args with a raw severity.
708    pub fn set_raw_severity(mut self, severity: u8) -> Self {
709        self.raw_severity = Some(severity);
710        self
711    }
712
713    /// Sets the number of rolled out messages.
714    /// If value is greater than zero, a RolledOutLogs error
715    /// will also be added to the list of errors or updated if
716    /// already present.
717    #[must_use = "You must call build on your builder to consume its result"]
718    pub fn set_rolled_out(mut self, value: u64) -> Self {
719        if value == 0 {
720            return self;
721        }
722        let val = self.errors.iter_mut().find_map(|error| {
723            if let LogError::RolledOutLogs { count } = error { Some(count) } else { None }
724        });
725        if let Some(v) = val {
726            *v = value;
727        } else {
728            self.errors.push(LogError::RolledOutLogs { count: value });
729        }
730        self
731    }
732
733    /// Sets the severity of the log. This will unset the raw severity.
734    pub fn set_severity(mut self, severity: Severity) -> Self {
735        self.args.severity = severity;
736        self.raw_severity = None;
737        self
738    }
739
740    /// Sets the process ID that logged the message
741    #[must_use = "You must call build on your builder to consume its result"]
742    pub fn set_pid(mut self, value: u64) -> Self {
743        self.pid = Some(value);
744        self
745    }
746
747    /// Sets the thread ID that logged the message
748    #[must_use = "You must call build on your builder to consume its result"]
749    pub fn set_tid(mut self, value: u64) -> Self {
750        self.tid = Some(value);
751        self
752    }
753
754    /// Constructs a LogsData from this builder
755    pub fn build(self) -> LogsData {
756        let mut args = vec![];
757        if let Some(msg) = self.msg {
758            args.push(LogsProperty::String(LogsField::MsgStructured, msg));
759        }
760        let mut payload_fields = vec![DiagnosticsHierarchy::new("message", args, vec![])];
761        if !self.keys.is_empty() {
762            let val = DiagnosticsHierarchy::new("keys", self.keys, vec![]);
763            payload_fields.push(val);
764        }
765        let mut payload = LogsHierarchy::new("root", vec![], payload_fields);
766        payload.sort();
767        let (raw_severity, severity) =
768            self.raw_severity.map(Severity::parse_exact).unwrap_or((None, self.args.severity));
769        let mut ret = LogsData::for_logs(
770            self.args.moniker,
771            Some(payload),
772            self.args.timestamp,
773            self.args.component_url,
774            severity,
775            self.errors,
776        );
777        ret.metadata.raw_severity = raw_severity;
778        ret.metadata.file = self.file;
779        ret.metadata.line = self.line;
780        ret.metadata.pid = self.pid;
781        ret.metadata.tid = self.tid;
782        ret.metadata.tags = Some(self.tags);
783        ret
784    }
785
786    /// Adds an error
787    #[must_use = "You must call build on your builder to consume its result"]
788    pub fn add_error(mut self, error: LogError) -> Self {
789        self.errors.push(error);
790        self
791    }
792
793    /// Sets the message to be printed in the log message
794    #[must_use = "You must call build on your builder to consume its result"]
795    pub fn set_message(mut self, msg: impl Into<String>) -> Self {
796        self.msg = Some(msg.into());
797        self
798    }
799
800    /// Sets the file name that printed this message.
801    #[must_use = "You must call build on your builder to consume its result"]
802    pub fn set_file(mut self, file: impl Into<String>) -> Self {
803        self.file = Some(file.into());
804        self
805    }
806
807    /// Sets the line number that printed this message.
808    #[must_use = "You must call build on your builder to consume its result"]
809    pub fn set_line(mut self, line: u64) -> Self {
810        self.line = Some(line);
811        self
812    }
813
814    /// Adds a property to the list of key value pairs that are a part of this log message.
815    #[must_use = "You must call build on your builder to consume its result"]
816    pub fn add_key(mut self, kvp: Property<LogsField>) -> Self {
817        self.keys.push(kvp);
818        self
819    }
820
821    /// Adds a tag to the list of tags that precede this log message.
822    #[must_use = "You must call build on your builder to consume its result"]
823    pub fn add_tag(mut self, tag: impl Into<String>) -> Self {
824        self.tags.push(tag.into());
825        self
826    }
827}
828
829impl Data<Logs> {
830    /// Creates a new data instance for logs.
831    pub fn for_logs(
832        moniker: ExtendedMoniker,
833        payload: Option<LogsHierarchy>,
834        timestamp: impl Into<Timestamp>,
835        component_url: Option<FlyStr>,
836        severity: impl Into<Severity>,
837        errors: Vec<LogError>,
838    ) -> Self {
839        let errors = if errors.is_empty() { None } else { Some(errors) };
840
841        Data {
842            moniker,
843            version: SCHEMA_VERSION,
844            data_source: DataSource::Logs,
845            payload,
846            metadata: LogsMetadata {
847                timestamp: timestamp.into(),
848                component_url,
849                severity: severity.into(),
850                raw_severity: None,
851                errors,
852                file: None,
853                line: None,
854                pid: None,
855                tags: None,
856                tid: None,
857                dropped: None,
858                size_bytes: None,
859            },
860        }
861    }
862
863    /// Sets the severity from a raw severity number. Overrides the severity to match the raw
864    /// severity.
865    pub fn set_raw_severity(&mut self, raw_severity: u8) {
866        self.metadata.raw_severity = Some(raw_severity);
867        self.metadata.severity = Severity::from(raw_severity);
868    }
869
870    /// Sets the severity of the log. This will unset the raw severity.
871    pub fn set_severity(&mut self, severity: Severity) {
872        self.metadata.severity = severity;
873        self.metadata.raw_severity = None;
874    }
875
876    /// Returns the string log associated with the message, if one exists.
877    pub fn msg(&self) -> Option<&str> {
878        self.payload_message().as_ref().and_then(|p| {
879            p.properties.iter().find_map(|property| match property {
880                LogsProperty::String(LogsField::MsgStructured, msg) => Some(msg.as_str()),
881                _ => None,
882            })
883        })
884    }
885
886    /// If the log has a message, returns a shared reference to the message contents.
887    pub fn msg_mut(&mut self) -> Option<&mut String> {
888        self.payload_message_mut().and_then(|p| {
889            p.properties.iter_mut().find_map(|property| match property {
890                LogsProperty::String(LogsField::MsgStructured, msg) => Some(msg),
891                _ => None,
892            })
893        })
894    }
895
896    /// If the log has message, returns an exclusive reference to it.
897    pub fn payload_message(&self) -> Option<&DiagnosticsHierarchy<LogsField>> {
898        self.payload
899            .as_ref()
900            .and_then(|p| p.children.iter().find(|property| property.name.as_str() == "message"))
901    }
902
903    /// If the log has structured keys, returns an exclusive reference to them.
904    pub fn payload_keys(&self) -> Option<&DiagnosticsHierarchy<LogsField>> {
905        self.payload
906            .as_ref()
907            .and_then(|p| p.children.iter().find(|property| property.name.as_str() == "keys"))
908    }
909
910    pub fn metadata(&self) -> &LogsMetadata {
911        &self.metadata
912    }
913
914    /// Returns an iterator over the payload keys as strings with the format "key=value".
915    pub fn payload_keys_strings(&self) -> Box<dyn Iterator<Item = String> + Send + '_> {
916        let maybe_iter = self.payload_keys().map(|p| {
917            Box::new(p.properties.iter().filter_map(|property| match property {
918                LogsProperty::String(LogsField::Tag, _tag) => None,
919                LogsProperty::String(LogsField::ProcessId, _tag) => None,
920                LogsProperty::String(LogsField::ThreadId, _tag) => None,
921                LogsProperty::String(LogsField::Dropped, _tag) => None,
922                LogsProperty::String(LogsField::Msg, _tag) => None,
923                LogsProperty::String(LogsField::FilePath, _tag) => None,
924                LogsProperty::String(LogsField::LineNumber, _tag) => None,
925                LogsProperty::String(
926                    key @ (LogsField::Other(_) | LogsField::MsgStructured),
927                    value,
928                ) => Some(format!("{key}={value}")),
929                LogsProperty::Bytes(key @ (LogsField::Other(_) | LogsField::MsgStructured), _) => {
930                    Some(format!("{key} = <bytes>"))
931                }
932                LogsProperty::Int(
933                    key @ (LogsField::Other(_) | LogsField::MsgStructured),
934                    value,
935                ) => Some(format!("{key}={value}")),
936                LogsProperty::Uint(
937                    key @ (LogsField::Other(_) | LogsField::MsgStructured),
938                    value,
939                ) => Some(format!("{key}={value}")),
940                LogsProperty::Double(
941                    key @ (LogsField::Other(_) | LogsField::MsgStructured),
942                    value,
943                ) => Some(format!("{key}={value}")),
944                LogsProperty::Bool(
945                    key @ (LogsField::Other(_) | LogsField::MsgStructured),
946                    value,
947                ) => Some(format!("{key}={value}")),
948                LogsProperty::DoubleArray(
949                    key @ (LogsField::Other(_) | LogsField::MsgStructured),
950                    value,
951                ) => Some(format!("{key}={value:?}")),
952                LogsProperty::IntArray(
953                    key @ (LogsField::Other(_) | LogsField::MsgStructured),
954                    value,
955                ) => Some(format!("{key}={value:?}")),
956                LogsProperty::UintArray(
957                    key @ (LogsField::Other(_) | LogsField::MsgStructured),
958                    value,
959                ) => Some(format!("{key}={value:?}")),
960                LogsProperty::StringList(
961                    key @ (LogsField::Other(_) | LogsField::MsgStructured),
962                    value,
963                ) => Some(format!("{key}={value:?}")),
964                _ => None,
965            }))
966        });
967        match maybe_iter {
968            Some(i) => Box::new(i),
969            None => Box::new(std::iter::empty()),
970        }
971    }
972
973    /// If the log has a message, returns a mutable reference to it.
974    pub fn payload_message_mut(&mut self) -> Option<&mut DiagnosticsHierarchy<LogsField>> {
975        self.payload.as_mut().and_then(|p| {
976            p.children.iter_mut().find(|property| property.name.as_str() == "message")
977        })
978    }
979
980    /// Returns the file path associated with the message, if one exists.
981    pub fn file_path(&self) -> Option<&str> {
982        self.metadata.file.as_deref()
983    }
984
985    /// Returns the line number associated with the message, if one exists.
986    pub fn line_number(&self) -> Option<&u64> {
987        self.metadata.line.as_ref()
988    }
989
990    /// Returns the pid associated with the message, if one exists.
991    pub fn pid(&self) -> Option<u64> {
992        self.metadata.pid
993    }
994
995    /// Returns the tid associated with the message, if one exists.
996    pub fn tid(&self) -> Option<u64> {
997        self.metadata.tid
998    }
999
1000    /// Returns the tags associated with the message, if any exist.
1001    pub fn tags(&self) -> Option<&Vec<String>> {
1002        self.metadata.tags.as_ref()
1003    }
1004
1005    /// Returns the severity level of this log.
1006    pub fn severity(&self) -> Severity {
1007        self.metadata.severity
1008    }
1009
1010    /// Returns number of dropped logs if reported in the message.
1011    pub fn dropped_logs(&self) -> Option<u64> {
1012        self.metadata.errors.as_ref().and_then(|errors| {
1013            errors.iter().find_map(|e| match e {
1014                LogError::DroppedLogs { count } => Some(*count),
1015                _ => None,
1016            })
1017        })
1018    }
1019
1020    /// Returns number of rolled out logs if reported in the message.
1021    pub fn rolled_out_logs(&self) -> Option<u64> {
1022        self.metadata.errors.as_ref().and_then(|errors| {
1023            errors.iter().find_map(|e| match e {
1024                LogError::RolledOutLogs { count } => Some(*count),
1025                _ => None,
1026            })
1027        })
1028    }
1029
1030    /// Returns the component name. This only makes sense for v1 components.
1031    pub fn component_name(&self) -> Cow<'_, str> {
1032        match &self.moniker {
1033            ExtendedMoniker::ComponentManager => {
1034                Cow::Borrowed(EXTENDED_MONIKER_COMPONENT_MANAGER_STR)
1035            }
1036            ExtendedMoniker::ComponentInstance(moniker) => {
1037                if moniker.is_root() {
1038                    Cow::Borrowed(ROOT_MONIKER_REPR)
1039                } else {
1040                    Cow::Owned(moniker.leaf().unwrap().to_string())
1041                }
1042            }
1043        }
1044    }
1045}
1046
1047/// Display options for unstructured logs.
1048#[derive(Clone, Copy, Debug)]
1049pub struct LogTextDisplayOptions {
1050    /// Whether or not to display the full moniker.
1051    pub show_full_moniker: bool,
1052
1053    /// Whether or not to display metadata like PID & TID.
1054    pub show_metadata: bool,
1055
1056    /// Whether or not to display tags provided by the log producer.
1057    pub show_tags: bool,
1058
1059    /// Whether or not to display the source location which produced the log.
1060    pub show_file: bool,
1061
1062    /// Whether to include ANSI color codes in the output.
1063    pub color: LogTextColor,
1064
1065    /// How to print timestamps for this log message.
1066    pub time_format: LogTimeDisplayFormat,
1067}
1068
1069impl Default for LogTextDisplayOptions {
1070    fn default() -> Self {
1071        Self {
1072            show_full_moniker: true,
1073            show_metadata: true,
1074            show_tags: true,
1075            show_file: true,
1076            color: Default::default(),
1077            time_format: Default::default(),
1078        }
1079    }
1080}
1081
1082/// Configuration for the color of a log line that is displayed in tools using [`LogTextPresenter`].
1083#[derive(Clone, Copy, Debug, Default)]
1084pub enum LogTextColor {
1085    /// Do not print this log with ANSI colors.
1086    #[default]
1087    None,
1088
1089    /// Display color codes according to log severity and presence of dropped or rolled out logs.
1090    BySeverity,
1091
1092    /// Highlight this message as noteworthy regardless of severity, e.g. for known spam messages.
1093    Highlight,
1094}
1095
1096impl LogTextColor {
1097    fn begin_record(&self, f: &mut fmt::Formatter<'_>, severity: Severity) -> fmt::Result {
1098        match self {
1099            LogTextColor::BySeverity => match severity {
1100                Severity::Fatal => {
1101                    write!(f, "{}{}", color::Bg(color::Red), color::Fg(color::White))?
1102                }
1103                Severity::Error => write!(f, "{}", color::Fg(color::Red))?,
1104                Severity::Warn => write!(f, "{}", color::Fg(color::Yellow))?,
1105                Severity::Info => (),
1106                Severity::Debug => write!(f, "{}", color::Fg(color::LightBlue))?,
1107                Severity::Trace => write!(f, "{}", color::Fg(color::LightMagenta))?,
1108            },
1109            LogTextColor::Highlight => write!(f, "{}", color::Fg(color::LightYellow))?,
1110            LogTextColor::None => {}
1111        }
1112        Ok(())
1113    }
1114
1115    fn begin_lost_message_counts(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1116        if let LogTextColor::BySeverity = self {
1117            // This will be reset below before the next line.
1118            write!(f, "{}", color::Fg(color::Yellow))?;
1119        }
1120        Ok(())
1121    }
1122
1123    fn end_record(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1124        match self {
1125            LogTextColor::BySeverity | LogTextColor::Highlight => write!(f, "{}", style::Reset)?,
1126            LogTextColor::None => {}
1127        };
1128        Ok(())
1129    }
1130}
1131
1132/// Options for the timezone associated to the timestamp of a log line.
1133#[derive(Clone, Copy, Debug, PartialEq)]
1134pub enum Timezone {
1135    /// Display a timestamp in terms of the local timezone as reported by the operating system.
1136    Local,
1137
1138    /// Display a timestamp in terms of UTC.
1139    Utc,
1140}
1141
1142impl Timezone {
1143    fn format(&self, seconds: i64, rem_nanos: u32) -> impl std::fmt::Display {
1144        const TIMESTAMP_FORMAT: &str = "%Y-%m-%d %H:%M:%S.%3f";
1145        match self {
1146            Timezone::Local => {
1147                Local.timestamp_opt(seconds, rem_nanos).unwrap().format(TIMESTAMP_FORMAT)
1148            }
1149            Timezone::Utc => {
1150                Utc.timestamp_opt(seconds, rem_nanos).unwrap().format(TIMESTAMP_FORMAT)
1151            }
1152        }
1153    }
1154}
1155
1156/// Configuration for how to display the timestamp associated to a log line.
1157#[derive(Clone, Copy, Debug, Default)]
1158pub enum LogTimeDisplayFormat {
1159    /// Display the log message's timestamp as monotonic nanoseconds since boot.
1160    #[default]
1161    Original,
1162
1163    /// Display the log's timestamp as a human-readable string in ISO 8601 format.
1164    WallTime {
1165        /// The format for displaying a timestamp as a string.
1166        tz: Timezone,
1167
1168        /// The offset to apply to the original device-monotonic time before printing it as a
1169        /// human-readable timestamp.
1170        offset: i64,
1171    },
1172}
1173
1174impl LogTimeDisplayFormat {
1175    fn write_timestamp(&self, f: &mut fmt::Formatter<'_>, time: Timestamp) -> fmt::Result {
1176        const NANOS_IN_SECOND: i64 = 1_000_000_000;
1177
1178        match self {
1179            // Don't try to print a human readable string if it's going to be in 1970, fall back
1180            // to monotonic.
1181            Self::Original | Self::WallTime { offset: 0, .. } => {
1182                let time: Duration =
1183                    Duration::from_nanos(time.into_nanos().try_into().unwrap_or(0));
1184                write!(f, "[{:05}.{:06}]", time.as_secs(), time.as_micros() % MICROS_IN_SEC)?;
1185            }
1186            Self::WallTime { tz, offset } => {
1187                let adjusted = time.into_nanos() + offset;
1188                let seconds = adjusted / NANOS_IN_SECOND;
1189                let rem_nanos = (adjusted % NANOS_IN_SECOND) as u32;
1190                let formatted = tz.format(seconds, rem_nanos);
1191                write!(f, "[{formatted}]")?;
1192            }
1193        }
1194        Ok(())
1195    }
1196}
1197
1198/// Used to control stringification options of Data<Logs>
1199pub struct LogTextPresenter<'a> {
1200    /// The log to parameterize
1201    log: &'a Data<Logs>,
1202
1203    /// Options for stringifying the log
1204    options: LogTextDisplayOptions,
1205}
1206
1207impl<'a> LogTextPresenter<'a> {
1208    /// Creates a new LogTextPresenter with the specified options and
1209    /// log message. This presenter is bound to the lifetime of the
1210    /// underlying log message.
1211    pub fn new(log: &'a Data<Logs>, options: LogTextDisplayOptions) -> Self {
1212        Self { log, options }
1213    }
1214}
1215
1216impl fmt::Display for Data<Logs> {
1217    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1218        LogTextPresenter::new(self, Default::default()).fmt(f)
1219    }
1220}
1221
1222impl Deref for LogTextPresenter<'_> {
1223    type Target = Data<Logs>;
1224    fn deref(&self) -> &Self::Target {
1225        self.log
1226    }
1227}
1228
1229impl fmt::Display for LogTextPresenter<'_> {
1230    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1231        self.options.color.begin_record(f, self.log.severity())?;
1232        self.options.time_format.write_timestamp(f, self.metadata.timestamp)?;
1233
1234        if self.options.show_metadata {
1235            match self.pid() {
1236                Some(pid) => write!(f, "[{pid}]")?,
1237                None => write!(f, "[]")?,
1238            }
1239            match self.tid() {
1240                Some(tid) => write!(f, "[{tid}]")?,
1241                None => write!(f, "[]")?,
1242            }
1243        }
1244
1245        let moniker = if self.options.show_full_moniker {
1246            match &self.moniker {
1247                ExtendedMoniker::ComponentManager => {
1248                    Cow::Borrowed(EXTENDED_MONIKER_COMPONENT_MANAGER_STR)
1249                }
1250                ExtendedMoniker::ComponentInstance(instance) => {
1251                    if instance.is_root() {
1252                        Cow::Borrowed(ROOT_MONIKER_REPR)
1253                    } else {
1254                        Cow::Owned(instance.to_string())
1255                    }
1256                }
1257            }
1258        } else {
1259            self.component_name()
1260        };
1261        write!(f, "[{moniker}]")?;
1262
1263        if self.options.show_tags {
1264            match &self.metadata.tags {
1265                Some(tags) if !tags.is_empty() => {
1266                    let mut filtered =
1267                        tags.iter().filter(|tag| *tag != moniker.as_ref()).peekable();
1268                    if filtered.peek().is_some() {
1269                        write!(f, "[{}]", filtered.join(","))?;
1270                    }
1271                }
1272                _ => {}
1273            }
1274        }
1275
1276        write!(f, " {}:", self.metadata.severity)?;
1277
1278        if self.options.show_file {
1279            match (&self.metadata.file, &self.metadata.line) {
1280                (Some(file), Some(line)) => write!(f, " [{file}({line})]")?,
1281                (Some(file), None) => write!(f, " [{file}]")?,
1282                _ => (),
1283            }
1284        }
1285
1286        if let Some(msg) = self.msg() {
1287            write!(f, " {msg}")?;
1288        } else {
1289            write!(f, " <missing message>")?;
1290        }
1291        for kvp in self.payload_keys_strings() {
1292            write!(f, " {kvp}")?;
1293        }
1294
1295        let dropped = self.log.dropped_logs().unwrap_or_default();
1296        let rolled = self.log.rolled_out_logs().unwrap_or_default();
1297        if dropped != 0 || rolled != 0 {
1298            self.options.color.begin_lost_message_counts(f)?;
1299            if dropped != 0 {
1300                write!(f, " [dropped={dropped}]")?;
1301            }
1302            if rolled != 0 {
1303                write!(f, " [rolled={rolled}]")?;
1304            }
1305        }
1306
1307        self.options.color.end_record(f)?;
1308
1309        Ok(())
1310    }
1311}
1312
1313impl Eq for Data<Logs> {}
1314
1315impl PartialOrd for Data<Logs> {
1316    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1317        Some(self.cmp(other))
1318    }
1319}
1320
1321impl Ord for Data<Logs> {
1322    fn cmp(&self, other: &Self) -> Ordering {
1323        self.metadata.timestamp.cmp(&other.metadata.timestamp)
1324    }
1325}
1326
1327/// An enum containing well known argument names passed through logs, as well
1328/// as an `Other` variant for any other argument names.
1329///
1330/// This contains the fields of logs sent as a [`LogMessage`].
1331///
1332/// [`LogMessage`]: https://fuchsia.dev/reference/fidl/fuchsia.logger#LogMessage
1333#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize)]
1334pub enum LogsField {
1335    ProcessId,
1336    ThreadId,
1337    Dropped,
1338    Tag,
1339    Msg,
1340    MsgStructured,
1341    FilePath,
1342    LineNumber,
1343    Other(String),
1344}
1345
1346impl fmt::Display for LogsField {
1347    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1348        match self {
1349            LogsField::ProcessId => write!(f, "pid"),
1350            LogsField::ThreadId => write!(f, "tid"),
1351            LogsField::Dropped => write!(f, "num_dropped"),
1352            LogsField::Tag => write!(f, "tag"),
1353            LogsField::Msg => write!(f, "message"),
1354            LogsField::MsgStructured => write!(f, "value"),
1355            LogsField::FilePath => write!(f, "file_path"),
1356            LogsField::LineNumber => write!(f, "line_number"),
1357            LogsField::Other(name) => write!(f, "{name}"),
1358        }
1359    }
1360}
1361
1362// TODO(https://fxbug.dev/42127608) - ensure that strings reported here align with naming
1363// decisions made for the structured log format sent by other components.
1364/// The label for the process koid in the log metadata.
1365pub const PID_LABEL: &str = "pid";
1366/// The label for the thread koid in the log metadata.
1367pub const TID_LABEL: &str = "tid";
1368/// The label for the number of dropped logs in the log metadata.
1369pub const DROPPED_LABEL: &str = "num_dropped";
1370/// The label for a tag in the log metadata.
1371pub const TAG_LABEL: &str = "tag";
1372/// The label for the contents of a message in the log payload.
1373pub const MESSAGE_LABEL_STRUCTURED: &str = "value";
1374/// The label for the message in the log payload.
1375pub const MESSAGE_LABEL: &str = "message";
1376/// The label for the file associated with a log line.
1377pub const FILE_PATH_LABEL: &str = "file";
1378/// The label for the line number in the file associated with a log line.
1379pub const LINE_NUMBER_LABEL: &str = "line";
1380
1381impl AsRef<str> for LogsField {
1382    fn as_ref(&self) -> &str {
1383        match self {
1384            Self::ProcessId => PID_LABEL,
1385            Self::ThreadId => TID_LABEL,
1386            Self::Dropped => DROPPED_LABEL,
1387            Self::Tag => TAG_LABEL,
1388            Self::Msg => MESSAGE_LABEL,
1389            Self::FilePath => FILE_PATH_LABEL,
1390            Self::LineNumber => LINE_NUMBER_LABEL,
1391            Self::MsgStructured => MESSAGE_LABEL_STRUCTURED,
1392            Self::Other(str) => str.as_str(),
1393        }
1394    }
1395}
1396
1397impl<T> From<T> for LogsField
1398where
1399    // Deref instead of AsRef b/c LogsField: AsRef<str> so this conflicts with concrete From<Self>
1400    T: Deref<Target = str>,
1401{
1402    fn from(s: T) -> Self {
1403        match s.as_ref() {
1404            PID_LABEL => Self::ProcessId,
1405            TID_LABEL => Self::ThreadId,
1406            DROPPED_LABEL => Self::Dropped,
1407            TAG_LABEL => Self::Tag,
1408            MESSAGE_LABEL => Self::Msg,
1409            FILE_PATH_LABEL => Self::FilePath,
1410            LINE_NUMBER_LABEL => Self::LineNumber,
1411            MESSAGE_LABEL_STRUCTURED => Self::MsgStructured,
1412            _ => Self::Other(s.to_string()),
1413        }
1414    }
1415}
1416
1417impl FromStr for LogsField {
1418    type Err = ();
1419    fn from_str(s: &str) -> Result<Self, Self::Err> {
1420        Ok(Self::from(s))
1421    }
1422}
1423
1424/// Possible errors that can come in a `DiagnosticsData` object where the data source is
1425/// `DataSource::Logs`.
1426#[cfg_attr(feature = "json_schema", derive(JsonSchema))]
1427#[derive(Clone, Deserialize, Debug, Eq, PartialEq, Serialize)]
1428pub enum LogError {
1429    /// Represents the number of logs that were dropped by the component writing the logs due to an
1430    /// error writing to the socket before succeeding to write a log.
1431    #[serde(rename = "dropped_logs")]
1432    DroppedLogs { count: u64 },
1433    /// Represents the number of logs that were dropped for a component by the archivist due to the
1434    /// log buffer execeeding its maximum capacity before the current message.
1435    #[serde(rename = "rolled_out_logs")]
1436    RolledOutLogs { count: u64 },
1437    #[serde(rename = "parse_record")]
1438    FailedToParseRecord(String),
1439    #[serde(rename = "other")]
1440    Other { message: String },
1441}
1442
1443const DROPPED_PAYLOAD_MSG: &str = "Schema failed to fit component budget.";
1444
1445impl MetadataError for LogError {
1446    fn dropped_payload() -> Self {
1447        Self::Other { message: DROPPED_PAYLOAD_MSG.into() }
1448    }
1449
1450    fn message(&self) -> Option<&str> {
1451        match self {
1452            Self::FailedToParseRecord(msg) => Some(msg.as_str()),
1453            Self::Other { message } => Some(message.as_str()),
1454            _ => None,
1455        }
1456    }
1457}
1458
1459/// Possible error that can come in a `DiagnosticsData` object where the data source is
1460/// `DataSource::Inspect`..
1461#[derive(Debug, PartialEq, Clone, Eq)]
1462pub struct InspectError {
1463    pub message: String,
1464}
1465
1466impl MetadataError for InspectError {
1467    fn dropped_payload() -> Self {
1468        Self { message: "Schema failed to fit component budget.".into() }
1469    }
1470
1471    fn message(&self) -> Option<&str> {
1472        Some(self.message.as_str())
1473    }
1474}
1475
1476impl fmt::Display for InspectError {
1477    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1478        write!(f, "{}", self.message)
1479    }
1480}
1481
1482impl Borrow<str> for InspectError {
1483    fn borrow(&self) -> &str {
1484        &self.message
1485    }
1486}
1487
1488impl Serialize for InspectError {
1489    fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
1490        self.message.serialize(ser)
1491    }
1492}
1493
1494impl<'de> Deserialize<'de> for InspectError {
1495    fn deserialize<D>(de: D) -> Result<Self, D::Error>
1496    where
1497        D: Deserializer<'de>,
1498    {
1499        let message = String::deserialize(de)?;
1500        Ok(Self { message })
1501    }
1502}
1503
1504#[cfg(test)]
1505mod tests {
1506    use super::*;
1507    use diagnostics_hierarchy::hierarchy;
1508    use selectors::FastError;
1509    use serde_json::json;
1510    use test_case::test_case;
1511
1512    const TEST_URL: &str = "fuchsia-pkg://test";
1513
1514    #[fuchsia::test]
1515    fn test_canonical_json_inspect_formatting() {
1516        let mut hierarchy = hierarchy! {
1517            root: {
1518                x: "foo",
1519            }
1520        };
1521
1522        hierarchy.sort();
1523        let json_schema = InspectDataBuilder::new(
1524            "a/b/c/d".try_into().unwrap(),
1525            TEST_URL,
1526            Timestamp::from_nanos(123456i64),
1527        )
1528        .with_hierarchy(hierarchy)
1529        .with_name(InspectHandleName::filename("test_file_plz_ignore.inspect"))
1530        .build();
1531
1532        let result_json =
1533            serde_json::to_value(&json_schema).expect("serialization should succeed.");
1534
1535        let expected_json = json!({
1536          "moniker": "a/b/c/d",
1537          "version": 1,
1538          "data_source": "Inspect",
1539          "payload": {
1540            "root": {
1541              "x": "foo"
1542            }
1543          },
1544          "metadata": {
1545            "component_url": TEST_URL,
1546            "filename": "test_file_plz_ignore.inspect",
1547            "timestamp": 123456,
1548          }
1549        });
1550
1551        pretty_assertions::assert_eq!(result_json, expected_json, "golden diff failed.");
1552    }
1553
1554    #[fuchsia::test]
1555    fn test_errorful_json_inspect_formatting() {
1556        let json_schema = InspectDataBuilder::new(
1557            "a/b/c/d".try_into().unwrap(),
1558            TEST_URL,
1559            Timestamp::from_nanos(123456i64),
1560        )
1561        .with_name(InspectHandleName::filename("test_file_plz_ignore.inspect"))
1562        .with_errors(vec![InspectError { message: "too much fun being had.".to_string() }])
1563        .build();
1564
1565        let result_json =
1566            serde_json::to_value(&json_schema).expect("serialization should succeed.");
1567
1568        let expected_json = json!({
1569          "moniker": "a/b/c/d",
1570          "version": 1,
1571          "data_source": "Inspect",
1572          "payload": null,
1573          "metadata": {
1574            "component_url": TEST_URL,
1575            "errors": ["too much fun being had."],
1576            "filename": "test_file_plz_ignore.inspect",
1577            "timestamp": 123456,
1578          }
1579        });
1580
1581        pretty_assertions::assert_eq!(result_json, expected_json, "golden diff failed.");
1582    }
1583
1584    fn parse_selectors(strings: Vec<&str>) -> Vec<Selector> {
1585        strings
1586            .iter()
1587            .map(|s| match selectors::parse_selector::<FastError>(s) {
1588                Ok(selector) => selector,
1589                Err(e) => panic!("Couldn't parse selector {s}: {e}"),
1590            })
1591            .collect::<Vec<_>>()
1592    }
1593
1594    #[fuchsia::test]
1595    fn test_filter_returns_none_on_empty_hierarchy() {
1596        let data = InspectDataBuilder::new(
1597            "a/b/c/d".try_into().unwrap(),
1598            TEST_URL,
1599            Timestamp::from_nanos(123456i64),
1600        )
1601        .build();
1602        let selectors = parse_selectors(vec!["a/b/c/d:foo"]);
1603        assert_eq!(data.filter(&selectors).expect("Filter OK"), None);
1604    }
1605
1606    #[fuchsia::test]
1607    fn test_filter_returns_none_on_selector_mismatch() {
1608        let mut hierarchy = hierarchy! {
1609            root: {
1610                x: "foo",
1611            }
1612        };
1613        hierarchy.sort();
1614        let data = InspectDataBuilder::new(
1615            "b/c/d/e".try_into().unwrap(),
1616            TEST_URL,
1617            Timestamp::from_nanos(123456i64),
1618        )
1619        .with_hierarchy(hierarchy)
1620        .build();
1621        let selectors = parse_selectors(vec!["a/b/c/d:foo"]);
1622        assert_eq!(data.filter(&selectors).expect("Filter OK"), None);
1623    }
1624
1625    #[fuchsia::test]
1626    fn test_filter_returns_none_on_data_mismatch() {
1627        let mut hierarchy = hierarchy! {
1628            root: {
1629                x: "foo",
1630            }
1631        };
1632        hierarchy.sort();
1633        let data = InspectDataBuilder::new(
1634            "a/b/c/d".try_into().unwrap(),
1635            TEST_URL,
1636            Timestamp::from_nanos(123456i64),
1637        )
1638        .with_hierarchy(hierarchy)
1639        .build();
1640        let selectors = parse_selectors(vec!["a/b/c/d:foo"]);
1641
1642        assert_eq!(data.filter(&selectors).expect("FIlter OK"), None);
1643    }
1644
1645    #[fuchsia::test]
1646    fn test_filter_returns_matching_data() {
1647        let mut hierarchy = hierarchy! {
1648            root: {
1649                x: "foo",
1650                y: "bar",
1651            }
1652        };
1653        hierarchy.sort();
1654        let data = InspectDataBuilder::new(
1655            "a/b/c/d".try_into().unwrap(),
1656            TEST_URL,
1657            Timestamp::from_nanos(123456i64),
1658        )
1659        .with_name(InspectHandleName::filename("test_file_plz_ignore.inspect"))
1660        .with_hierarchy(hierarchy)
1661        .build();
1662        let selectors = parse_selectors(vec!["a/b/c/d:root:x"]);
1663
1664        let expected_json = json!({
1665          "moniker": "a/b/c/d",
1666          "version": 1,
1667          "data_source": "Inspect",
1668          "payload": {
1669            "root": {
1670              "x": "foo"
1671            }
1672          },
1673          "metadata": {
1674            "component_url": TEST_URL,
1675            "filename": "test_file_plz_ignore.inspect",
1676            "timestamp": 123456,
1677          }
1678        });
1679
1680        let result_json = serde_json::to_value(data.filter(&selectors).expect("Filter Ok"))
1681            .expect("serialization should succeed.");
1682
1683        pretty_assertions::assert_eq!(result_json, expected_json, "golden diff failed.");
1684    }
1685
1686    #[fuchsia::test]
1687    fn default_builder_test() {
1688        let builder = LogsDataBuilder::new(BuilderArgs {
1689            component_url: Some("url".into()),
1690            moniker: ExtendedMoniker::parse_str("moniker").unwrap(),
1691            severity: Severity::Info,
1692            timestamp: Timestamp::from_nanos(0),
1693        });
1694        //let tree = builder.build();
1695        let expected_json = json!({
1696          "moniker": "moniker",
1697          "version": 1,
1698          "data_source": "Logs",
1699          "payload": {
1700              "root":
1701              {
1702                  "message":{}
1703              }
1704          },
1705          "metadata": {
1706            "component_url": "url",
1707              "severity": "INFO",
1708              "tags": [],
1709
1710            "timestamp": 0,
1711          }
1712        });
1713        let result_json =
1714            serde_json::to_value(builder.build()).expect("serialization should succeed.");
1715        pretty_assertions::assert_eq!(result_json, expected_json, "golden diff failed.");
1716    }
1717
1718    #[fuchsia::test]
1719    fn regular_message_test() {
1720        let builder = LogsDataBuilder::new(BuilderArgs {
1721            component_url: Some("url".into()),
1722            moniker: ExtendedMoniker::parse_str("moniker").unwrap(),
1723            severity: Severity::Info,
1724            timestamp: Timestamp::from_nanos(0),
1725        })
1726        .set_message("app")
1727        .set_file("test file.cc")
1728        .set_line(420)
1729        .set_pid(1001)
1730        .set_tid(200)
1731        .set_dropped(2)
1732        .add_tag("You're")
1733        .add_tag("IT!")
1734        .add_key(LogsProperty::String(LogsField::Other("key".to_string()), "value".to_string()));
1735        // TODO(https://fxbug.dev/42157027): Convert to our custom DSL when possible.
1736        let expected_json = json!({
1737          "moniker": "moniker",
1738          "version": 1,
1739          "data_source": "Logs",
1740          "payload": {
1741              "root":
1742              {
1743                  "keys":{
1744                      "key":"value"
1745                  },
1746                  "message":{
1747                      "value":"app"
1748                  }
1749              }
1750          },
1751          "metadata": {
1752            "errors": [],
1753            "component_url": "url",
1754              "errors": [{"dropped_logs":{"count":2}}],
1755              "file": "test file.cc",
1756              "line": 420,
1757              "pid": 1001,
1758              "severity": "INFO",
1759              "tags": ["You're", "IT!"],
1760              "tid": 200,
1761
1762            "timestamp": 0,
1763          }
1764        });
1765        let result_json =
1766            serde_json::to_value(builder.build()).expect("serialization should succeed.");
1767        pretty_assertions::assert_eq!(result_json, expected_json, "golden diff failed.");
1768    }
1769
1770    #[fuchsia::test]
1771    fn display_for_logs() {
1772        let data = LogsDataBuilder::new(BuilderArgs {
1773            timestamp: Timestamp::from_nanos(12345678000i64),
1774            component_url: Some(FlyStr::from("fake-url")),
1775            moniker: ExtendedMoniker::parse_str("moniker").unwrap(),
1776            severity: Severity::Info,
1777        })
1778        .set_pid(123)
1779        .set_tid(456)
1780        .set_message("some message".to_string())
1781        .set_file("some_file.cc".to_string())
1782        .set_line(420)
1783        .add_tag("foo")
1784        .add_tag("bar")
1785        .add_key(LogsProperty::String(LogsField::Other("test".to_string()), "property".to_string()))
1786        .add_key(LogsProperty::String(LogsField::MsgStructured, "test".to_string()))
1787        .build();
1788
1789        assert_eq!(
1790            "[00012.345678][123][456][moniker][foo,bar] INFO: [some_file.cc(420)] some message test=property value=test",
1791            format!("{data}")
1792        )
1793    }
1794
1795    #[fuchsia::test]
1796    fn display_for_logs_with_duplicate_moniker() {
1797        let data = LogsDataBuilder::new(BuilderArgs {
1798            timestamp: Timestamp::from_nanos(12345678000i64),
1799            component_url: Some(FlyStr::from("fake-url")),
1800            moniker: ExtendedMoniker::parse_str("moniker").unwrap(),
1801            severity: Severity::Info,
1802        })
1803        .set_pid(123)
1804        .set_tid(456)
1805        .set_message("some message".to_string())
1806        .set_file("some_file.cc".to_string())
1807        .set_line(420)
1808        .add_tag("moniker")
1809        .add_tag("bar")
1810        .add_tag("moniker")
1811        .add_key(LogsProperty::String(LogsField::Other("test".to_string()), "property".to_string()))
1812        .add_key(LogsProperty::String(LogsField::MsgStructured, "test".to_string()))
1813        .build();
1814
1815        assert_eq!(
1816            "[00012.345678][123][456][moniker][bar] INFO: [some_file.cc(420)] some message test=property value=test",
1817            format!("{data}")
1818        )
1819    }
1820
1821    #[fuchsia::test]
1822    fn display_for_logs_with_duplicate_moniker_and_no_other_tags() {
1823        let data = LogsDataBuilder::new(BuilderArgs {
1824            timestamp: Timestamp::from_nanos(12345678000i64),
1825            component_url: Some(FlyStr::from("fake-url")),
1826            moniker: ExtendedMoniker::parse_str("moniker").unwrap(),
1827            severity: Severity::Info,
1828        })
1829        .set_pid(123)
1830        .set_tid(456)
1831        .set_message("some message".to_string())
1832        .set_file("some_file.cc".to_string())
1833        .set_line(420)
1834        .add_tag("moniker")
1835        .add_tag("moniker")
1836        .add_key(LogsProperty::String(LogsField::Other("test".to_string()), "property".to_string()))
1837        .add_key(LogsProperty::String(LogsField::MsgStructured, "test".to_string()))
1838        .build();
1839
1840        assert_eq!(
1841            "[00012.345678][123][456][moniker] INFO: [some_file.cc(420)] some message test=property value=test",
1842            format!("{data}")
1843        )
1844    }
1845
1846    #[fuchsia::test]
1847    fn display_for_logs_partial_moniker() {
1848        let data = LogsDataBuilder::new(BuilderArgs {
1849            timestamp: Timestamp::from_nanos(12345678000i64),
1850            component_url: Some(FlyStr::from("fake-url")),
1851            moniker: ExtendedMoniker::parse_str("test/moniker").unwrap(),
1852            severity: Severity::Info,
1853        })
1854        .set_pid(123)
1855        .set_tid(456)
1856        .set_message("some message".to_string())
1857        .set_file("some_file.cc".to_string())
1858        .set_line(420)
1859        .add_tag("foo")
1860        .add_tag("bar")
1861        .add_key(LogsProperty::String(LogsField::Other("test".to_string()), "property".to_string()))
1862        .add_key(LogsProperty::String(LogsField::MsgStructured, "test".to_string()))
1863        .build();
1864
1865        assert_eq!(
1866            "[00012.345678][123][456][moniker][foo,bar] INFO: [some_file.cc(420)] some message test=property value=test",
1867            format!(
1868                "{}",
1869                LogTextPresenter::new(
1870                    &data,
1871                    LogTextDisplayOptions { show_full_moniker: false, ..Default::default() }
1872                )
1873            )
1874        )
1875    }
1876
1877    #[fuchsia::test]
1878    fn display_for_logs_exclude_metadata() {
1879        let data = LogsDataBuilder::new(BuilderArgs {
1880            timestamp: Timestamp::from_nanos(12345678000i64),
1881            component_url: Some(FlyStr::from("fake-url")),
1882            moniker: ExtendedMoniker::parse_str("moniker").unwrap(),
1883            severity: Severity::Info,
1884        })
1885        .set_pid(123)
1886        .set_tid(456)
1887        .set_message("some message".to_string())
1888        .set_file("some_file.cc".to_string())
1889        .set_line(420)
1890        .add_tag("foo")
1891        .add_tag("bar")
1892        .add_key(LogsProperty::String(LogsField::Other("test".to_string()), "property".to_string()))
1893        .add_key(LogsProperty::String(LogsField::MsgStructured, "test".to_string()))
1894        .build();
1895
1896        assert_eq!(
1897            "[00012.345678][moniker][foo,bar] INFO: [some_file.cc(420)] some message test=property value=test",
1898            format!(
1899                "{}",
1900                LogTextPresenter::new(
1901                    &data,
1902                    LogTextDisplayOptions { show_metadata: false, ..Default::default() }
1903                )
1904            )
1905        )
1906    }
1907
1908    #[fuchsia::test]
1909    fn display_for_logs_exclude_tags() {
1910        let data = LogsDataBuilder::new(BuilderArgs {
1911            timestamp: Timestamp::from_nanos(12345678000i64),
1912            component_url: Some(FlyStr::from("fake-url")),
1913            moniker: ExtendedMoniker::parse_str("moniker").unwrap(),
1914            severity: Severity::Info,
1915        })
1916        .set_pid(123)
1917        .set_tid(456)
1918        .set_message("some message".to_string())
1919        .set_file("some_file.cc".to_string())
1920        .set_line(420)
1921        .add_tag("foo")
1922        .add_tag("bar")
1923        .add_key(LogsProperty::String(LogsField::Other("test".to_string()), "property".to_string()))
1924        .add_key(LogsProperty::String(LogsField::MsgStructured, "test".to_string()))
1925        .build();
1926
1927        assert_eq!(
1928            "[00012.345678][123][456][moniker] INFO: [some_file.cc(420)] some message test=property value=test",
1929            format!(
1930                "{}",
1931                LogTextPresenter::new(
1932                    &data,
1933                    LogTextDisplayOptions { show_tags: false, ..Default::default() }
1934                )
1935            )
1936        )
1937    }
1938
1939    #[fuchsia::test]
1940    fn display_for_logs_exclude_file() {
1941        let data = LogsDataBuilder::new(BuilderArgs {
1942            timestamp: Timestamp::from_nanos(12345678000i64),
1943            component_url: Some(FlyStr::from("fake-url")),
1944            moniker: ExtendedMoniker::parse_str("moniker").unwrap(),
1945            severity: Severity::Info,
1946        })
1947        .set_pid(123)
1948        .set_tid(456)
1949        .set_message("some message".to_string())
1950        .set_file("some_file.cc".to_string())
1951        .set_line(420)
1952        .add_tag("foo")
1953        .add_tag("bar")
1954        .add_key(LogsProperty::String(LogsField::Other("test".to_string()), "property".to_string()))
1955        .add_key(LogsProperty::String(LogsField::MsgStructured, "test".to_string()))
1956        .build();
1957
1958        assert_eq!(
1959            "[00012.345678][123][456][moniker][foo,bar] INFO: some message test=property value=test",
1960            format!(
1961                "{}",
1962                LogTextPresenter::new(
1963                    &data,
1964                    LogTextDisplayOptions { show_file: false, ..Default::default() }
1965                )
1966            )
1967        )
1968    }
1969
1970    #[fuchsia::test]
1971    fn display_for_logs_include_color_by_severity() {
1972        let data = LogsDataBuilder::new(BuilderArgs {
1973            timestamp: Timestamp::from_nanos(12345678000i64),
1974            component_url: Some(FlyStr::from("fake-url")),
1975            moniker: ExtendedMoniker::parse_str("moniker").unwrap(),
1976            severity: Severity::Error,
1977        })
1978        .set_pid(123)
1979        .set_tid(456)
1980        .set_message("some message".to_string())
1981        .set_file("some_file.cc".to_string())
1982        .set_line(420)
1983        .add_tag("foo")
1984        .add_tag("bar")
1985        .add_key(LogsProperty::String(LogsField::Other("test".to_string()), "property".to_string()))
1986        .add_key(LogsProperty::String(LogsField::MsgStructured, "test".to_string()))
1987        .build();
1988
1989        assert_eq!(
1990            format!(
1991                "{}[00012.345678][123][456][moniker][foo,bar] ERROR: [some_file.cc(420)] some message test=property value=test{}",
1992                color::Fg(color::Red),
1993                style::Reset
1994            ),
1995            format!(
1996                "{}",
1997                LogTextPresenter::new(
1998                    &data,
1999                    LogTextDisplayOptions { color: LogTextColor::BySeverity, ..Default::default() }
2000                )
2001            )
2002        )
2003    }
2004
2005    #[fuchsia::test]
2006    fn display_for_logs_highlight_line() {
2007        let data = LogsDataBuilder::new(BuilderArgs {
2008            timestamp: Timestamp::from_nanos(12345678000i64),
2009            component_url: Some(FlyStr::from("fake-url")),
2010            moniker: ExtendedMoniker::parse_str("moniker").unwrap(),
2011            severity: Severity::Info,
2012        })
2013        .set_pid(123)
2014        .set_tid(456)
2015        .set_message("some message".to_string())
2016        .set_file("some_file.cc".to_string())
2017        .set_line(420)
2018        .add_tag("foo")
2019        .add_tag("bar")
2020        .add_key(LogsProperty::String(LogsField::Other("test".to_string()), "property".to_string()))
2021        .add_key(LogsProperty::String(LogsField::MsgStructured, "test".to_string()))
2022        .build();
2023
2024        assert_eq!(
2025            format!(
2026                "{}[00012.345678][123][456][moniker][foo,bar] INFO: [some_file.cc(420)] some message test=property value=test{}",
2027                color::Fg(color::LightYellow),
2028                style::Reset
2029            ),
2030            LogTextPresenter::new(
2031                &data,
2032                LogTextDisplayOptions { color: LogTextColor::Highlight, ..Default::default() }
2033            )
2034            .to_string()
2035        )
2036    }
2037
2038    #[fuchsia::test]
2039    fn display_for_logs_with_wall_time() {
2040        let data = LogsDataBuilder::new(BuilderArgs {
2041            timestamp: Timestamp::from_nanos(12345678000i64),
2042            component_url: Some(FlyStr::from("fake-url")),
2043            moniker: ExtendedMoniker::parse_str("moniker").unwrap(),
2044            severity: Severity::Info,
2045        })
2046        .set_pid(123)
2047        .set_tid(456)
2048        .set_message("some message".to_string())
2049        .set_file("some_file.cc".to_string())
2050        .set_line(420)
2051        .add_tag("foo")
2052        .add_tag("bar")
2053        .add_key(LogsProperty::String(LogsField::Other("test".to_string()), "property".to_string()))
2054        .add_key(LogsProperty::String(LogsField::MsgStructured, "test".to_string()))
2055        .build();
2056
2057        assert_eq!(
2058            "[1970-01-01 00:00:12.345][123][456][moniker][foo,bar] INFO: [some_file.cc(420)] some message test=property value=test",
2059            LogTextPresenter::new(
2060                &data,
2061                LogTextDisplayOptions {
2062                    time_format: LogTimeDisplayFormat::WallTime { tz: Timezone::Utc, offset: 1 },
2063                    ..Default::default()
2064                }
2065            )
2066            .to_string()
2067        );
2068
2069        assert_eq!(
2070            "[00012.345678][123][456][moniker][foo,bar] INFO: [some_file.cc(420)] some message test=property value=test",
2071            LogTextPresenter::new(
2072                &data,
2073                LogTextDisplayOptions {
2074                    time_format: LogTimeDisplayFormat::WallTime { tz: Timezone::Utc, offset: 0 },
2075                    ..Default::default()
2076                }
2077            )
2078            .to_string(),
2079            "should fall back to monotonic if offset is 0"
2080        );
2081    }
2082
2083    #[fuchsia::test]
2084    fn display_for_logs_with_dropped_count() {
2085        let data = LogsDataBuilder::new(BuilderArgs {
2086            timestamp: Timestamp::from_nanos(12345678000i64),
2087            component_url: Some(FlyStr::from("fake-url")),
2088            moniker: ExtendedMoniker::parse_str("moniker").unwrap(),
2089            severity: Severity::Info,
2090        })
2091        .set_dropped(5)
2092        .set_pid(123)
2093        .set_tid(456)
2094        .set_message("some message".to_string())
2095        .set_file("some_file.cc".to_string())
2096        .set_line(420)
2097        .add_tag("foo")
2098        .add_tag("bar")
2099        .add_key(LogsProperty::String(LogsField::Other("test".to_string()), "property".to_string()))
2100        .add_key(LogsProperty::String(LogsField::MsgStructured, "test".to_string()))
2101        .build();
2102
2103        assert_eq!(
2104            "[00012.345678][123][456][moniker][foo,bar] INFO: [some_file.cc(420)] some message test=property value=test [dropped=5]",
2105            format!("{}", LogTextPresenter::new(&data, LogTextDisplayOptions::default())),
2106        );
2107
2108        assert_eq!(
2109            format!(
2110                "[00012.345678][123][456][moniker][foo,bar] INFO: [some_file.cc(420)] some message test=property value=test{} [dropped=5]{}",
2111                color::Fg(color::Yellow),
2112                style::Reset
2113            ),
2114            LogTextPresenter::new(
2115                &data,
2116                LogTextDisplayOptions { color: LogTextColor::BySeverity, ..Default::default() }
2117            )
2118            .to_string()
2119        );
2120    }
2121
2122    #[fuchsia::test]
2123    fn display_for_logs_with_rolled_count() {
2124        let data = LogsDataBuilder::new(BuilderArgs {
2125            timestamp: Timestamp::from_nanos(12345678000i64),
2126            component_url: Some(FlyStr::from("fake-url")),
2127            moniker: ExtendedMoniker::parse_str("moniker").unwrap(),
2128            severity: Severity::Info,
2129        })
2130        .set_rolled_out(10)
2131        .set_pid(123)
2132        .set_tid(456)
2133        .set_message("some message".to_string())
2134        .set_file("some_file.cc".to_string())
2135        .set_line(420)
2136        .add_tag("foo")
2137        .add_tag("bar")
2138        .add_key(LogsProperty::String(LogsField::Other("test".to_string()), "property".to_string()))
2139        .add_key(LogsProperty::String(LogsField::MsgStructured, "test".to_string()))
2140        .build();
2141
2142        assert_eq!(
2143            "[00012.345678][123][456][moniker][foo,bar] INFO: [some_file.cc(420)] some message test=property value=test [rolled=10]",
2144            format!("{}", LogTextPresenter::new(&data, LogTextDisplayOptions::default())),
2145        );
2146
2147        assert_eq!(
2148            format!(
2149                "[00012.345678][123][456][moniker][foo,bar] INFO: [some_file.cc(420)] some message test=property value=test{} [rolled=10]{}",
2150                color::Fg(color::Yellow),
2151                style::Reset
2152            ),
2153            LogTextPresenter::new(
2154                &data,
2155                LogTextDisplayOptions { color: LogTextColor::BySeverity, ..Default::default() }
2156            )
2157            .to_string()
2158        );
2159    }
2160
2161    #[fuchsia::test]
2162    fn display_for_logs_with_dropped_and_rolled_counts() {
2163        let data = LogsDataBuilder::new(BuilderArgs {
2164            timestamp: Timestamp::from_nanos(12345678000i64),
2165            component_url: Some(FlyStr::from("fake-url")),
2166            moniker: ExtendedMoniker::parse_str("moniker").unwrap(),
2167            severity: Severity::Info,
2168        })
2169        .set_dropped(5)
2170        .set_rolled_out(10)
2171        .set_pid(123)
2172        .set_tid(456)
2173        .set_message("some message".to_string())
2174        .set_file("some_file.cc".to_string())
2175        .set_line(420)
2176        .add_tag("foo")
2177        .add_tag("bar")
2178        .add_key(LogsProperty::String(LogsField::Other("test".to_string()), "property".to_string()))
2179        .add_key(LogsProperty::String(LogsField::MsgStructured, "test".to_string()))
2180        .build();
2181
2182        assert_eq!(
2183            "[00012.345678][123][456][moniker][foo,bar] INFO: [some_file.cc(420)] some message test=property value=test [dropped=5] [rolled=10]",
2184            format!("{}", LogTextPresenter::new(&data, LogTextDisplayOptions::default())),
2185        );
2186
2187        assert_eq!(
2188            format!(
2189                "[00012.345678][123][456][moniker][foo,bar] INFO: [some_file.cc(420)] some message test=property value=test{} [dropped=5] [rolled=10]{}",
2190                color::Fg(color::Yellow),
2191                style::Reset
2192            ),
2193            LogTextPresenter::new(
2194                &data,
2195                LogTextDisplayOptions { color: LogTextColor::BySeverity, ..Default::default() }
2196            )
2197            .to_string()
2198        );
2199    }
2200
2201    #[fuchsia::test]
2202    fn display_for_logs_no_tags() {
2203        let data = LogsDataBuilder::new(BuilderArgs {
2204            timestamp: Timestamp::from_nanos(12345678000i64),
2205            component_url: Some(FlyStr::from("fake-url")),
2206            moniker: ExtendedMoniker::parse_str("moniker").unwrap(),
2207            severity: Severity::Info,
2208        })
2209        .set_pid(123)
2210        .set_tid(456)
2211        .set_message("some message".to_string())
2212        .build();
2213
2214        assert_eq!("[00012.345678][123][456][moniker] INFO: some message", format!("{data}"))
2215    }
2216
2217    #[fuchsia::test]
2218    fn size_bytes_deserialize_backwards_compatibility() {
2219        let original_json = json!({
2220          "moniker": "a/b",
2221          "version": 1,
2222          "data_source": "Logs",
2223          "payload": {
2224            "root": {
2225              "message":{}
2226            }
2227          },
2228          "metadata": {
2229            "component_url": "url",
2230              "severity": "INFO",
2231              "tags": [],
2232
2233            "timestamp": 123,
2234          }
2235        });
2236        let expected_data = LogsDataBuilder::new(BuilderArgs {
2237            component_url: Some("url".into()),
2238            moniker: ExtendedMoniker::parse_str("a/b").unwrap(),
2239            severity: Severity::Info,
2240            timestamp: Timestamp::from_nanos(123),
2241        })
2242        .build();
2243        let original_data: LogsData = serde_json::from_value(original_json).unwrap();
2244        assert_eq!(original_data, expected_data);
2245        // We skip deserializing the size_bytes
2246        assert_eq!(original_data.metadata.size_bytes, None);
2247    }
2248
2249    #[fuchsia::test]
2250    fn dropped_deserialize_backwards_compatibility() {
2251        let original_json = json!({
2252          "moniker": "a/b",
2253          "version": 1,
2254          "data_source": "Logs",
2255          "payload": {
2256            "root": {
2257              "message":{}
2258            }
2259          },
2260          "metadata": {
2261            "dropped": 0,
2262            "component_url": "url",
2263              "severity": "INFO",
2264              "tags": [],
2265
2266            "timestamp": 123,
2267          }
2268        });
2269        let expected_data = LogsDataBuilder::new(BuilderArgs {
2270            component_url: Some("url".into()),
2271            moniker: ExtendedMoniker::parse_str("a/b").unwrap(),
2272            severity: Severity::Info,
2273            timestamp: Timestamp::from_nanos(123),
2274        })
2275        .build();
2276        let original_data: LogsData = serde_json::from_value(original_json).unwrap();
2277        assert_eq!(original_data, expected_data);
2278        // We skip deserializing dropped
2279        assert_eq!(original_data.metadata.dropped, None);
2280    }
2281
2282    #[fuchsia::test]
2283    fn severity_aliases() {
2284        assert_eq!(Severity::from_str("warn").unwrap(), Severity::Warn);
2285        assert_eq!(Severity::from_str("warning").unwrap(), Severity::Warn);
2286    }
2287
2288    #[fuchsia::test]
2289    fn test_metadata_merge() {
2290        let mut meta = InspectMetadata {
2291            errors: Some(vec![InspectError { message: "error1".to_string() }]),
2292            name: InspectHandleName::name("test"),
2293            component_url: "fuchsia-pkg://test".into(),
2294            timestamp: Timestamp::from_nanos(100),
2295            escrowed: false,
2296        };
2297
2298        meta.merge(InspectMetadata {
2299            errors: Some(vec![InspectError { message: "error2".to_string() }]),
2300            name: InspectHandleName::name("test"),
2301            component_url: "fuchsia-pkg://test".into(),
2302            timestamp: Timestamp::from_nanos(200),
2303            escrowed: false,
2304        });
2305
2306        assert_eq!(
2307            meta,
2308            InspectMetadata {
2309                errors: Some(vec![
2310                    InspectError { message: "error1".to_string() },
2311                    InspectError { message: "error2".to_string() },
2312                ]),
2313                name: InspectHandleName::name("test"),
2314                component_url: "fuchsia-pkg://test".into(),
2315                timestamp: Timestamp::from_nanos(200),
2316                escrowed: false,
2317            }
2318        );
2319    }
2320
2321    #[fuchsia::test]
2322    fn test_metadata_merge_older_timestamp_noop() {
2323        let mut meta = InspectMetadata {
2324            errors: None,
2325            name: InspectHandleName::name("test"),
2326            component_url: TEST_URL.into(),
2327            timestamp: Timestamp::from_nanos(200),
2328            escrowed: false,
2329        };
2330        meta.merge(InspectMetadata {
2331            errors: None,
2332            name: InspectHandleName::name("test"),
2333            component_url: TEST_URL.into(),
2334            timestamp: Timestamp::from_nanos(100),
2335            escrowed: false,
2336        });
2337        assert_eq!(
2338            meta,
2339            InspectMetadata {
2340                errors: None,
2341                name: InspectHandleName::name("test"),
2342                component_url: TEST_URL.into(),
2343                timestamp: Timestamp::from_nanos(200),
2344                escrowed: false,
2345            }
2346        );
2347    }
2348
2349    fn new_test_data(moniker: &str, payload_val: Option<&str>, timestamp: i64) -> InspectData {
2350        let mut builder = InspectDataBuilder::new(
2351            moniker.try_into().unwrap(),
2352            TEST_URL,
2353            Timestamp::from_nanos(timestamp),
2354        );
2355        if let Some(val) = payload_val {
2356            builder = builder.with_hierarchy(hierarchy! { root: { "key": val } });
2357        }
2358        builder.build()
2359    }
2360
2361    #[fuchsia::test]
2362    fn test_data_merge() {
2363        let mut data = new_test_data("a/b/c", Some("val1"), 100);
2364        let mut other = new_test_data("a/b/c", Some("val2"), 200);
2365        other.metadata.errors = Some(vec![InspectError { message: "error".into() }]);
2366
2367        data.merge(other);
2368
2369        let expected_payload = hierarchy! { root: { "key": "val2" } };
2370        assert_eq!(data.payload, Some(expected_payload));
2371        assert_eq!(data.metadata.timestamp, Timestamp::from_nanos(200));
2372        assert_eq!(data.metadata.errors, Some(vec![InspectError { message: "error".into() }]));
2373    }
2374
2375    #[test_case(new_test_data("a/b/d", Some("v2"), 100); "different moniker")]
2376    #[test_case(
2377        {
2378            let mut d = new_test_data("a/b/c", Some("v2"), 100);
2379            d.version = 2;
2380            d
2381        }; "different version")]
2382    #[test_case(
2383        {
2384            let mut d = new_test_data("a/b/c", Some("v2"), 100);
2385            d.data_source = DataSource::Logs;
2386            d
2387        }; "different data source")]
2388    #[fuchsia::test]
2389    fn test_data_merge_noop(other: InspectData) {
2390        let mut data = new_test_data("a/b/c", Some("v1"), 100);
2391        let original = data.clone();
2392        data.merge(other);
2393        assert_eq!(data, original);
2394    }
2395
2396    #[test_case(None, Some("val2"), Some("val2") ; "none_with_some")]
2397    #[test_case(Some("val1"), None, Some("val1") ; "some_with_none")]
2398    #[test_case(Some("val1"), Some("val2"), Some("val2") ; "some_with_some")]
2399    #[fuchsia::test]
2400    fn test_data_merge_payloads(
2401        payload: Option<&str>,
2402        other_payload: Option<&str>,
2403        expected: Option<&str>,
2404    ) {
2405        let mut data = new_test_data("a/b/c", payload, 100);
2406        let other = new_test_data("a/b/c", other_payload, 100);
2407
2408        data.merge(other);
2409        assert_eq!(data, new_test_data("a/b/c", expected, 100));
2410    }
2411}