Skip to main content

starnix_core/fs/fuchsia/
syslog.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::task::CurrentTask;
6use crate::vfs::buffers::{InputBuffer, OutputBuffer};
7use crate::vfs::{
8    Anon, FileHandle, FileObject, FileOps, fileops_impl_nonseekable, fileops_impl_noop_sync,
9};
10use starnix_logging::log_info;
11use starnix_uapi::errors::Errno;
12use starnix_uapi::open_flags::OpenFlags;
13
14pub struct SyslogFile;
15
16impl SyslogFile {
17    pub fn new_file(current_task: &CurrentTask) -> FileHandle {
18        // TODO: https://fxbug.dev/404739824 - Use a non-private node once labeling of external resources is addressed.
19        Anon::new_private_file(
20            current_task,
21            Box::new(SyslogFile),
22            OpenFlags::RDWR,
23            "[fuchsia:syslog]",
24        )
25    }
26}
27
28impl FileOps for SyslogFile {
29    fileops_impl_nonseekable!();
30    fileops_impl_noop_sync!();
31
32    fn write(
33        &self,
34        _file: &FileObject,
35        _current_task: &CurrentTask,
36        offset: usize,
37        data: &mut dyn InputBuffer,
38    ) -> Result<usize, Errno> {
39        debug_assert!(offset == 0);
40        data.read_each(&mut |bytes| {
41            log_info!(tag = "stdio"; "{}", String::from_utf8_lossy(bytes));
42            Ok(bytes.len())
43        })
44    }
45
46    fn read(
47        &self,
48        _file: &FileObject,
49        _current_task: &CurrentTask,
50        offset: usize,
51        _data: &mut dyn OutputBuffer,
52    ) -> Result<usize, Errno> {
53        debug_assert!(offset == 0);
54        Ok(0)
55    }
56}