archivist_lib/logs/
stored_message.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// 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::identity::ComponentIdentity;
use crate::logs::stats::LogStreamStats;
use anyhow::Result;
use bstr::{BStr, ByteSlice};
use diagnostics_data::{LogsData, Severity};
use diagnostics_log_encoding::encode::{
    Encoder, EncoderOpts, EncodingError, MutableBuffer, RecordEvent, WriteEventParams,
};
use diagnostics_log_encoding::Argument;
use diagnostics_message::LoggerMessage;
use fidl_fuchsia_diagnostics as fdiagnostics;
use fidl_fuchsia_logger::MAX_DATAGRAM_LEN_BYTES;
use std::fmt::Debug;
use std::io::Cursor;
use std::sync::Arc;

type RawSeverity = u8;

#[derive(Debug, Eq, PartialEq)]
pub struct StoredMessage {
    bytes: Box<[u8]>,
    severity: Severity,
    timestamp: zx::BootInstant,
}

impl StoredMessage {
    pub fn new(buf: Box<[u8]>, stats: &Arc<LogStreamStats>) -> Option<Self> {
        match diagnostics_log_encoding::parse::basic_info(&buf) {
            Ok((timestamp, severity)) => {
                Some(StoredMessage { bytes: buf, severity: severity.into(), timestamp })
            }
            _ => {
                stats.increment_invalid(buf.len());
                None
            }
        }
    }

    pub fn from_legacy(buf: Box<[u8]>, stats: &Arc<LogStreamStats>) -> Option<Self> {
        let Ok(LoggerMessage {
            timestamp,
            raw_severity,
            message,
            pid,
            tid,
            dropped_logs,
            tags,
            size_bytes: _,
        }) = LoggerMessage::try_from(buf.as_ref())
        else {
            stats.increment_invalid(buf.len());
            return None;
        };
        let mut encoder =
            Encoder::new(Cursor::new([0u8; MAX_DATAGRAM_LEN_BYTES as _]), EncoderOpts::default());
        let _ = encoder.write_event(WriteEventParams {
            event: LegacyMessageRecord { severity: raw_severity, data: &message, timestamp },
            tags: &tags,
            metatags: std::iter::empty(),
            pid: zx::Koid::from_raw(pid),
            tid: zx::Koid::from_raw(tid),
            dropped: dropped_logs,
        });
        let cursor = encoder.take();
        let position = cursor.position() as usize;
        let buf = cursor.get_ref();
        Some(Self {
            timestamp,
            severity: Severity::from(raw_severity),
            bytes: Box::from(&buf[..position]),
        })
    }

    pub fn from_debuglog(record: zx::DebugLogRecord, dropped: u64) -> Self {
        let mut data = record.data();
        if let Some(b'\n') = data.last() {
            data = &data[..data.len() - 1];
        }

        let severity = match record.severity {
            zx::DebugLogSeverity::Trace => fdiagnostics::Severity::Trace,
            zx::DebugLogSeverity::Debug => fdiagnostics::Severity::Debug,
            zx::DebugLogSeverity::Warn => fdiagnostics::Severity::Warn,
            zx::DebugLogSeverity::Error => fdiagnostics::Severity::Error,
            zx::DebugLogSeverity::Fatal => fdiagnostics::Severity::Fatal,
            zx::DebugLogSeverity::Unknown => fdiagnostics::Severity::Info,
            zx::DebugLogSeverity::Info => {
                // By default `zx_log_record_t` carries INFO severity. Since `zx_debuglog_write`
                // doesn't support setting a severity, historically logs have been tagged and
                // annotated with their severity in the message. If we get here attempt to use the
                // severity in the message, otherwise fallback to INFO.
                const MAX_STRING_SEARCH_SIZE: usize = 170;
                let last = data
                    .char_indices()
                    .nth(MAX_STRING_SEARCH_SIZE)
                    .map(|(i, _, _)| i)
                    .unwrap_or(data.len());
                let early_contents = &data[..last];
                if early_contents.contains_str("ERROR:") {
                    fdiagnostics::Severity::Error
                } else if early_contents.contains_str("WARNING:") {
                    fdiagnostics::Severity::Warn
                } else {
                    fdiagnostics::Severity::Info
                }
            }
        };

        let mut encoder =
            Encoder::new(Cursor::new([0u8; MAX_DATAGRAM_LEN_BYTES as _]), EncoderOpts::default());
        let _ = encoder.write_event(WriteEventParams {
            event: DebugLogRecordEvent {
                severity: severity.into_primitive(),
                data,
                timestamp: record.timestamp,
            },
            tags: &["klog"],
            metatags: std::iter::empty(),
            pid: record.pid,
            tid: record.tid,
            dropped,
        });
        let cursor = encoder.take();
        let position = cursor.position() as usize;
        let buf = cursor.get_ref();

        Self {
            bytes: Box::from(&buf[..position]),
            severity: severity.into(),
            timestamp: record.timestamp,
        }
    }

    pub fn bytes(&self) -> &[u8] {
        &self.bytes
    }

    pub fn size(&self) -> usize {
        self.bytes.len()
    }

    pub fn severity(&self) -> Severity {
        self.severity
    }

    pub fn timestamp(&self) -> zx::BootInstant {
        self.timestamp
    }

    pub fn parse(&self, source: &ComponentIdentity) -> Result<LogsData> {
        let mut data = diagnostics_message::from_structured(source.into(), &self.bytes)?;
        // TODO(https://fxbug.dev/368426475): fix chromium, then remove. The problematic logs are
        // being ingested as sturctured logs. Not a legacy logs too.
        match i8::from_le_bytes(data.metadata.raw_severity().to_le_bytes()) {
            -1 => data.set_severity(Severity::Debug),
            -2 => data.set_severity(Severity::Trace),
            0 => data.set_severity(Severity::Info),
            _ => {}
        }
        Ok(data)
    }
}

struct LegacyMessageRecord<'a> {
    severity: RawSeverity,
    data: &'a str,
    timestamp: zx::BootInstant,
}

impl RecordEvent for LegacyMessageRecord<'_> {
    fn raw_severity(&self) -> RawSeverity {
        self.severity
    }

    fn file(&self) -> Option<&str> {
        None
    }

    fn line(&self) -> Option<u32> {
        None
    }

    fn target(&self) -> &str {
        ""
    }

    fn timestamp(&self) -> zx::BootInstant {
        self.timestamp
    }

    fn write_arguments<B: MutableBuffer>(
        self,
        writer: &mut Encoder<B>,
    ) -> Result<(), EncodingError> {
        writer.write_argument(Argument::message(self.data))?;
        Ok(())
    }
}

struct DebugLogRecordEvent<'a> {
    severity: RawSeverity,
    data: &'a BStr,
    timestamp: zx::BootInstant,
}

impl RecordEvent for DebugLogRecordEvent<'_> {
    fn raw_severity(&self) -> RawSeverity {
        self.severity
    }

    fn file(&self) -> Option<&str> {
        None
    }

    fn line(&self) -> Option<u32> {
        None
    }

    fn target(&self) -> &str {
        ""
    }

    fn timestamp(&self) -> zx::BootInstant {
        self.timestamp
    }

    fn write_arguments<B: MutableBuffer>(
        self,
        writer: &mut Encoder<B>,
    ) -> Result<(), EncodingError> {
        writer.write_argument(Argument::message(self.data.to_str_lossy().as_ref()))?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::logs::debuglog::KERNEL_IDENTITY;
    use crate::logs::testing::TestDebugEntry;
    use diagnostics_data::{BuilderArgs, LogsDataBuilder};
    use fidl_fuchsia_logger::LogMessage;

    #[fuchsia::test]
    fn convert_debuglog_to_log_message_test() {
        let klog = TestDebugEntry::new("test log".as_bytes());
        let data = StoredMessage::from_debuglog(klog.record, 10).parse(&KERNEL_IDENTITY).unwrap();
        assert_eq!(
            data,
            LogsDataBuilder::new(BuilderArgs {
                timestamp: klog.record.timestamp,
                component_url: Some(KERNEL_IDENTITY.url.clone()),
                moniker: KERNEL_IDENTITY.moniker.clone(),
                severity: Severity::Info,
            })
            .set_dropped(10)
            .set_pid(klog.record.pid.raw_koid())
            .set_tid(klog.record.tid.raw_koid())
            .add_tag("klog")
            .set_message("test log".to_string())
            .build()
        );
        // make sure the `klog` tag still shows up for legacy listeners
        let log_message: LogMessage = data.into();
        assert_eq!(
            log_message,
            LogMessage {
                pid: klog.record.pid.raw_koid(),
                tid: klog.record.tid.raw_koid(),
                time: klog.record.timestamp,
                severity: fdiagnostics::Severity::Info.into_primitive() as i32,
                dropped_logs: 10,
                tags: vec!["klog".to_string()],
                msg: "test log".to_string(),
            }
        );

        // maximum allowed klog size
        let klog = TestDebugEntry::new(&vec![b'a'; zx::sys::ZX_LOG_RECORD_DATA_MAX]);
        let data = StoredMessage::from_debuglog(klog.record, 0).parse(&KERNEL_IDENTITY).unwrap();
        assert_eq!(
            data,
            LogsDataBuilder::new(BuilderArgs {
                timestamp: klog.record.timestamp,
                component_url: Some(KERNEL_IDENTITY.url.clone()),
                moniker: KERNEL_IDENTITY.moniker.clone(),
                severity: Severity::Info,
            })
            .set_pid(klog.record.pid.raw_koid())
            .set_tid(klog.record.tid.raw_koid())
            .add_tag("klog")
            .set_message(String::from_utf8(vec![b'a'; zx::sys::ZX_LOG_RECORD_DATA_MAX]).unwrap())
            .build()
        );

        // empty message
        let klog = TestDebugEntry::new(&[]);
        let data = StoredMessage::from_debuglog(klog.record, 0).parse(&KERNEL_IDENTITY).unwrap();
        assert_eq!(
            data,
            LogsDataBuilder::new(BuilderArgs {
                timestamp: klog.record.timestamp,
                component_url: Some(KERNEL_IDENTITY.url.clone()),
                moniker: KERNEL_IDENTITY.moniker.clone(),
                severity: Severity::Info,
            })
            .set_pid(klog.record.pid.raw_koid())
            .set_tid(klog.record.tid.raw_koid())
            .add_tag("klog")
            .set_message("".to_string())
            .build()
        );

        // invalid utf-8
        let klog = TestDebugEntry::new(b"\x00\x9f\x92");
        let data = StoredMessage::from_debuglog(klog.record, 0).parse(&KERNEL_IDENTITY).unwrap();
        assert_eq!(data.msg().unwrap(), "\x00��");
    }
}