diagnostics_data/
logs_legacy.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::{Data, Logs};
use fidl_fuchsia_logger::LogMessage;

use std::fmt::Write;

/// Convert this `Message` to a FIDL representation suitable for sending to `LogListenerSafe`.
impl From<&Data<Logs>> for LogMessage {
    fn from(data: &Data<Logs>) -> LogMessage {
        let mut msg = data.msg().unwrap_or("").to_string();

        if let Some(payload) = data.payload_keys() {
            for property in payload.properties.iter() {
                write!(&mut msg, " {property}").expect("allocations have to fail for this to fail");
            }
        }
        let file = data.metadata.file.as_ref();
        let line = data.metadata.line.as_ref();
        if let (Some(file), Some(line)) = (file, line) {
            msg = format!("[{}({})] {}", file, line, msg);
        }

        let tags = match &data.metadata.tags {
            None => vec![data.component_name().to_string()],
            Some(tags) if tags.is_empty() => vec![data.component_name().to_string()],
            Some(tags) => tags.clone(),
        };

        LogMessage {
            pid: data.pid().unwrap_or(zx::sys::ZX_KOID_INVALID),
            tid: data.tid().unwrap_or(zx::sys::ZX_KOID_INVALID),
            time: data.metadata.timestamp,
            severity: data.metadata.raw_severity() as i32,
            dropped_logs: data.dropped_logs().unwrap_or(0) as _,
            tags,
            msg,
        }
    }
}

/// Convert this `Message` to a FIDL representation suitable for sending to `LogListenerSafe`.
impl From<Data<Logs>> for LogMessage {
    fn from(data: Data<Logs>) -> LogMessage {
        let mut msg = data.msg().unwrap_or("").to_string();

        if let Some(payload) = data.payload_keys() {
            for property in payload.properties.iter() {
                write!(&mut msg, " {property}").expect("allocations have to fail for this to fail");
            }
        }
        let file = data.metadata.file.as_ref();
        let line = data.metadata.line.as_ref();
        if let (Some(file), Some(line)) = (file, line) {
            msg = format!("[{}({})] {}", file, line, msg);
        }

        LogMessage {
            pid: data.pid().unwrap_or(zx::sys::ZX_KOID_INVALID),
            tid: data.tid().unwrap_or(zx::sys::ZX_KOID_INVALID),
            time: data.metadata.timestamp,
            severity: data.metadata.raw_severity() as i32,
            dropped_logs: data.dropped_logs().unwrap_or(0) as _,
            tags: match data.metadata.tags {
                Some(tags) => tags,
                None => vec![data.component_name().to_string()],
            },
            msg,
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::{BuilderArgs, LogsDataBuilder, Severity, Timestamp};
    use fidl_fuchsia_diagnostics as fdiagnostics;

    const TEST_URL: &str = "fuchsia-pkg://test";
    const TEST_MONIKER: &str = "fake-test/moniker";

    macro_rules! severity_roundtrip_test {
        ($raw:expr, $expected:expr) => {
            let severity = Severity::from(u8::from($raw));
            let msg = LogsDataBuilder::new(BuilderArgs {
                timestamp: Timestamp::from_nanos(0),
                component_url: Some(TEST_URL.into()),
                moniker: moniker::ExtendedMoniker::parse_str(TEST_MONIKER).unwrap(),
                severity,
            })
            .build();

            let legacy_msg: LogMessage = (&msg).into();
            assert_eq!(
                legacy_msg.severity,
                i32::from($expected),
                "failed to round trip severity for {:?} (raw {}), intermediates: {:#?}\n{:#?}",
                severity,
                $raw,
                msg,
                legacy_msg
            );
        };
    }

    #[fuchsia::test]
    fn raw_severity_roundtrip_trace() {
        severity_roundtrip_test!(
            fdiagnostics::Severity::Trace.into_primitive() - 1,
            fdiagnostics::Severity::Trace.into_primitive()
        );
        severity_roundtrip_test!(
            fdiagnostics::Severity::Trace.into_primitive(),
            fdiagnostics::Severity::Trace.into_primitive()
        );
        severity_roundtrip_test!(
            fdiagnostics::Severity::Trace.into_primitive() + 1,
            fdiagnostics::Severity::Debug.into_primitive()
        );
    }

    #[fuchsia::test]
    fn severity_roundtrip_debug() {
        severity_roundtrip_test!(
            fdiagnostics::Severity::Debug.into_primitive() - 1,
            fdiagnostics::Severity::Debug.into_primitive()
        );
        severity_roundtrip_test!(
            fdiagnostics::Severity::Debug.into_primitive(),
            fdiagnostics::Severity::Debug.into_primitive()
        );
        severity_roundtrip_test!(
            fdiagnostics::Severity::Debug.into_primitive() + 1,
            fdiagnostics::Severity::Info.into_primitive()
        );
    }

    #[fuchsia::test]
    fn severity_roundtrip_info() {
        severity_roundtrip_test!(
            fdiagnostics::Severity::Info.into_primitive() - 1,
            fdiagnostics::Severity::Info.into_primitive()
        );
        severity_roundtrip_test!(
            fdiagnostics::Severity::Info.into_primitive(),
            fdiagnostics::Severity::Info.into_primitive()
        );
        severity_roundtrip_test!(
            fdiagnostics::Severity::Info.into_primitive() + 1,
            fdiagnostics::Severity::Warn.into_primitive()
        );
    }

    #[fuchsia::test]
    fn severity_roundtrip_warn() {
        severity_roundtrip_test!(
            fdiagnostics::Severity::Warn.into_primitive() - 1,
            fdiagnostics::Severity::Warn.into_primitive()
        );
        severity_roundtrip_test!(
            fdiagnostics::Severity::Warn.into_primitive(),
            fdiagnostics::Severity::Warn.into_primitive()
        );
        severity_roundtrip_test!(
            fdiagnostics::Severity::Warn.into_primitive() + 1,
            fdiagnostics::Severity::Error.into_primitive()
        );
    }

    #[fuchsia::test]
    fn severity_roundtrip_error() {
        severity_roundtrip_test!(
            fdiagnostics::Severity::Error.into_primitive() - 1,
            fdiagnostics::Severity::Error.into_primitive()
        );
        severity_roundtrip_test!(
            fdiagnostics::Severity::Error.into_primitive(),
            fdiagnostics::Severity::Error.into_primitive()
        );
        severity_roundtrip_test!(
            fdiagnostics::Severity::Error.into_primitive() + 1,
            fdiagnostics::Severity::Fatal.into_primitive()
        );
    }

    #[fuchsia::test]
    fn severity_roundtrip_fatal() {
        severity_roundtrip_test!(
            fdiagnostics::Severity::Fatal.into_primitive() - 1,
            fdiagnostics::Severity::Fatal.into_primitive()
        );
        severity_roundtrip_test!(
            fdiagnostics::Severity::Fatal.into_primitive(),
            fdiagnostics::Severity::Fatal.into_primitive()
        );
        severity_roundtrip_test!(
            fdiagnostics::Severity::Fatal.into_primitive() + 1,
            fdiagnostics::Severity::Fatal.into_primitive()
        );
    }
}