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
//! Logging macros and utilities
//!
//! # See also
//!
//! [C API docs](https://pipewire.pages.freedesktop.org/wireplumber/c_api/log_api.html)

mod macros;

#[doc(hidden)]
pub use self::macros::_log_inner;
pub use self::macros::{critical, debug, info, log, message, trace, warning};
#[allow(unused_imports)]
pub(crate) use self::macros::{wp_critical, wp_debug, wp_info, wp_message, wp_trace, wp_warning};
use {
	crate::prelude::*,
	glib::{GString, GStringBuilder, LogLevelFlags},
	libspa_sys::spa_log,
	std::env,
};

pub struct Log(());

impl Log {
	#[doc(alias = "WP_LOG_LEVEL_TRACE")]
	pub const LEVEL_TRACE: LogLevelFlags = LogLevelFlags::from_bits_truncate(ffi::WP_LOG_LEVEL_TRACE as _);

	/*#[doc(alias = "WP_OBJECT_FORMAT")]
	pub const OBJECT_FORMAT: CStr = ffi::WP_OBJECT_FORMAT;*/

	pub fn domain() -> &'static str {
		LibraryErrorEnum::domain().as_str()
	}

	#[doc(alias = "wp_log_level_is_enabled")]
	pub fn level_is_enabled<L: Into<LogLevelFlags>>(flags: L) -> bool {
		unsafe { from_glib(ffi::wp_log_level_is_enabled(flags.into().into_glib())) }
	}

	#[doc(alias = "wp_log_set_level")]
	pub fn set_level(level: &str) {
		env::set_var("WIREPLUMBER_DEBUG", level); // XXX: this doesn't seem to work properly otherwise?
		unsafe { ffi::wp_log_set_level(level.to_glib_none().0) }
	}

	pub fn set_default_level(level: &str) {
		if env::var_os("WIREPLUMBER_DEBUG").is_none() {
			Self::set_level(level)
		}
	}

	#[doc(alias = "wp_spa_log_get_instance")]
	pub fn spa_log() -> *mut spa_log {
		unsafe { ffi::wp_spa_log_get_instance() }
	}

	#[doc(alias = "wp_log_structured_standard")]
	pub fn log_string<M: Into<GString>, L: Into<LogLevelFlags>>(log_level: L, context: StructuredLogContext, message: M) {
		unsafe {
			// XXX: so much allocation, it burns...
			let domain = context.domain.unwrap_or_default().to_glib_none();
			let file = context.file.unwrap_or_default().to_glib_none();
			let line = context.line.as_ref().map(ToString::to_string);
			let line = line.as_ref().map(|l| &l[..]).unwrap_or_default().to_glib_none();
			let function = context.function.unwrap_or_default().to_glib_none();
			let obj_type = context.object_type.unwrap_or(match context.object {
				Some(obj) => obj.type_(),
				None => Type::INVALID,
			});
			let obj = context.object.as_ref().map(|obj| (*obj).to_glib_none());
			let message = message.into();
			ffi::wp_log_structured_standard(
				domain.0,
				log_level.into().into_glib(),
				file.0,
				line.0,
				function.0,
				obj_type.into_glib(),
				obj.as_ref().map(|o| o.0).unwrap_or(ptr::null()) as *mut _,
				b"%s\0".as_ptr() as *const _,
				message.as_ptr(),
			)
		}
	}

	pub fn log_args<O: AsRef<GObject>, L: Into<LogLevelFlags>>(
		log_level: L,
		context: StructuredLogContext<O>,
		args: fmt::Arguments,
	) {
		let mut message = GStringBuilder::default();
		let _ = write!(message, "{args}");
		Self::log_string(log_level.into(), context.to_object(), message.into_string())
	}

	#[doc(alias = "wp_log_writer_default")]
	pub unsafe fn writer_default<L: Into<LogLevelFlags>>(
		log_levels: L,
		fields: &[glib::ffi::GLogField],
		user_data: gpointer,
	) -> glib::ffi::GLogWriterOutput {
		ffi::wp_log_writer_default(log_levels.into().into_glib(), fields.as_ptr(), fields.len(), user_data)
	}
	// TODO: wp_log_writer_default, wp_log_structured_standard
}

#[derive(Debug, Clone, Default)]
pub struct StructuredLogContext<'a, O = GObject> {
	pub domain: Option<&'a str>,
	pub file: Option<&'static str>,
	pub line: Option<u32>,
	pub function: Option<&'a str>,
	pub object: Option<&'a O>,
	pub object_type: Option<Type>,
}

impl<'a, O> StructuredLogContext<'a, O> {
	fn to_object(&self) -> StructuredLogContext<'a, GObject>
	where
		O: AsRef<GObject>,
	{
		StructuredLogContext {
			domain: self.domain,
			file: self.file,
			line: self.line,
			function: self.function,
			object: self.object.map(|o| o.as_ref()),
			object_type: self.object_type,
		}
	}
}