#![allow(unused)]
use std::any::Any;
use chrysalis_rs::{
Extension, ExtensionRegistry, LogEntry, LogLevel,
error::Result,
};
struct TimestampFormatExtension {
enabled: bool,
format: String,
}
impl TimestampFormatExtension {
fn new(format: impl Into<String>) -> Self {
Self {
enabled: true,
format: format.into(),
}
}
fn format_timestamp(&self, timestamp: chrono::DateTime<chrono::Utc>) -> String {
timestamp.format(&self.format).to_string()
}
fn set_format(&mut self, format: impl Into<String>) {
self.format = format.into();
}
}
impl Extension for TimestampFormatExtension {
fn name(&self) -> &str {
"timestamp_formatter"
}
fn initialize(&mut self) -> Result<()> {
chrono::Utc::now().format(&self.format);
Ok(())
}
fn shutdown(&mut self) -> Result<()> {
Ok(())
}
fn is_enabled(&self) -> bool {
self.enabled
}
fn set_enabled(&mut self, enabled: bool) {
self.enabled = enabled;
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
fn main() -> Result<()> {
let mut registry = ExtensionRegistry::new();
let timestamp_ext = TimestampFormatExtension::new("%Y-%m-%d %H:%M:%S");
registry.register(timestamp_ext)?;
registry.initialize_all()?;
let entry = LogEntry::new("Testing extensions", LogLevel::Info);
if let Some(ext) = registry.get_by_type::<TimestampFormatExtension>() {
if ext.is_enabled() {
let formatted = ext.format_timestamp(entry.metadata.timestamp);
println!("Log time: {}", formatted);
}
}
if let Some(ext) = registry.get_mut("timestamp_formatter") {
if let Some(ts_ext) = ext.as_any_mut().downcast_mut::<TimestampFormatExtension>() {
ts_ext.set_format("%H:%M:%S");
println!("Updated format!");
}
}
if let Some(ext) = registry.get_by_type::<TimestampFormatExtension>() {
let formatted = ext.format_timestamp(entry.metadata.timestamp);
println!("Log time (new format): {}", formatted);
}
registry.shutdown_all()?;
Ok(())
}