From 6d654de3d5517ebfce26ac664cf236a06f2bcb72 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Tue, 4 Aug 2020 23:26:30 +0800 Subject: [PATCH] runtime: implement core_log --- src/runtime/src/kernel/api.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/runtime/src/kernel/api.rs b/src/runtime/src/kernel/api.rs index d769e609..524cfe85 100644 --- a/src/runtime/src/kernel/api.rs +++ b/src/runtime/src/kernel/api.rs @@ -1,7 +1,9 @@ use core::ffi::VaList; use core::ptr; +use core::str; use libc::{c_char, c_int, size_t}; use libm; +use log::{info, warn}; use alloc::vec; @@ -16,7 +18,20 @@ extern "C" { fn vsnprintf_(buffer: *mut c_char, count: size_t, format: *const c_char, va: VaList) -> c_int; } -pub unsafe extern fn rtio_log(fmt: *const c_char, mut args: ...) { +unsafe extern fn core_log(fmt: *const c_char, mut args: ...) { + let size = vsnprintf_(ptr::null_mut(), 0, fmt, args.as_va_list()) as usize; + let mut buf = vec![0; size + 1]; + vsnprintf_(buf.as_mut_ptr() as *mut i8, size + 1, fmt, args.as_va_list()); + match str::from_utf8(buf.as_slice()) { + Ok(s) => info!("kernel: {}", s), + Err(e) => { + info!("kernel: {}", (str::from_utf8(&buf.as_slice()[..e.valid_up_to()]).unwrap())); + warn!("kernel: invalid utf-8"); + } + } +} + +unsafe extern fn rtio_log(fmt: *const c_char, mut args: ...) { let size = vsnprintf_(ptr::null_mut(), 0, fmt, args.as_va_list()) as usize; let mut buf = vec![0; size + 1]; vsnprintf_(buf.as_mut_ptr(), size + 1, fmt, args.as_va_list()); @@ -68,6 +83,9 @@ pub fn resolve(required: &[u8]) -> Option { api!(rtio_input_timestamp = rtio::input_timestamp), api!(rtio_input_data = rtio::input_data), api!(rtio_input_timestamped_data = rtio::input_timestamped_data), + + // log + api!(core_log = core_log), api!(rtio_log = rtio_log), // rtio dma