Skip to main content

glib/
log.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3#[cfg(unix)]
4use std::os::unix::io::{AsFd, AsRawFd};
5use std::{
6    boxed::Box as Box_,
7    sync::{Arc, Mutex, OnceLock},
8};
9
10#[cfg(feature = "v2_80")]
11use crate::StrVRef;
12use crate::{GStr, GString, LogWriterOutput, ffi, translate::*};
13
14#[derive(Debug)]
15pub struct LogHandlerId(u32);
16
17#[doc(hidden)]
18impl FromGlib<u32> for LogHandlerId {
19    #[inline]
20    unsafe fn from_glib(value: u32) -> Self {
21        Self(value)
22    }
23}
24
25#[doc(hidden)]
26impl IntoGlib for LogHandlerId {
27    type GlibType = u32;
28
29    #[inline]
30    fn into_glib(self) -> u32 {
31        self.0
32    }
33}
34
35#[derive(Copy, Clone, Debug, PartialEq, Eq)]
36pub enum LogLevel {
37    #[doc(alias = "G_LOG_LEVEL_ERROR")]
38    Error,
39    #[doc(alias = "G_LOG_LEVEL_CRITICAL")]
40    Critical,
41    #[doc(alias = "G_LOG_LEVEL_WARNING")]
42    Warning,
43    #[doc(alias = "G_LOG_LEVEL_MESSAGE")]
44    Message,
45    #[doc(alias = "G_LOG_LEVEL_INFO")]
46    Info,
47    #[doc(alias = "G_LOG_LEVEL_DEBUG")]
48    Debug,
49}
50
51#[doc(hidden)]
52impl IntoGlib for LogLevel {
53    type GlibType = u32;
54
55    #[inline]
56    fn into_glib(self) -> u32 {
57        match self {
58            Self::Error => ffi::G_LOG_LEVEL_ERROR,
59            Self::Critical => ffi::G_LOG_LEVEL_CRITICAL,
60            Self::Warning => ffi::G_LOG_LEVEL_WARNING,
61            Self::Message => ffi::G_LOG_LEVEL_MESSAGE,
62            Self::Info => ffi::G_LOG_LEVEL_INFO,
63            Self::Debug => ffi::G_LOG_LEVEL_DEBUG,
64        }
65    }
66}
67
68#[doc(hidden)]
69impl FromGlib<u32> for LogLevel {
70    #[inline]
71    unsafe fn from_glib(value: u32) -> Self {
72        if value & ffi::G_LOG_LEVEL_ERROR != 0 {
73            Self::Error
74        } else if value & ffi::G_LOG_LEVEL_CRITICAL != 0 {
75            Self::Critical
76        } else if value & ffi::G_LOG_LEVEL_WARNING != 0 {
77            Self::Warning
78        } else if value & ffi::G_LOG_LEVEL_MESSAGE != 0 {
79            Self::Message
80        } else if value & ffi::G_LOG_LEVEL_INFO != 0 {
81            Self::Info
82        } else if value & ffi::G_LOG_LEVEL_DEBUG != 0 {
83            Self::Debug
84        } else {
85            panic!("Unknown log level: {value}")
86        }
87    }
88}
89
90impl LogLevel {
91    #[doc(hidden)]
92    pub fn priority(&self) -> &'static str {
93        match self {
94            Self::Error => "3",
95            Self::Critical => "4",
96            Self::Warning => "4",
97            Self::Message => "5",
98            Self::Info => "6",
99            Self::Debug => "7",
100        }
101    }
102}
103
104bitflags::bitflags! {
105    #[doc(alias = "GLogLevelFlags")]
106    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
107    pub struct LogLevels: u32 {
108        #[doc(alias = "G_LOG_LEVEL_ERROR")]
109        const LEVEL_ERROR = ffi::G_LOG_LEVEL_ERROR;
110        #[doc(alias = "G_LOG_LEVEL_CRITICAL")]
111        const LEVEL_CRITICAL = ffi::G_LOG_LEVEL_CRITICAL;
112        #[doc(alias = "G_LOG_LEVEL_WARNING")]
113        const LEVEL_WARNING = ffi::G_LOG_LEVEL_WARNING;
114        #[doc(alias = "G_LOG_LEVEL_MESSAGE")]
115        const LEVEL_MESSAGE = ffi::G_LOG_LEVEL_MESSAGE;
116        #[doc(alias = "G_LOG_LEVEL_INFO")]
117        const LEVEL_INFO = ffi::G_LOG_LEVEL_INFO;
118        #[doc(alias = "G_LOG_LEVEL_DEBUG")]
119        const LEVEL_DEBUG = ffi::G_LOG_LEVEL_DEBUG;
120    }
121}
122
123#[doc(hidden)]
124impl IntoGlib for LogLevels {
125    type GlibType = ffi::GLogLevelFlags;
126
127    #[inline]
128    fn into_glib(self) -> ffi::GLogLevelFlags {
129        self.bits()
130    }
131}
132
133#[doc(hidden)]
134impl FromGlib<ffi::GLogLevelFlags> for LogLevels {
135    #[inline]
136    unsafe fn from_glib(value: ffi::GLogLevelFlags) -> Self {
137        Self::from_bits_truncate(value)
138    }
139}
140
141fn to_log_flags(fatal: bool, recursion: bool) -> u32 {
142    (if fatal { ffi::G_LOG_FLAG_FATAL } else { 0 })
143        | if recursion {
144            ffi::G_LOG_FLAG_RECURSION
145        } else {
146            0
147        }
148}
149
150#[doc(alias = "g_log_set_handler_full")]
151pub fn log_set_handler<P: Fn(Option<&str>, LogLevel, &str) + Send + Sync + 'static>(
152    log_domain: Option<&str>,
153    log_levels: LogLevels,
154    fatal: bool,
155    recursion: bool,
156    log_func: P,
157) -> LogHandlerId {
158    let log_func_data: Box_<P> = Box_::new(log_func);
159    unsafe extern "C" fn log_func_func<
160        P: Fn(Option<&str>, LogLevel, &str) + Send + Sync + 'static,
161    >(
162        log_domain: *const libc::c_char,
163        log_level: ffi::GLogLevelFlags,
164        message: *const libc::c_char,
165        user_data: ffi::gpointer,
166    ) {
167        unsafe {
168            let log_domain: Borrowed<Option<GString>> = from_glib_borrow(log_domain);
169            let message: Borrowed<GString> = from_glib_borrow(message);
170            let callback: &P = &*(user_data as *mut _);
171            (*callback)(
172                (*log_domain).as_ref().map(|s| s.as_str()),
173                from_glib(log_level),
174                message.as_str(),
175            );
176        }
177    }
178    let log_func = Some(log_func_func::<P> as _);
179    unsafe extern "C" fn destroy_func<
180        P: Fn(Option<&str>, LogLevel, &str) + Send + Sync + 'static,
181    >(
182        data: ffi::gpointer,
183    ) {
184        unsafe {
185            let _callback: Box_<P> = Box_::from_raw(data as *mut _);
186        }
187    }
188    let destroy_call4 = Some(destroy_func::<P> as _);
189    let super_callback0: Box_<P> = log_func_data;
190    unsafe {
191        from_glib(ffi::g_log_set_handler_full(
192            log_domain.to_glib_none().0,
193            log_levels.into_glib() | to_log_flags(fatal, recursion),
194            log_func,
195            Box_::into_raw(super_callback0) as *mut _,
196            destroy_call4,
197        ))
198    }
199}
200
201#[doc(alias = "g_log_remove_handler")]
202pub fn log_remove_handler(log_domain: Option<&str>, handler_id: LogHandlerId) {
203    unsafe {
204        ffi::g_log_remove_handler(log_domain.to_glib_none().0, handler_id.into_glib());
205    }
206}
207
208#[doc(alias = "g_log_set_always_fatal")]
209pub fn log_set_always_fatal(fatal_levels: LogLevels) -> LogLevels {
210    unsafe { from_glib(ffi::g_log_set_always_fatal(fatal_levels.into_glib())) }
211}
212
213#[doc(alias = "g_log_set_fatal_mask")]
214pub fn log_set_fatal_mask(log_domain: Option<&str>, fatal_levels: LogLevels) -> LogLevels {
215    unsafe {
216        from_glib(ffi::g_log_set_fatal_mask(
217            log_domain.to_glib_none().0,
218            fatal_levels.into_glib(),
219        ))
220    }
221}
222
223type PrintCallback = dyn Fn(&str) + Send + Sync + 'static;
224
225fn print_handler() -> &'static Mutex<Option<Arc<PrintCallback>>> {
226    static MUTEX: OnceLock<Mutex<Option<Arc<PrintCallback>>>> = OnceLock::new();
227    MUTEX.get_or_init(|| Mutex::new(None))
228}
229
230// rustdoc-stripper-ignore-next
231/// To set back the default print handler, use the [`unset_print_handler`] function.
232#[doc(alias = "g_set_print_handler")]
233pub fn set_print_handler<P: Fn(&str) + Send + Sync + 'static>(func: P) {
234    unsafe extern "C" fn func_func(string: *const libc::c_char) {
235        unsafe {
236            if let Some(callback) = print_handler()
237                .lock()
238                .expect("Failed to lock PRINT_HANDLER")
239                .as_ref()
240                .map(Arc::clone)
241            {
242                let string: Borrowed<GString> = from_glib_borrow(string);
243                (*callback)(string.as_str())
244            }
245        }
246    }
247    *print_handler()
248        .lock()
249        .expect("Failed to lock PRINT_HANDLER to change callback") = Some(Arc::new(func));
250    unsafe { ffi::g_set_print_handler(Some(func_func as _)) };
251}
252
253// rustdoc-stripper-ignore-next
254/// To set the default print handler, use the [`set_print_handler`] function.
255pub fn unset_print_handler() {
256    *print_handler()
257        .lock()
258        .expect("Failed to lock PRINT_HANDLER to remove callback") = None;
259    unsafe { ffi::g_set_print_handler(None) };
260}
261
262fn printerr_handler() -> &'static Mutex<Option<Arc<PrintCallback>>> {
263    static MUTEX: OnceLock<Mutex<Option<Arc<PrintCallback>>>> = OnceLock::new();
264    MUTEX.get_or_init(|| Mutex::new(None))
265}
266
267// rustdoc-stripper-ignore-next
268/// To set back the default print handler, use the [`unset_printerr_handler`] function.
269#[doc(alias = "g_set_printerr_handler")]
270pub fn set_printerr_handler<P: Fn(&str) + Send + Sync + 'static>(func: P) {
271    unsafe extern "C" fn func_func(string: *const libc::c_char) {
272        unsafe {
273            if let Some(callback) = printerr_handler()
274                .lock()
275                .expect("Failed to lock PRINTERR_HANDLER")
276                .as_ref()
277                .map(Arc::clone)
278            {
279                let string: Borrowed<GString> = from_glib_borrow(string);
280                (*callback)(string.as_str())
281            }
282        }
283    }
284    *printerr_handler()
285        .lock()
286        .expect("Failed to lock PRINTERR_HANDLER to change callback") = Some(Arc::new(func));
287    unsafe { ffi::g_set_printerr_handler(Some(func_func as _)) };
288}
289
290// rustdoc-stripper-ignore-next
291/// To set the default print handler, use the [`set_printerr_handler`] function.
292pub fn unset_printerr_handler() {
293    *printerr_handler()
294        .lock()
295        .expect("Failed to lock PRINTERR_HANDLER to remove callback") = None;
296    unsafe { ffi::g_set_printerr_handler(None) };
297}
298
299type LogCallback = dyn Fn(Option<&str>, LogLevel, &str) + Send + Sync + 'static;
300
301fn default_handler() -> &'static Mutex<Option<Arc<LogCallback>>> {
302    static MUTEX: OnceLock<Mutex<Option<Arc<LogCallback>>>> = OnceLock::new();
303    MUTEX.get_or_init(|| Mutex::new(None))
304}
305
306// rustdoc-stripper-ignore-next
307/// To set back the default print handler, use the [`log_unset_default_handler`] function.
308#[doc(alias = "g_log_set_default_handler")]
309pub fn log_set_default_handler<P: Fn(Option<&str>, LogLevel, &str) + Send + Sync + 'static>(
310    log_func: P,
311) {
312    unsafe extern "C" fn func_func(
313        log_domain: *const libc::c_char,
314        log_levels: ffi::GLogLevelFlags,
315        message: *const libc::c_char,
316        _user_data: ffi::gpointer,
317    ) {
318        unsafe {
319            if let Some(callback) = default_handler()
320                .lock()
321                .expect("Failed to lock DEFAULT_HANDLER")
322                .as_ref()
323                .map(Arc::clone)
324            {
325                let log_domain: Borrowed<Option<GString>> = from_glib_borrow(log_domain);
326                let message: Borrowed<GString> = from_glib_borrow(message);
327                (*callback)(
328                    (*log_domain).as_ref().map(|s| s.as_str()),
329                    from_glib(log_levels),
330                    message.as_str(),
331                );
332            }
333        }
334    }
335    *default_handler()
336        .lock()
337        .expect("Failed to lock DEFAULT_HANDLER to change callback") = Some(Arc::new(log_func));
338    unsafe { ffi::g_log_set_default_handler(Some(func_func as _), std::ptr::null_mut()) };
339}
340
341// rustdoc-stripper-ignore-next
342/// To set the default print handler, use the [`log_set_default_handler`] function.
343#[doc(alias = "g_log_set_default_handler")]
344pub fn log_unset_default_handler() {
345    *default_handler()
346        .lock()
347        .expect("Failed to lock DEFAULT_HANDLER to remove callback") = None;
348    unsafe {
349        ffi::g_log_set_default_handler(Some(ffi::g_log_default_handler), std::ptr::null_mut())
350    };
351}
352
353#[doc(alias = "g_log_default_handler")]
354pub fn log_default_handler(log_domain: Option<&str>, log_level: LogLevel, message: Option<&str>) {
355    unsafe {
356        ffi::g_log_default_handler(
357            log_domain.to_glib_none().0,
358            log_level.into_glib(),
359            message.to_glib_none().0,
360            std::ptr::null_mut(),
361        )
362    }
363}
364
365// rustdoc-stripper-ignore-next
366/// Structure representing a single field in a structured log entry.
367///
368/// See [`g_log_structured`][gls] for details. Log fields may contain UTF-8 strings, binary with
369/// embedded nul bytes, or arbitrary pointers.
370///
371/// [gls]: https://docs.gtk.org/glib/func.log_structured.html
372#[repr(transparent)]
373#[derive(Debug)]
374#[doc(alias = "GLogField")]
375pub struct LogField<'a>(ffi::GLogField, std::marker::PhantomData<&'a GStr>);
376
377impl<'a> LogField<'a> {
378    // rustdoc-stripper-ignore-next
379    /// Creates a field from a borrowed key and value.
380    pub fn new(key: &'a GStr, value: &[u8]) -> Self {
381        let (value, length) = if value.is_empty() {
382            // Use an empty C string to represent empty data, since length: 0 is reserved for user
383            // data fields.
384            (&[0u8] as &[u8], -1isize)
385        } else {
386            (value, value.len().try_into().unwrap())
387        };
388        Self(
389            ffi::GLogField {
390                key: key.as_ptr(),
391                value: value.as_ptr() as *const _,
392                length,
393            },
394            Default::default(),
395        )
396    }
397    // rustdoc-stripper-ignore-next
398    /// Creates a field with an empty value and `data` as a user data key. Fields created with this
399    /// function are ignored by the default log writer. These fields are used to pass custom data
400    /// into a writer function set with [`log_set_writer_func`], where it can be retrieved using
401    /// [`Self::user_data`].
402    ///
403    /// The passed `usize` can be used by the log writer as a key into a static data structure.
404    /// Thread locals are preferred since the log writer function will run in the same thread that
405    /// invoked [`log_structured_array`].
406    pub fn new_user_data(key: &'a GStr, data: usize) -> Self {
407        Self(
408            ffi::GLogField {
409                key: key.as_ptr(),
410                value: std::ptr::without_provenance(data),
411                length: 0,
412            },
413            Default::default(),
414        )
415    }
416    // rustdoc-stripper-ignore-next
417    /// Retrieves the field key.
418    pub fn key(&self) -> &str {
419        unsafe { std::ffi::CStr::from_ptr(self.0.key as *const _) }
420            .to_str()
421            .unwrap()
422    }
423    // rustdoc-stripper-ignore-next
424    /// Retrieves a byte array of the field value. Returns `None` if the field was created with
425    /// [`Self::new_user_data`].
426    pub fn value_bytes(&self) -> Option<&[u8]> {
427        match self.0.length {
428            0 => None,
429            n if n < 0 => {
430                Some(unsafe { std::ffi::CStr::from_ptr(self.0.value as *const _) }.to_bytes())
431            }
432            _ => Some(unsafe {
433                std::slice::from_raw_parts(self.0.value as *const u8, self.0.length as usize)
434            }),
435        }
436    }
437    // rustdoc-stripper-ignore-next
438    /// Retrieves a string of the field value, or `None` if the string is not valid UTF-8. Also
439    /// returns `None` if the field was created with [`Self::new_user_data`].
440    pub fn value_str(&self) -> Option<&str> {
441        std::str::from_utf8(self.value_bytes()?).ok()
442    }
443    // rustdoc-stripper-ignore-next
444    /// Retrieves the the user data value from a field created with [`Self::new_user_data`].
445    /// Returns `None` if the field was created with [`Self::new`].
446    pub fn user_data(&self) -> Option<usize> {
447        (self.0.length == 0).then_some(self.0.value.addr())
448    }
449}
450
451type WriterCallback = dyn Fn(LogLevel, &[LogField<'_>]) -> LogWriterOutput + Send + Sync + 'static;
452
453static WRITER_FUNC: OnceLock<Box<WriterCallback>> = OnceLock::new();
454
455#[doc(alias = "g_log_set_writer_func")]
456pub fn log_set_writer_func<
457    P: Fn(LogLevel, &[LogField<'_>]) -> LogWriterOutput + Send + Sync + 'static,
458>(
459    writer_func: P,
460) {
461    if WRITER_FUNC.set(Box::new(writer_func)).is_err() {
462        panic!("Writer func can only be set once");
463    }
464    unsafe extern "C" fn writer_trampoline(
465        log_level: ffi::GLogLevelFlags,
466        fields: *const ffi::GLogField,
467        n_fields: libc::size_t,
468        _user_data: ffi::gpointer,
469    ) -> ffi::GLogWriterOutput {
470        unsafe {
471            let writer_func = WRITER_FUNC.get().unwrap();
472            let fields = std::slice::from_raw_parts(fields as *const LogField<'_>, n_fields);
473            writer_func(from_glib(log_level), fields).into_glib()
474        }
475    }
476    unsafe {
477        ffi::g_log_set_writer_func(Some(writer_trampoline), std::ptr::null_mut(), None);
478    }
479}
480
481#[macro_export]
482#[doc(hidden)]
483macro_rules! g_log_inner {
484    ($log_domain:expr, $log_level:expr, $format:literal $(,$arg:expr)* $(,)?) => {{
485        let mut w = $crate::GStringBuilder::default();
486
487        // Can't really happen but better safe than sorry
488        if !std::fmt::Write::write_fmt(&mut w, std::format_args!($format, $($arg),*)).is_err() {
489            unsafe {
490                $crate::ffi::g_log(
491                    $crate::translate::ToGlibPtr::to_glib_none(&$log_domain).0,
492                    <$crate::LogLevel as $crate::translate::IntoGlib>::into_glib(
493                        $log_level
494                    ),
495                    b"%s\0".as_ptr() as *const _,
496                    $crate::translate::ToGlibPtr::<*const std::os::raw::c_char>::to_glib_none(
497                        &w.into_string()
498                    ).0,
499                );
500            }
501        }
502    }};
503}
504
505// rustdoc-stripper-ignore-next
506/// Macro used to log using GLib logging system. It uses [g_log].
507///
508/// [g_log]: https://docs.gtk.org/glib/func.log.html
509///
510/// Example:
511///
512/// ```no_run
513/// use glib::{LogLevel, g_log};
514///
515/// g_log!("test", LogLevel::Debug, "test");
516/// g_log!("test", LogLevel::Message, "test");
517/// // trailing commas work as well:
518/// g_log!("test", LogLevel::Message, "test",);
519///
520/// // You can also pass arguments like in format! or println!:
521/// let x = 12;
522/// g_log!("test", LogLevel::Error, "test: {}", x);
523/// g_log!("test", LogLevel::Critical, "test: {}", x);
524/// g_log!("test", LogLevel::Warning, "test: {} {}", x, "a");
525/// // trailing commas work as well:
526/// g_log!("test", LogLevel::Warning, "test: {} {}", x, "a",);
527/// ```
528///
529/// To be noted that the log domain is optional:
530///
531/// ```no_run
532/// use glib::{LogLevel, g_log};
533///
534/// // As you can see: no log domain:
535/// g_log!(LogLevel::Message, "test");
536/// // For the rest, it's just like when you have the log domain:
537/// // trailing commas:
538/// g_log!(LogLevel::Message, "test",);
539///
540/// // formatting:
541/// let x = 12;
542/// g_log!(LogLevel::Warning, "test: {} {}", x, "a");
543/// g_log!(LogLevel::Warning, "test: {} {}", x, "a",);
544/// ```
545#[macro_export]
546macro_rules! g_log {
547    ($log_level:expr, $format:literal $(,$arg:expr)* $(,)?) => {{
548        $crate::g_log_inner!(None::<&str>, $log_level, $format, $($arg),*);
549    }};
550    ($log_domain:expr, $log_level:expr, $format:literal $(,$arg:expr)* $(,)?) => {{
551        let log_domain = <Option<&str> as std::convert::From<_>>::from($log_domain);
552        $crate::g_log_inner!(log_domain, $log_level, $format, $($arg),*);
553    }};
554}
555
556// rustdoc-stripper-ignore-next
557/// Macro used to log using GLib logging system. It uses [g_log].
558///
559/// [g_log]: https://docs.gtk.org/glib/func.log.html
560///
561/// It is the same as calling the [`g_log!`](crate::g_log!) macro with [`LogLevel::Error`].
562///
563/// Example:
564///
565/// ```no_run
566/// use glib::g_error;
567///
568/// g_error!("test", "test");
569/// // Equivalent to:
570/// use glib::{g_log, LogLevel};
571/// g_log!("test", LogLevel::Error, "test");
572///
573/// // trailing commas work as well:
574/// g_error!("test", "test",);
575///
576/// // You can also pass arguments like in format! or println!:
577/// let x = 12;
578/// g_error!("test", "test: {}", x);
579/// g_error!("test", "test: {} {}", x, "a");
580/// // trailing commas work as well:
581/// g_error!("test", "test: {} {}", x, "a",);
582/// ```
583#[macro_export]
584macro_rules! g_error {
585    ($log_domain:expr, $format:literal, $($arg:expr),* $(,)?) => {{
586        $crate::g_log!($log_domain, $crate::LogLevel::Error, $format, $($arg),*);
587    }};
588    ($log_domain:expr, $format:literal $(,)?) => {{
589        $crate::g_log!($log_domain, $crate::LogLevel::Error, $format);
590    }};
591}
592
593// rustdoc-stripper-ignore-next
594/// Macro used to log using GLib logging system. It uses [g_log].
595///
596/// [g_log]: https://docs.gtk.org/glib/func.log.html
597///
598/// It is the same as calling the [`g_log!`](crate::g_log!) macro with [`LogLevel::Critical`].
599///
600/// Example:
601///
602/// ```no_run
603/// use glib::g_critical;
604///
605/// g_critical!("test", "test");
606/// // Equivalent to:
607/// use glib::{g_log, LogLevel};
608/// g_log!("test", LogLevel::Critical, "test");
609///
610/// // trailing commas work as well:
611/// g_critical!("test", "test",);
612///
613/// // You can also pass arguments like in format! or println!:
614/// let x = 12;
615/// g_critical!("test", "test: {}", x);
616/// g_critical!("test", "test: {} {}", x, "a");
617/// // trailing commas work as well:
618/// g_critical!("test", "test: {} {}", x, "a",);
619/// ```
620#[macro_export]
621macro_rules! g_critical {
622    ($log_domain:expr, $format:literal, $($arg:expr),* $(,)?) => {{
623        $crate::g_log!($log_domain, $crate::LogLevel::Critical, $format, $($arg),*);
624    }};
625    ($log_domain:expr, $format:literal $(,)?) => {{
626        $crate::g_log!($log_domain, $crate::LogLevel::Critical, $format);
627    }};
628}
629
630// rustdoc-stripper-ignore-next
631/// Macro used to log using GLib logging system. It uses [g_log].
632///
633/// [g_log]: https://docs.gtk.org/glib/func.log.html
634///
635/// It is the same as calling the [`g_log!`](crate::g_log!) macro with [`LogLevel::Warning`].
636///
637/// Example:
638///
639/// ```no_run
640/// use glib::g_warning;
641///
642/// g_warning!("test", "test");
643/// // Equivalent to:
644/// use glib::{g_log, LogLevel};
645/// g_log!("test", LogLevel::Warning, "test");
646///
647/// // trailing commas work as well:
648/// g_warning!("test", "test",);
649///
650/// // You can also pass arguments like in format! or println!:
651/// let x = 12;
652/// g_warning!("test", "test: {}", x);
653/// g_warning!("test", "test: {} {}", x, "a");
654/// // trailing commas work as well:
655/// g_warning!("test", "test: {} {}", x, "a",);
656/// ```
657#[macro_export]
658macro_rules! g_warning {
659    ($log_domain:expr, $format:literal, $($arg:expr),* $(,)?) => {{
660        $crate::g_log!($log_domain, $crate::LogLevel::Warning, $format, $($arg),*);
661    }};
662    ($log_domain:expr, $format:literal $(,)?) => {{
663        $crate::g_log!($log_domain, $crate::LogLevel::Warning, $format);
664    }};
665}
666
667// rustdoc-stripper-ignore-next
668/// Macro used to log using GLib logging system. It uses [g_log].
669///
670/// [g_log]: https://docs.gtk.org/glib/func.log.html
671///
672/// It is the same as calling the [`g_log!`](crate::g_log!) macro with [`LogLevel::Message`].
673///
674/// Example:
675///
676/// ```no_run
677/// use glib::g_message;
678///
679/// g_message!("test", "test");
680/// // Equivalent to:
681/// use glib::{g_log, LogLevel};
682/// g_log!("test", LogLevel::Message, "test");
683///
684/// // trailing commas work as well:
685/// g_message!("test", "test",);
686///
687/// // You can also pass arguments like in format! or println!:
688/// let x = 12;
689/// g_message!("test", "test: {}", x);
690/// g_message!("test", "test: {} {}", x, "a");
691/// // trailing commas work as well:
692/// g_message!("test", "test: {} {}", x, "a",);
693/// ```
694#[macro_export]
695macro_rules! g_message {
696    ($log_domain:expr, $format:literal, $($arg:expr),* $(,)?) => {{
697        $crate::g_log!($log_domain, $crate::LogLevel::Message, $format, $($arg),*);
698    }};
699    ($log_domain:expr, $format:literal $(,)?) => {{
700        $crate::g_log!($log_domain, $crate::LogLevel::Message, $format);
701    }};
702}
703
704// rustdoc-stripper-ignore-next
705/// Macro used to log using GLib logging system. It uses [g_log].
706///
707/// [g_log]: https://docs.gtk.org/glib/func.log.html
708///
709/// It is the same as calling the [`g_log!`](crate::g_log!) macro with [`LogLevel::Info`].
710///
711/// Example:
712///
713/// ```no_run
714/// use glib::g_info;
715///
716/// g_info!("test", "test");
717/// // Equivalent to:
718/// use glib::{g_log, LogLevel};
719/// g_log!("test", LogLevel::Info, "test");
720///
721/// // trailing commas work as well:
722/// g_info!("test", "test",);
723///
724/// // You can also pass arguments like in format! or println!:
725/// let x = 12;
726/// g_info!("test", "test: {}", x);
727/// g_info!("test", "test: {} {}", x, "a");
728/// // trailing commas work as well:
729/// g_info!("test", "test: {} {}", x, "a",);
730/// ```
731#[macro_export]
732macro_rules! g_info {
733    ($log_domain:expr, $format:literal, $($arg:expr),* $(,)?) => {{
734        $crate::g_log!($log_domain, $crate::LogLevel::Info, $format, $($arg),*);
735    }};
736    ($log_domain:expr, $format:literal $(,)?) => {{
737        $crate::g_log!($log_domain, $crate::LogLevel::Info, $format);
738    }};
739}
740
741// rustdoc-stripper-ignore-next
742/// Macro used to log using GLib logging system. It uses [g_log].
743///
744/// [g_log]: https://docs.gtk.org/glib/func.log.html
745///
746/// It is the same as calling the [`g_log!`](crate::g_log!) macro with [`LogLevel::Debug`].
747///
748/// Example:
749///
750/// ```no_run
751/// use glib::g_debug;
752///
753/// g_debug!("test", "test");
754/// // Equivalent to:
755/// use glib::{g_log, LogLevel};
756/// g_log!("test", LogLevel::Debug, "test");
757///
758/// // trailing commas work as well:
759/// g_debug!("test", "test",);
760///
761/// // You can also pass arguments like in format! or println!:
762/// let x = 12;
763/// g_debug!("test", "test: {}", x);
764/// g_debug!("test", "test: {} {}", x, "a");
765/// // trailing commas work as well:
766/// g_debug!("test", "test: {} {}", x, "a",);
767/// ```
768#[macro_export]
769macro_rules! g_debug {
770    ($log_domain:expr, $format:literal, $($arg:expr),* $(,)?) => {{
771        $crate::g_log!($log_domain, $crate::LogLevel::Debug, $format, $($arg),*);
772    }};
773    ($log_domain:expr, $format:literal $(,)?) => {{
774        $crate::g_log!($log_domain, $crate::LogLevel::Debug, $format);
775    }};
776}
777
778#[doc(hidden)]
779#[macro_export]
780macro_rules! g_print_inner {
781    ($func:ident, $format:expr $(, $arg:expr)* $(,)?) => {{
782        let mut w = $crate::GStringBuilder::default();
783
784        // Can't really happen but better safe than sorry
785        if !std::fmt::Write::write_fmt(&mut w, std::format_args!($format, $($arg),*)).is_err() {
786            unsafe {
787                $crate::ffi::$func(
788                    b"%s\0".as_ptr() as *const _,
789                    $crate::translate::ToGlibPtr::<*const std::os::raw::c_char>::to_glib_none(
790                        &w.into_string()
791                    ).0,
792                );
793            }
794        }
795    }};
796}
797
798// rustdoc-stripper-ignore-next
799/// Macro used to print messages. It uses [g_print].
800///
801/// [g_print]: https://docs.gtk.org/glib/func.print.html
802///
803/// Example:
804///
805/// ```no_run
806/// use glib::g_print;
807///
808/// g_print!("test");
809/// // trailing commas work as well:
810/// g_print!("test",);
811///
812/// let x = 12;
813/// g_print!("test: {}", x);
814/// g_print!("test: {} {}", x, "a");
815/// // trailing commas work as well:
816/// g_print!("test: {} {}", x, "a",);
817/// ```
818#[macro_export]
819macro_rules! g_print {
820    ($format:expr $(,$arg:expr)* $(,)?) => {{
821        $crate::g_print_inner!(g_print, $format, $($arg),*);
822    }};
823}
824
825// rustdoc-stripper-ignore-next
826/// Macro used to print error messages. It uses [g_printerr].
827///
828/// [g_printerr]: https://docs.gtk.org/glib/func.printerr.html
829///
830/// Example:
831///
832/// ```no_run
833/// use glib::g_printerr;
834///
835/// g_printerr!("test");
836/// // trailing commas work as well:
837/// g_printerr!("test",);
838///
839/// let x = 12;
840/// g_printerr!("test: {}", x);
841/// g_printerr!("test: {} {}", x, "a");
842/// // trailing commas work as well:
843/// g_printerr!("test: {} {}", x, "a",);
844/// ```
845#[macro_export]
846macro_rules! g_printerr {
847    ($format:expr $(, $arg:expr)* $(,)?) => {{
848        $crate::g_print_inner!(g_printerr, $format, $($arg),*);
849    }};
850}
851
852// rustdoc-stripper-ignore-next
853/// Macro used to log using GLib structured logging system.
854///
855/// The structured data is provided inside braces as key-value pairs using the `=>` token and
856/// separated by semicolons. The key can be a string literal or an expression that satisfies
857/// [`AsRef<GStr>`]. The value can be a format string with arguments, or a single expression that
858/// satisfies `AsRef<[u8]>`.
859///
860/// See [`g_log_structured`][gls] for more details.
861///
862/// [gls]: https://docs.gtk.org/glib/func.log_structured.html
863/// [`AsRef<GStr>`]: crate::GStr
864///
865/// Example:
866///
867/// ```no_run
868/// use glib::{GString, LogLevel, log_structured};
869/// use std::ffi::CString;
870///
871/// log_structured!(
872///     "test",
873///     LogLevel::Debug,
874///     {
875///         // a normal string field
876///         "MY_FIELD" => "123";
877///         // fields can also take format arguments
878///         "MY_FIELD2" => "abc {}", "def";
879///         // single argument can be a &str or a &[u8] or anything else satisfying AsRef<[u8]>
880///         "MY_FIELD3" => CString::new("my string").unwrap().to_bytes();
881///         // field names can also be dynamic
882///         GString::from("MY_FIELD4") => b"a binary string".to_owned();
883///         // the main log message goes in the MESSAGE field
884///         "MESSAGE" => "test: {} {}", 1, 2, ;
885///     }
886/// );
887/// ```
888#[macro_export]
889macro_rules! log_structured {
890    ($log_domain:expr, $log_level:expr, {$($key:expr => $format:expr $(,$arg:expr)* $(,)?);+ $(;)?} $(,)?) => {{
891        let log_domain = <Option<&str> as std::convert::From<_>>::from($log_domain);
892        let log_domain_str = log_domain.unwrap_or_default();
893        let level: $crate::LogLevel = $log_level;
894        let field_count =
895            <[()]>::len(&[$($crate::log_structured_inner!(@clear $key)),+])
896            + log_domain.map(|_| 2usize).unwrap_or(1usize)
897            + 3;
898
899        let mut line = [0u8; 32]; // 32 decimal digits of line numbers should be enough!
900        let line = {
901            use std::io::Write;
902
903            let mut cursor = std::io::Cursor::new(&mut line[..]);
904            std::write!(&mut cursor, "{}", line!()).unwrap();
905            let pos = cursor.position() as usize;
906            &line[..pos]
907        };
908
909        $crate::log_structured_array(
910            level,
911            &[
912                $crate::LogField::new(
913                    $crate::gstr!("PRIORITY"),
914                    level.priority().as_bytes(),
915                ),
916                $crate::LogField::new(
917                    $crate::gstr!("CODE_FILE"),
918                    file!().as_bytes(),
919                ),
920                $crate::LogField::new(
921                    $crate::gstr!("CODE_LINE"),
922                    line,
923                ),
924                $crate::LogField::new(
925                    $crate::gstr!("CODE_FUNC"),
926                    $crate::function_name!().as_bytes(),
927                ),
928                $(
929                    $crate::LogField::new(
930                        $crate::log_structured_inner!(@key $key),
931                        $crate::log_structured_inner!(@value $format $(,$arg)*),
932                    ),
933                )+
934                $crate::LogField::new(
935                    $crate::gstr!("GLIB_DOMAIN"),
936                    log_domain_str.as_bytes(),
937                ),
938            ][0..field_count],
939        )
940    }};
941}
942
943#[doc(hidden)]
944#[macro_export]
945macro_rules! log_structured_inner {
946    (@clear $($_:tt)*) => { () };
947    (@key $key:literal) => { $crate::gstr!($key) };
948    (@key $key:expr) => { std::convert::AsRef::<$crate::GStr>::as_ref(&$key) };
949    (@value $value:expr) => { std::convert::AsRef::<[u8]>::as_ref(&$value) };
950    (@value $format:expr $(,$arg:expr)+) => {
951        {
952            let mut builder = $crate::GStringBuilder::default();
953            if std::fmt::Write::write_fmt(&mut builder, format_args!($format, $($arg),+)).is_err() {
954                return;
955            }
956            builder.into_string()
957        }.as_str().as_bytes()
958    };
959}
960
961#[doc(alias = "g_log_structured_array")]
962#[inline]
963pub fn log_structured_array(log_level: LogLevel, fields: &[LogField<'_>]) {
964    unsafe {
965        ffi::g_log_structured_array(
966            log_level.into_glib(),
967            fields.as_ptr() as *const ffi::GLogField,
968            fields.len(),
969        )
970    }
971}
972
973#[doc(alias = "g_log_variant")]
974#[inline]
975pub fn log_variant(log_domain: Option<&str>, log_level: LogLevel, fields: &crate::Variant) {
976    unsafe {
977        ffi::g_log_variant(
978            log_domain.to_glib_none().0,
979            log_level.into_glib(),
980            fields.to_glib_none().0,
981        );
982    }
983}
984
985#[cfg(unix)]
986#[cfg_attr(docsrs, doc(cfg(unix)))]
987#[doc(alias = "g_log_writer_supports_color")]
988#[inline]
989pub fn log_writer_supports_color(output_fd: impl AsFd) -> bool {
990    unsafe {
991        from_glib(ffi::g_log_writer_supports_color(
992            output_fd.as_fd().as_raw_fd(),
993        ))
994    }
995}
996
997#[cfg(unix)]
998#[cfg_attr(docsrs, doc(cfg(unix)))]
999#[doc(alias = "g_log_writer_is_journald")]
1000#[inline]
1001pub fn log_writer_is_journald(output_fd: impl AsFd) -> bool {
1002    unsafe { from_glib(ffi::g_log_writer_is_journald(output_fd.as_fd().as_raw_fd())) }
1003}
1004
1005#[doc(alias = "g_log_writer_format_fields")]
1006#[inline]
1007pub fn log_writer_format_fields(
1008    log_level: LogLevel,
1009    fields: &[LogField<'_>],
1010    use_color: bool,
1011) -> GString {
1012    unsafe {
1013        from_glib_full(ffi::g_log_writer_format_fields(
1014            log_level.into_glib(),
1015            fields.as_ptr() as *const ffi::GLogField,
1016            fields.len(),
1017            use_color.into_glib(),
1018        ))
1019    }
1020}
1021
1022#[doc(alias = "g_log_writer_journald")]
1023#[inline]
1024pub fn log_writer_journald(log_level: LogLevel, fields: &[LogField<'_>]) -> LogWriterOutput {
1025    unsafe {
1026        from_glib(ffi::g_log_writer_journald(
1027            log_level.into_glib(),
1028            fields.as_ptr() as *const ffi::GLogField,
1029            fields.len(),
1030            std::ptr::null_mut(),
1031        ))
1032    }
1033}
1034
1035#[doc(alias = "g_log_writer_standard_streams")]
1036#[inline]
1037pub fn log_writer_standard_streams(
1038    log_level: LogLevel,
1039    fields: &[LogField<'_>],
1040) -> LogWriterOutput {
1041    unsafe {
1042        from_glib(ffi::g_log_writer_standard_streams(
1043            log_level.into_glib(),
1044            fields.as_ptr() as *const ffi::GLogField,
1045            fields.len(),
1046            std::ptr::null_mut(),
1047        ))
1048    }
1049}
1050
1051#[doc(alias = "g_log_writer_default")]
1052#[inline]
1053pub fn log_writer_default(log_level: LogLevel, fields: &[LogField<'_>]) -> LogWriterOutput {
1054    unsafe {
1055        from_glib(ffi::g_log_writer_default(
1056            log_level.into_glib(),
1057            fields.as_ptr() as *const ffi::GLogField,
1058            fields.len(),
1059            std::ptr::null_mut(),
1060        ))
1061    }
1062}
1063
1064// rustdoc-stripper-ignore-next
1065/// Sets whether GLib log functions output to stderr or stdout.
1066///
1067/// By default, log messages of level [`LogLevel::Info`] and [`LogLevel::Debug`] are sent to stdout,
1068/// and other log messages are sent to stderr. Passing `true` will send all messages to stderr.
1069///
1070/// # Safety
1071///
1072/// This function sets global state and is not thread-aware, as such it should be called before any
1073/// threads may try to use GLib logging.
1074#[cfg(feature = "v2_68")]
1075#[cfg_attr(docsrs, doc(cfg(feature = "v2_68")))]
1076#[doc(alias = "g_log_writer_default_set_use_stderr")]
1077#[inline]
1078pub unsafe fn log_writer_default_set_use_stderr(use_stderr: bool) {
1079    unsafe {
1080        ffi::g_log_writer_default_set_use_stderr(use_stderr.into_glib());
1081    }
1082}
1083
1084#[cfg(feature = "v2_68")]
1085#[cfg_attr(docsrs, doc(cfg(feature = "v2_68")))]
1086#[doc(alias = "g_log_writer_default_would_drop")]
1087#[inline]
1088pub fn log_writer_default_would_drop(log_level: LogLevel, log_domain: Option<&str>) -> bool {
1089    unsafe {
1090        from_glib(ffi::g_log_writer_default_would_drop(
1091            log_level.into_glib(),
1092            log_domain.to_glib_none().0,
1093        ))
1094    }
1095}
1096
1097#[cfg(feature = "v2_80")]
1098#[cfg_attr(docsrs, doc(cfg(feature = "v2_80")))]
1099#[doc(alias = "g_log_writer_default_set_debug_domains")]
1100#[inline]
1101pub fn log_writer_default_set_debug_domains(domains: &StrVRef) {
1102    unsafe { ffi::g_log_writer_default_set_debug_domains(domains.to_glib_none().0) }
1103}