Debian Codegraph @

libs/gst/helpers/ptp/log.rs

raw
1 // GStreamer
2 //
3 // Copyright (C) 2015-2023 Sebastian Dröge <[email protected]>
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public License, v2.0.
6 // If a copy of the MPL was not distributed with this file, You can obtain one at
7 // <https://mozilla.org/MPL/2.0/>.
8 //
9 // SPDX-License-Identifier: MPL-2.0
10
11 #[cfg(not(test))]
12 use crate::io::Stderr;
13 #[cfg(not(test))]
14 use std::io::{self, Cursor, Write};
15
16 #[derive(Copy, Clone, PartialEq, Eq)]
17 #[repr(u8)]
18 #[allow(dead_code)]
19 pub enum LogLevel {
20 Error = 1,
21 Warning = 2,
22 _Fixme = 3,
23 Info = 4,
24 Debug = 5,
25 _Log = 6,
26 Trace = 7,
27 }
28
29 /*
30 * - 1 byte GstDebugLevel
31 * - 2 byte BE filename length
32 * - filename UTF-8 string
33 * - 2 byte BE module path length
34 * - module path UTF-8 string
35 * - 4 byte BE line number
36 * - remainder is UTF-8 string
37 */
38 #[cfg(test)]
39 pub fn log(
40 _level: LogLevel,
41 _file: &str,
42 _module_path: &str,
43 _line: u32,
44 _args: std::fmt::Arguments,
45 ) {
46 }
47
48 #[cfg(not(test))]
49 pub fn log(level: LogLevel, file: &str, module_path: &str, line: u32, args: std::fmt::Arguments) {
50 let mut stderr = Stderr::acquire();
51 let mut buffer = [0u8; 8192];
52 let mut cursor = Cursor::new(&mut buffer[..]);
53
54 // Silently ignore errors. What was written to the buffer was written and if there's more data
55 // than fits it will simply be cut off in the end.
56 let _ = (|| -> Result<(), io::Error> {
57 cursor.write_all(&[0u8, 0u8])?;
58 cursor.write_all(&[level as u8])?;
59 cursor.write_all(&(file.len() as u16).to_be_bytes())?;
60 cursor.write_all(file.as_bytes())?;
61 cursor.write_all(&(module_path.len() as u16).to_be_bytes())?;
62 cursor.write_all(module_path.as_bytes())?;
63 cursor.write_all(&line.to_be_bytes())?;
64 cursor.write_fmt(args)?;
65
66 Ok(())
67 })();
68
69 let pos = cursor.position() as u16;
70 if pos < 2 {
71 return;
72 }
73
74 cursor.set_position(0);
75 let _ = cursor.write_all(&(pos - 2).to_be_bytes());
76
77 let _ = stderr.write_all(&buffer[..pos as usize]);
78 }
79
80 #[allow(unused_macros)]
81 macro_rules! error {
82 ($format:expr $(, $arg:expr)* $(,)?) => {{
83 $crate::log::log($crate::log::LogLevel::Error, file!(), module_path!(), line!(), format_args!($format, $($arg),*));
84 }};
85 }
86 #[allow(unused_macros)]
87 macro_rules! warn {
88 ($format:expr $(, $arg:expr)* $(,)?) => {{
89 $crate::log::log($crate::log::LogLevel::Warning, file!(), module_path!(), line!(), format_args!($format, $($arg),*));
90 }};
91 }
92 #[allow(unused_macros)]
93 macro_rules! info {
94 ($format:expr $(, $arg:expr)* $(,)?) => {{
95 $crate::log::log($crate::log::LogLevel::Info, file!(), module_path!(), line!(), format_args!($format, $($arg),*));
96 }};
97 }
98 #[allow(unused_macros)]
99 macro_rules! debug {
100 ($format:expr $(, $arg:expr)* $(,)?) => {{
101 $crate::log::log($crate::log::LogLevel::Debug, file!(), module_path!(), line!(), format_args!($format, $($arg),*));
102 }};
103 }
104 #[allow(unused_macros)]
105 macro_rules! trace {
106 ($format:expr $(, $arg:expr)* $(,)?) => {{
107 $crate::log::log($crate::log::LogLevel::Trace, file!(), module_path!(), line!(), format_args!($format, $($arg),*));
108 }};
109 }