gdnative_core/
log.rs

1//! Functions for using the engine's logging system in the editor.
2use std::ffi::CStr;
3use std::fmt::{self, Display};
4
5// Collection of macros accessing the Godot engine log/print functionality
6pub use crate::{godot_dbg, godot_error, godot_print, godot_site, godot_warn};
7
8use crate::core_types::GodotString;
9use crate::private;
10
11/// Value representing a call site for errors and warnings. Can be constructed
12/// using the [`godot_site`] macro, or manually.
13#[derive(Copy, Clone, Debug)]
14pub struct Site<'a> {
15    file: &'a CStr,
16    func: &'a CStr,
17    line: u32,
18}
19
20impl<'a> Site<'a> {
21    /// Construct a new `Site` value using values provided manually.
22    #[inline]
23    pub const fn new(file: &'a CStr, func: &'a CStr, line: u32) -> Self {
24        Site { file, func, line }
25    }
26}
27
28impl<'a> Default for Site<'a> {
29    #[inline]
30    fn default() -> Self {
31        let unset = unsafe { CStr::from_bytes_with_nul_unchecked(b"<unset>\0") };
32        Site::new(unset, unset, 0)
33    }
34}
35
36impl<'a> Display for Site<'a> {
37    #[inline]
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        write!(
40            f,
41            "file {}, {}, line {}",
42            self.file.to_string_lossy(),
43            self.func.to_string_lossy(),
44            self.line
45        )
46    }
47}
48
49/// Print a message to the Godot console.
50///
51/// Typically, you would use this through the [`godot_print`] macro.
52///
53/// # Panics
54///
55/// If the API isn't initialized.
56#[inline]
57pub fn print<S: Display>(msg: S) {
58    unsafe {
59        let msg = GodotString::from_str(msg.to_string());
60        (private::get_api().godot_print)(&msg.to_sys() as *const _);
61    }
62}
63
64/// Print a warning to the Godot console.
65///
66/// Typically, you would use this through the [`godot_warn`] macro.
67///
68/// # Panics
69///
70/// If the API isn't initialized, or if the message contains any NUL-bytes.
71#[inline]
72pub fn warn<S: Display>(site: Site<'_>, msg: S) {
73    let msg = msg.to_string();
74    let msg = ::std::ffi::CString::new(msg).unwrap();
75
76    unsafe {
77        (private::get_api().godot_print_warning)(
78            msg.as_ptr(),
79            site.func.as_ptr(),
80            site.file.as_ptr(),
81            site.line as libc::c_int,
82        );
83    }
84}
85
86/// Print an error to the Godot console.
87///
88/// Typically, you would use this through the [`godot_error`] macro.
89///
90/// # Panics
91///
92/// If the API isn't initialized, or if the message contains any NUL-bytes.
93#[inline]
94pub fn error<S: Display>(site: Site<'_>, msg: S) {
95    let msg = msg.to_string();
96    let msg = ::std::ffi::CString::new(msg).unwrap();
97
98    unsafe {
99        (private::get_api().godot_print_error)(
100            msg.as_ptr(),
101            site.func.as_ptr(),
102            site.file.as_ptr(),
103            site.line as libc::c_int,
104        );
105    }
106}