smplx_sdk/global.rs
1use std::sync::OnceLock;
2
3use crate::program::TrackerLogLevel;
4
5/// A structure to represent the global configuration settings for the application.
6#[derive(Clone, Copy, Debug)]
7pub struct GlobalConfig {
8 log_level: TrackerLogLevel,
9}
10
11impl Default for GlobalConfig {
12 fn default() -> Self {
13 Self {
14 log_level: TrackerLogLevel::Debug,
15 }
16 }
17}
18
19static GLOBAL_CONFIG: OnceLock<GlobalConfig> = OnceLock::new();
20
21/// Sets the global configuration for the SDK.
22///
23/// This function allows setting a global configuration which includes
24/// the logging level for `simplicity` contracts execution.
25/// It must be called exactly once during the application's lifetime.
26///
27/// # Errors
28/// Returns an error containing the newly created `GlobalConfig` if the global configuration has already been initialized.
29pub fn set_global_config(log_level: TrackerLogLevel) -> Result<(), GlobalConfig> {
30 GLOBAL_CONFIG.set(GlobalConfig { log_level })
31}
32
33/// Returns the default log level if `GLOBAL_CONFIG` is not initialized
34pub fn get_log_level() -> TrackerLogLevel {
35 GLOBAL_CONFIG
36 .get()
37 .map_or(GlobalConfig::default().log_level, |config| config.log_level)
38}