vulcan_luaskills/runtime/
logging.rs1use std::sync::{Arc, OnceLock, RwLock};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum RuntimeLogLevel {
7 Info,
10 Warn,
13 Error,
16}
17
18#[derive(Debug, Clone)]
21pub struct RuntimeLogEvent {
22 pub level: RuntimeLogLevel,
25 pub message: String,
28}
29
30pub type RuntimeLogCallback = Arc<dyn Fn(&RuntimeLogEvent) + Send + Sync + 'static>;
33
34static RUNTIME_LOG_CALLBACK: OnceLock<RwLock<Option<RuntimeLogCallback>>> = OnceLock::new();
37
38fn runtime_log_callback() -> &'static RwLock<Option<RuntimeLogCallback>> {
41 RUNTIME_LOG_CALLBACK.get_or_init(|| RwLock::new(None))
42}
43
44pub fn set_log_callback(callback: Option<RuntimeLogCallback>) {
47 if let Ok(mut guard) = runtime_log_callback().write() {
48 *guard = callback;
49 }
50}
51
52pub fn emit(level: RuntimeLogLevel, message: impl Into<String>) {
55 let event = RuntimeLogEvent {
56 level,
57 message: message.into(),
58 };
59 let callback = runtime_log_callback()
60 .read()
61 .ok()
62 .and_then(|guard| guard.as_ref().cloned());
63 if let Some(callback) = callback {
64 callback(&event);
65 }
66}
67
68pub fn info(message: impl Into<String>) {
71 emit(RuntimeLogLevel::Info, message);
72}
73
74pub fn warn(message: impl Into<String>) {
77 emit(RuntimeLogLevel::Warn, message);
78}
79
80pub fn error(message: impl Into<String>) {
83 emit(RuntimeLogLevel::Error, message);
84}