dlog_redirect_stdout/lib.rs
1use dlog::{BufferId, Priority};
2use dlog_redirect_stdout_sys::*;
3use std::ffi::CStr;
4use std::io::{Error, ErrorKind, Result};
5
6/// Open an unstructured connection to a DLog logging sink.
7///
8/// Opens a connection with stdout to which you can stream data.
9/// DLog will automatically convert it to individual logs appropriately.
10///
11/// # Arguments
12///
13/// * `buffer` - Target DLog buffer.
14/// * `tag` - DLog tag for the resulting logs.
15/// * `prio` - Priority for the resulting logs.
16///
17/// # Errors
18///
19/// * [`std::io::ErrorKind::InvalidInput`] - Buffer does not allow connections.
20/// * [`std::io::ErrorKind::PermissionDenied`] - Buffer disabled via dlog config.
21/// * [`std::io::ErrorKind::Uncategorized`] - Buffer not configured via dlog config.
22/// * [`std::io::ErrorKind::Uncategorized`] - Unsupported DLog backend.
23/// * [`std::io::ErrorKind::InvalidInput`] - Tag empty.
24///
25/// # Examples
26///
27/// ```
28/// use dlog::{BufferId, Priority};
29/// use dlog_redirect_stdout::redirect_stdout;
30///
31/// fn main() -> Result<(), std::io::Error> {
32/// redirect_stdout(BufferId::LogIdMain, "foo", Priority::Info)?;
33/// // The following sends an info log with tag "foo" and message "my_message".
34/// println!("my_message");
35/// Ok(())
36/// }
37/// ```
38pub fn redirect_stdout(buffer: BufferId, tag: &str, prio: Priority) -> Result<()> {
39 let tag = format!("{}\0", tag);
40 let tag_c = CStr::from_bytes_with_nul(tag.as_bytes()).map_err(|_| ErrorKind::InvalidInput)?;
41 // SAFETY: the string is defined in a verified way above. All other parameters
42 // are checked for correctness on the C side.
43 match unsafe { dlog_connect_fd(buffer as i32, 1, tag_c.as_ptr(), prio as i32) } {
44 0 => Ok(()),
45 e => Err(Error::from_raw_os_error(-e)),
46 }
47}
48
49/// Open an unstructured connection to a DLog logging sink.
50///
51/// Opens a connection with stderr to which you can stream data.
52/// DLog will automatically convert it to individual logs appropriately.
53///
54/// # Arguments
55///
56/// * `buffer` - Target DLog buffer.
57/// * `tag` - DLog tag for the resulting logs.
58/// * `prio` - Priority for the resulting logs.
59///
60/// # Errors
61///
62/// * [`std::io::ErrorKind::InvalidInput`] - Buffer does not allow connections.
63/// * [`std::io::ErrorKind::PermissionDenied`] - Buffer disabled via dlog config.
64/// * [`std::io::ErrorKind::Uncategorized`] - Buffer not configured via dlog config.
65/// * [`std::io::ErrorKind::Uncategorized`] - Unsupported DLog backend.
66/// * [`std::io::ErrorKind::InvalidInput`] - Tag empty.
67///
68/// # Examples
69///
70/// ```
71/// use dlog::{BufferId, Priority};
72/// use dlog_redirect_stdout::redirect_stderr;
73///
74/// fn main() -> Result<(), std::io::Error> {
75/// redirect_stderr(BufferId::LogIdMain, "foo", Priority::Info)?;
76/// // The following sends an info log with tag "foo" and message "my_message".
77/// eprintln!("my_message");
78/// Ok(())
79/// }
80/// ```
81pub fn redirect_stderr(buffer: BufferId, tag: &str, prio: Priority) -> Result<()> {
82 let tag = format!("{}\0", tag);
83 let tag_c = CStr::from_bytes_with_nul(tag.as_bytes()).map_err(|_| ErrorKind::InvalidInput)?;
84 // SAFETY: the string is defined in a verified way above. All other parameters
85 // are checked for correctness on the C side.
86 match unsafe { dlog_connect_fd(buffer as i32, 2, tag_c.as_ptr(), prio as i32) } {
87 0 => Ok(()),
88 e => Err(Error::from_raw_os_error(-e)),
89 }
90}
91
92/// Check whether a file descriptor has been opened via DLog.
93///
94/// Use for low-level code that does bulk operations on FDs (for example, close
95/// them all) but still wants to be able to use DLog interfaces afterwards.
96///
97/// # Returns
98///
99/// * `bool` - Whether the descriptor refers to a DLog file.
100pub fn is_log_fd(fd: i32) -> bool {
101 // SAFETY: the C function is safe by itself.
102 unsafe { dlog_is_log_fd(fd) }
103}