use crate::ErrorManager::{ErrorManager, DlmErrorType};
use crate::Compiler::Core::Config::DebugMode;
pub struct DLMModuleBase {
error_manager: ErrorManager,
module_name: String,
priority: i32,
is_debug_enabled: bool,
is_verbose_enabled: bool,
}
impl DLMModuleBase {
pub fn new(module_name: impl Into<String>, priority: i32) -> Self {
Self::new_with_error_manager(module_name,priority,ErrorManager::get_shared_instance())
}
pub fn new_with_error_manager(module_name: impl Into<String>, priority: i32, error_manager: ErrorManager,
) -> Self {
let debug_mode = error_manager.get_debug_mode();
let (is_debug_enabled, is_verbose_enabled) = match debug_mode {
DebugMode::Off => (false, false),
DebugMode::Regular => (true, false),
DebugMode::Verbose => (true, true),
};
DLMModuleBase {
error_manager,
module_name: module_name.into(),
priority,
is_debug_enabled,
is_verbose_enabled,
}
}
pub fn module_name(&self) -> &str {
&self.module_name
}
pub fn priority(&self) -> i32 {
self.priority
}
pub fn is_debug_enabled(&self) -> bool {
self.is_debug_enabled
}
pub fn is_verbose_enabled(&self) -> bool {
self.is_verbose_enabled
}
#[inline]
pub fn log_info(&self, message: &str) {
self.error_manager.log_info(&format!("[{}] {}", self.module_name, message));
}
#[inline]
pub fn log_debug(&self, message: &str) {
if self.is_debug_enabled {
self.error_manager.log_debug(&format!("[{}] {}", self.module_name, message));
}
}
#[inline]
pub fn log_verbose(&self, message: &str) {
if self.is_verbose_enabled {
self.error_manager.log_debug(&format!("[{}] {}", self.module_name, message));
}
}
#[inline]
pub fn log_warning(&self, message: &str) {
self.error_manager.log_warning(&format!("[{}] {}", self.module_name, message));
}
#[inline]
pub fn log_error(&self, message: &str) {
self.error_manager.add_dlm_error(
DlmErrorType::ModuleExecutionFailed,
message.to_string(),
Some(self.module_name.clone()),
None,
None,
crate::ErrorManager::ErrorSeverity::Error,
);
}
pub fn error_manager(&self) -> &ErrorManager {
&self.error_manager
}
}