139 lines
3.5 KiB
Rust
139 lines
3.5 KiB
Rust
use crate::pxl8::{pxl8_log, pxl8_log_init};
|
|
use core::ffi::c_char;
|
|
|
|
static mut G_LOG: pxl8_log = pxl8_log {
|
|
handler: None,
|
|
level: crate::pxl8::pxl8_log_level::PXL8_LOG_LEVEL_DEBUG,
|
|
};
|
|
|
|
pub fn init() {
|
|
unsafe {
|
|
pxl8_log_init(&raw mut G_LOG);
|
|
}
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! pxl8_debug {
|
|
($($arg:tt)*) => {{
|
|
use ::core::fmt::Write;
|
|
let mut buf = $crate::log::LogBuffer::new();
|
|
let _ = ::core::write!(&mut buf, $($arg)*);
|
|
buf.push(0);
|
|
unsafe {
|
|
$crate::pxl8::pxl8_log_write_debug(
|
|
concat!(file!(), "\0").as_ptr() as *const ::core::ffi::c_char,
|
|
line!() as ::core::ffi::c_int,
|
|
c"%s".as_ptr() as *const ::core::ffi::c_char,
|
|
buf.as_ptr(),
|
|
);
|
|
}
|
|
}};
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! pxl8_error {
|
|
($($arg:tt)*) => {{
|
|
use ::core::fmt::Write;
|
|
let mut buf = $crate::log::LogBuffer::new();
|
|
let _ = ::core::write!(&mut buf, $($arg)*);
|
|
buf.push(0);
|
|
unsafe {
|
|
$crate::pxl8::pxl8_log_write_error(
|
|
concat!(file!(), "\0").as_ptr() as *const ::core::ffi::c_char,
|
|
line!() as ::core::ffi::c_int,
|
|
c"%s".as_ptr() as *const ::core::ffi::c_char,
|
|
buf.as_ptr(),
|
|
);
|
|
}
|
|
}};
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! pxl8_info {
|
|
($($arg:tt)*) => {{
|
|
use ::core::fmt::Write;
|
|
let mut buf = $crate::log::LogBuffer::new();
|
|
let _ = ::core::write!(&mut buf, $($arg)*);
|
|
buf.push(0);
|
|
unsafe {
|
|
$crate::pxl8::pxl8_log_write_info(
|
|
c"%s".as_ptr() as *const ::core::ffi::c_char,
|
|
buf.as_ptr(),
|
|
);
|
|
}
|
|
}};
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! pxl8_trace {
|
|
($($arg:tt)*) => {{
|
|
use ::core::fmt::Write;
|
|
let mut buf = $crate::log::LogBuffer::new();
|
|
let _ = ::core::write!(&mut buf, $($arg)*);
|
|
buf.push(0);
|
|
unsafe {
|
|
$crate::pxl8::pxl8_log_write_trace(
|
|
concat!(file!(), "\0").as_ptr() as *const ::core::ffi::c_char,
|
|
line!() as ::core::ffi::c_int,
|
|
c"%s".as_ptr() as *const ::core::ffi::c_char,
|
|
buf.as_ptr(),
|
|
);
|
|
}
|
|
}};
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! pxl8_warn {
|
|
($($arg:tt)*) => {{
|
|
use ::core::fmt::Write;
|
|
let mut buf = $crate::log::LogBuffer::new();
|
|
let _ = ::core::write!(&mut buf, $($arg)*);
|
|
buf.push(0);
|
|
unsafe {
|
|
$crate::pxl8::pxl8_log_write_warn(
|
|
concat!(file!(), "\0").as_ptr() as *const ::core::ffi::c_char,
|
|
line!() as ::core::ffi::c_int,
|
|
c"%s".as_ptr() as *const ::core::ffi::c_char,
|
|
buf.as_ptr(),
|
|
);
|
|
}
|
|
}};
|
|
}
|
|
|
|
pub struct LogBuffer {
|
|
buf: [u8; 1024],
|
|
pos: usize,
|
|
}
|
|
|
|
impl LogBuffer {
|
|
pub const fn new() -> Self {
|
|
Self {
|
|
buf: [0; 1024],
|
|
pos: 0,
|
|
}
|
|
}
|
|
|
|
pub fn as_ptr(&self) -> *const c_char {
|
|
self.buf.as_ptr() as *const c_char
|
|
}
|
|
|
|
pub fn push(&mut self, b: u8) {
|
|
if self.pos < self.buf.len() {
|
|
self.buf[self.pos] = b;
|
|
self.pos += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
impl core::fmt::Write for LogBuffer {
|
|
fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
|
for b in s.bytes() {
|
|
if self.pos >= self.buf.len() - 1 {
|
|
break;
|
|
}
|
|
self.buf[self.pos] = b;
|
|
self.pos += 1;
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|