libcamera/
logging.rs

1use std::{
2    ffi::{CStr, CString},
3    io,
4};
5
6use libcamera_sys::*;
7
8use crate::utils::handle_result;
9
10/// Log destination type.
11#[derive(Copy, Clone, Debug)]
12pub enum LoggingTarget {
13    None,
14    Syslog,
15}
16
17impl From<LoggingTarget> for libcamera_logging_target_t {
18    fn from(value: LoggingTarget) -> Self {
19        match value {
20            LoggingTarget::None => libcamera_logging_target::LIBCAMERA_LOGGING_TARGET_NONE,
21            LoggingTarget::Syslog => libcamera_logging_target::LIBCAMERA_LOGGING_TARGET_SYSLOG,
22        }
23    }
24}
25
26#[derive(Copy, Clone, Debug)]
27pub enum LoggingLevel {
28    Debug,
29    Info,
30    Warn,
31    Error,
32    Fatal,
33}
34
35impl From<LoggingLevel> for &'static CStr {
36    fn from(value: LoggingLevel) -> Self {
37        match value {
38            LoggingLevel::Debug => c"DEBUG",
39            LoggingLevel::Info => c"INFO",
40            LoggingLevel::Warn => c"WARN",
41            LoggingLevel::Error => c"ERROR",
42            LoggingLevel::Fatal => c"FATAL",
43        }
44    }
45}
46
47#[derive(Copy, Clone, Debug)]
48pub enum LoggingStream {
49    StdOut,
50    StdErr,
51}
52
53impl From<LoggingStream> for libcamera_logging_stream_t {
54    fn from(value: LoggingStream) -> Self {
55        match value {
56            LoggingStream::StdOut => libcamera_logging_stream::LIBCAMERA_LOGGING_STREAM_STDOUT,
57            LoggingStream::StdErr => libcamera_logging_stream::LIBCAMERA_LOGGING_STREAM_STDERR,
58        }
59    }
60}
61
62/// Direct logging to a file.
63pub fn log_set_file(file: &str, color: bool) -> io::Result<()> {
64    let file = CString::new(file).expect("file contains null byte");
65    let ret = unsafe { libcamera_log_set_file(file.as_ptr(), color) };
66    handle_result(ret)
67}
68
69/// Direct logging to a stream.
70pub fn log_set_stream(stream: LoggingStream, color: bool) -> io::Result<()> {
71    let ret = unsafe { libcamera_log_set_stream(stream.into(), color) };
72    handle_result(ret)
73}
74
75/// Set the logging target.
76pub fn log_set_target(target: LoggingTarget) -> io::Result<()> {
77    let ret = unsafe { libcamera_log_set_target(target.into()) };
78    handle_result(ret)
79}