api and log refactor
This commit is contained in:
@@ -32,22 +32,12 @@ pub fn setup() -> () {
|
||||
SETUP_ONCE.call_once(|| {});
|
||||
}
|
||||
|
||||
// Log filtering
|
||||
fn logfilter<T: AsRef<str>, V: AsRef<[T]>>(metadata: &Metadata, ignore_list: V) -> bool {
|
||||
// Skip filtered targets
|
||||
!match (metadata.target(), ignore_list.as_ref()) {
|
||||
(path, ignore) if !ignore.is_empty() => {
|
||||
// Check that the module path does not match any ignore filters
|
||||
ignore.iter().any(|v| path.starts_with(v.as_ref()))
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
// API Singleton
|
||||
lazy_static! {
|
||||
static ref VEILID_API: SendWrapper<RefCell<Option<veilid_core::VeilidAPI>>> =
|
||||
SendWrapper::new(RefCell::new(None));
|
||||
static ref FILTERS: SendWrapper<RefCell<BTreeMap<&'static str, veilid_core::VeilidLayerFilter>>> =
|
||||
SendWrapper::new(RefCell::new(BTreeMap::new()));
|
||||
}
|
||||
|
||||
fn get_veilid_api() -> Result<veilid_core::VeilidAPI, veilid_core::VeilidAPIError> {
|
||||
@@ -123,9 +113,16 @@ pub struct VeilidWASMConfigLoggingPerformance {
|
||||
pub logs_in_console: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct VeilidWASMConfigLoggingAPI {
|
||||
pub enabled: bool,
|
||||
pub level: veilid_core::VeilidLogLevel,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct VeilidWASMConfigLogging {
|
||||
pub performance: VeilidWASMConfigLoggingPerformance,
|
||||
pub api: VeilidWASMConfigLoggingAPI,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
@@ -146,48 +143,61 @@ pub fn configure_veilid_platform(platform_config: String) {
|
||||
.expect("failed to deserialize plaform config json");
|
||||
|
||||
// Set up subscriber and layers
|
||||
let mut ignore_list = Vec::<String>::new();
|
||||
for ig in veilid_core::DEFAULT_LOG_IGNORE_LIST {
|
||||
ignore_list.push(ig.to_owned());
|
||||
}
|
||||
|
||||
let subscriber = Registry::default();
|
||||
let mut layers = Vec::new();
|
||||
let handles = (*FILTER_RELOAD_HANDLES).borrow_mut();
|
||||
|
||||
// Performance logger
|
||||
let subscriber = subscriber.with(if platform_config.logging.performance.enabled {
|
||||
let performance_max_log_level =
|
||||
platform_config.logging.performance.level.to_tracing_level();
|
||||
|
||||
let ignore_list = ignore_list.clone();
|
||||
Some(
|
||||
WASMLayer::new(
|
||||
WASMLayerConfigBuilder::new()
|
||||
.set_report_logs_in_timings(platform_config.logging.performance.logs_in_timings)
|
||||
.set_console_config(if platform_config.logging.performance.logs_in_console {
|
||||
ConsoleConfig::ReportWithConsoleColor
|
||||
} else {
|
||||
ConsoleConfig::NoReporting
|
||||
})
|
||||
.set_max_level(performance_max_log_level)
|
||||
.build(),
|
||||
)
|
||||
.with_filter(filter::FilterFn::new(move |metadata| {
|
||||
logfilter(metadata, &ignore_list)
|
||||
})),
|
||||
if platform_config.logging.performance.enabled {
|
||||
let filter =
|
||||
veilid_core::VeilidLayerFilter::new(platform_config.logging.performance.level, None);
|
||||
let layer = WASMLayer::new(
|
||||
WASMLayerConfigBuilder::new()
|
||||
.set_report_logs_in_timings(platform_config.logging.performance.logs_in_timings)
|
||||
.set_console_config(if platform_config.logging.performance.logs_in_console {
|
||||
ConsoleConfig::ReportWithConsoleColor
|
||||
} else {
|
||||
ConsoleConfig::NoReporting
|
||||
})
|
||||
.build(),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
});
|
||||
.with_filter(filter.clone());
|
||||
handles.insert("performance", filter);
|
||||
layers.push(layer.boxed());
|
||||
};
|
||||
|
||||
// API logger (always add layer, startup will init this if it is enabled in settings)
|
||||
let subscriber = subscriber.with(veilid_core::ApiTracingLayer::get());
|
||||
// API logger
|
||||
if platform_config.logging.api.enabled {
|
||||
let filter = veilid_core::VeilidLayerFilter::new(platform_config.logging.api.level, None);
|
||||
let layer = veilid_core::ApiTracingLayer::get().with_filter(filter.clone());
|
||||
handles.insert("api", filter);
|
||||
layers.push(layer.boxed());
|
||||
}
|
||||
|
||||
let subscriber = subscriber.with(layers);
|
||||
subscriber
|
||||
.try_init()
|
||||
.map_err(|e| format!("failed to initialize logging: {}", e))
|
||||
.expect("failed to initalize WASM platform");
|
||||
}
|
||||
|
||||
#[wasm_bindgen()]
|
||||
pub fn change_log_level(layer: String, log_level: String) {
|
||||
let layer = if layer == "all" { "".to_owned() } else { layer };
|
||||
let log_level: veilid_core::VeilidConfigLogLevel = deserialize_json(&log_level).unwrap();
|
||||
let filters = (*FILTERS).borrow();
|
||||
if layer.is_empty() {
|
||||
// Change all layers
|
||||
for f in filters.values() {
|
||||
f.set_max_level(log_level);
|
||||
}
|
||||
} else {
|
||||
// Change a specific layer
|
||||
let f = filters.get(layer.as_str()).unwrap();
|
||||
f.set_max_level(log_level);
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen()]
|
||||
pub fn startup_veilid_core(update_callback: Function, json_config: String) -> Promise {
|
||||
wrap_api_future(async move {
|
||||
@@ -221,17 +231,6 @@ pub fn get_veilid_state() -> Promise {
|
||||
})
|
||||
}
|
||||
|
||||
#[wasm_bindgen()]
|
||||
pub fn change_api_log_level(log_level: String) -> Promise {
|
||||
wrap_api_future(async move {
|
||||
let log_level: veilid_core::VeilidConfigLogLevel = deserialize_json(&log_level)?;
|
||||
//let veilid_api = get_veilid_api()?;
|
||||
//veilid_api.change_api_log_level(log_level).await;
|
||||
veilid_core::ApiTracingLayer::change_api_log_level(log_level.to_veilid_log_level());
|
||||
APIRESULT_UNDEFINED
|
||||
})
|
||||
}
|
||||
|
||||
#[wasm_bindgen()]
|
||||
pub fn shutdown_veilid_core() -> Promise {
|
||||
wrap_api_future(async move {
|
||||
|
Reference in New Issue
Block a user