1use std::ffi::CStr;
3use std::fmt::{self, Display};
4
5pub use crate::{godot_dbg, godot_error, godot_print, godot_site, godot_warn};
7
8use crate::core_types::GodotString;
9use crate::private;
10
11#[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 #[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#[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#[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#[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}