virtual_console_lib/
log.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
// 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::terminal::Terminal;
use anyhow::Error;
use fuchsia_async::{self as fasync, OnSignals};
use std::io::sink;
use term_model::ansi::Processor;
use term_model::event::EventListener;
use zx::{self as zx, AsHandleRef};

pub trait LogClient: 'static + Clone {
    type Listener;

    fn create_terminal(&self, id: u32, title: String) -> Result<Terminal<Self::Listener>, Error>;
    fn request_update(&self, id: u32);
}

pub struct Log;

impl Log {
    pub fn start<T: LogClient>(
        read_only_debuglog: zx::DebugLog,
        client: &T,
        id: u32,
    ) -> Result<(), Error>
    where
        <T as LogClient>::Listener: EventListener,
    {
        let client = client.clone();
        let terminal =
            client.create_terminal(id, "debuglog".to_string()).expect("failed to create terminal");
        let term = terminal.clone_term();

        // Get our process koid so we can filter out our own debug messages from the log.
        let proc_koid =
            fuchsia_runtime::process_self().get_koid().expect("failed to get koid for process");

        fasync::Task::local(async move {
            let mut sink = sink();
            let mut parser = Processor::new();

            loop {
                let on_signal = OnSignals::new(&read_only_debuglog, zx::Signals::LOG_READABLE);
                on_signal.await.expect("failed to wait for log readable");

                loop {
                    match read_only_debuglog.read() {
                        Ok(record) => {
                            // Don't print log messages from ourself.
                            if record.pid == proc_koid {
                                continue;
                            }

                            let mut term = term.borrow_mut();

                            // Write prefix with time stamps and ids.
                            let prefix = format!(
                                "\u{001b}[32m{:05}.{:03}\u{001b}[39m] \u{001b}[31m{:05}.\u{001b}[36m{:05}\u{001b}[39m> ",
                                record.timestamp.into_nanos() / 1_000_000_000,
                                (record.timestamp.into_nanos() / 1_000_000) % 1_000,
                                record.pid.raw_koid(),
                                record.tid.raw_koid(),
                            );
                            for byte in prefix.as_bytes() {
                                parser.advance(&mut *term, *byte, &mut sink);
                            }

                            // Ignore any trailing newline character.
                            let mut record_data = record.data();
                            if record_data.last() == Some(&b'\n') {
                                record_data = &record_data[..record_data.len() - 1];
                            }

                            // Write record data.
                            for byte in record_data.iter() {
                                parser.advance(&mut *term, *byte, &mut sink);
                            }

                            // Write carriage return and newline.
                            for byte in "\r\n".as_bytes() {
                                parser.advance(&mut *term, *byte, &mut sink);
                            }

                            // Request terminal update.
                            client.request_update(id);
                        }
                        Err(status) if status == zx::Status::SHOULD_WAIT => {
                            break;
                        }
                        Err(_) => {
                            let mut term = term.borrow_mut();
                            for byte in "\r\n<<LOG ERROR>>".as_bytes() {
                                parser.advance(&mut *term, *byte, &mut sink);
                            }

                            // Request terminal update.
                            client.request_update(id);
                            break;
                        }
                    }
                }
            }
        })
        .detach();

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::colors::ColorScheme;
    use fuchsia_async as fasync;
    use term_model::event::Event;

    #[derive(Default)]
    struct TestListener;

    impl EventListener for TestListener {
        fn send_event(&self, _event: Event) {}
    }

    #[derive(Default, Clone)]
    struct TestLogClient;

    impl LogClient for TestLogClient {
        type Listener = TestListener;

        fn create_terminal(
            &self,
            _id: u32,
            title: String,
        ) -> Result<Terminal<Self::Listener>, Error> {
            Ok(Terminal::new(TestListener::default(), title, ColorScheme::default(), 1024, None))
        }
        fn request_update(&self, _id: u32) {}
    }

    #[fasync::run_singlethreaded(test)]
    async fn can_start_log() -> Result<(), Error> {
        let resource = zx::Resource::from(zx::Handle::invalid());
        let debuglog = zx::DebugLog::create(&resource, zx::DebugLogOpts::empty()).unwrap();
        let client = TestLogClient::default();
        let _ = Log::start(debuglog, &client, 0)?;
        Ok(())
    }
}