refactor logger

This commit is contained in:
2026-02-22 05:43:26 -07:00
parent 8877c61aed
commit 9649961580
6 changed files with 121 additions and 121 deletions

View File

@@ -1,4 +1,4 @@
use crate::log::LogLevel; use crate::log::{LOG_LEVEL, LogLevel};
use clap::{Arg, ArgAction, ArgGroup, Command}; use clap::{Arg, ArgAction, ArgGroup, Command};
use regex::Regex; use regex::Regex;
use std::path::PathBuf; use std::path::PathBuf;
@@ -22,17 +22,21 @@ impl Args {
/// loads command line arguments and environment variables /// loads command line arguments and environment variables
pub fn parse() -> Args { pub fn parse() -> Args {
let matches: clap::ArgMatches = Self::parser().get_matches(); let matches: clap::ArgMatches = Self::parser().get_matches();
let raw_devices = matches.get_one::<String>("devices").unwrap();
let devices = Regex::new(matches.get_one::<String>("devices").unwrap())
.expect("Invalid regex pattern provided for `devices`");
let mut loglevel = LogLevel::Info;
if matches.get_flag("debug") {
loglevel = LogLevel::Debug;
} else if matches.get_flag("verbose") {
loglevel = LogLevel::Verbose;
}
LOG_LEVEL.set(loglevel).ok();
Args { Args {
devices: Regex::new(&raw_devices) devices,
.expect("Invalid regex pattern provided for `devices`"), loglevel,
loglevel: if matches.get_flag("debug") {
LogLevel::Debug
} else if matches.get_flag("verbose") {
LogLevel::Verbose
} else {
LogLevel::Info
},
env: EnvVars::parse(), env: EnvVars::parse(),
} }
} }

View File

@@ -5,6 +5,6 @@ pub mod spec;
pub mod tmpl; pub mod tmpl;
pub use cli::Args; pub use cli::Args;
pub use log::LogLevel; pub use log::{LogLevel, log_level};
pub use render::DeviceConfigBundle; pub use render::DeviceConfigBundle;
pub use spec::Specification; pub use spec::Specification;

View File

@@ -1,6 +1,7 @@
#![allow(dead_code)] #![allow(dead_code)]
use std::sync::OnceLock;
#[derive(Debug, PartialEq, Clone, Copy)] #[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
pub enum LogLevel { pub enum LogLevel {
Debug, Debug,
Verbose, Verbose,
@@ -10,17 +11,10 @@ pub enum LogLevel {
None, None,
} }
impl LogLevel { pub static LOG_LEVEL: OnceLock<LogLevel> = OnceLock::new();
pub fn value(&self) -> u8 {
match self { pub fn log_level() -> LogLevel {
LogLevel::Debug => u8::MIN, *LOG_LEVEL.get().unwrap_or(&LogLevel::Info)
LogLevel::Verbose => u8::from(30),
LogLevel::Info => u8::from(60),
LogLevel::Warning => u8::from(90),
LogLevel::Critical => u8::from(120),
LogLevel::None => u8::MAX,
}
}
} }
impl std::fmt::Display for LogLevel { impl std::fmt::Display for LogLevel {
@@ -31,8 +25,8 @@ impl std::fmt::Display for LogLevel {
#[macro_export] #[macro_export]
macro_rules! info { macro_rules! info {
($current_level:expr, $($msg:expr),*) => { ($($msg:expr),*) => {
if LogLevel::Info.value() >= $current_level.value() { if LogLevel::Info >= $crate::log_level() {
println!($($msg),*); println!($($msg),*);
} }
}; };
@@ -40,8 +34,8 @@ macro_rules! info {
#[macro_export] #[macro_export]
macro_rules! verb { macro_rules! verb {
($current_level:expr, $($msg:expr),*) => { ($($msg:expr),*) => {
if LogLevel::Verbose.value() >= $current_level.value() { if LogLevel::Verbose >= $crate::log_level() {
println!($($msg),*); println!($($msg),*);
} }
}; };
@@ -49,8 +43,8 @@ macro_rules! verb {
#[macro_export] #[macro_export]
macro_rules! dbug { macro_rules! dbug {
($current_level:expr, $($msg:expr),*) => { ($($msg:expr),*) => {
if LogLevel::Debug.value() >= $current_level.value() { if LogLevel::Debug >= $crate::log_level() {
println!($($msg),*); println!($($msg),*);
} }
}; };
@@ -58,8 +52,8 @@ macro_rules! dbug {
#[macro_export] #[macro_export]
macro_rules! warn { macro_rules! warn {
($current_level:expr, $($msg:expr),*) => { ($($msg:expr),*) => {
if LogLevel::Warning.value() >= $current_level.value() { if LogLevel::Warning >= $crate::log_level() {
eprintln!($($msg),*); eprintln!($($msg),*);
} }
}; };
@@ -67,8 +61,8 @@ macro_rules! warn {
#[macro_export] #[macro_export]
macro_rules! crit { macro_rules! crit {
($current_level:expr, $($msg:expr),*) => { ($($msg:expr),*) => {
if LogLevel::Critical.value() >= $current_level.value() { if LogLevel::Critical >= $crate::log_level() {
eprintln!($($msg),*); eprintln!($($msg),*);
} }
}; };

View File

@@ -1,7 +1,8 @@
use crate::log::LogLevel; use crate::log::LogLevel;
use crate::spec::Specification; use crate::spec::Specification;
use crate::tmpl; use crate::tmpl;
use crate::{dbug, info, verb}; use crate::{crit, dbug, info, verb};
use std::error::Error;
use std::io::Write; use std::io::Write;
pub struct DeviceConfigBundle { pub struct DeviceConfigBundle {
@@ -23,50 +24,49 @@ pub struct Configuration {
impl DeviceConfigBundle { impl DeviceConfigBundle {
pub fn from_spec(spec: Specification) -> Result<Self, Box<dyn std::error::Error>> { pub fn from_spec(spec: Specification) -> Result<Self, Box<dyn std::error::Error>> {
let mut context = Self::get_context(&spec); let mut context = Self::merge_context(&spec);
let hostname = Self::get_hostname(&context, &spec); let hostname = Self::get_hostname(&context, &spec);
info!(&spec.loglevel, "Rendering {}", hostname);
let base_dir = std::path::Path::new("./tmpl").join(spec.get_layer()); let base_dir = std::path::Path::new("./tmpl").join(spec.get_layer());
info!("Loading templates for '{hostname}':");
let structure_path = base_dir.join("structure.yaml"); let structure_path = base_dir.join("structure.yaml");
let structure = tmpl::Structure::from_file(&structure_path) let structure = tmpl::Structure::from_file(&structure_path)
.map_err(|e| format!("Failed to parse {}: {}", structure_path.display(), e))?; .map_err(|e| format!("Failed to parse {}: {e}", structure_path.display()))?;
let mut renderer = tera::Tera::default(); let mut renderer = tera::Tera::default();
renderer.add_raw_templates(structure.load_template_data(&base_dir, spec.loglevel))?; renderer.add_raw_templates(structure.load_template_data(base_dir))?;
dbug!(&spec.loglevel, "Processing templates for {}", hostname); dbug!("Processing templates for {hostname}");
let mut config_variants = Vec::new(); let mut failures = false;
let mut configs = Vec::new();
for variant in &structure.variations { for variant in &structure.variations {
context.insert(variant, &true); context.insert(variant, &true);
let mut configs = Vec::new(); let mut cfgs = Vec::new();
for template_name in &structure.files { for template_name in &structure.files {
dbug!(&spec.loglevel, " | {}.{}", &template_name, variant); dbug!(" | {template_name}.{variant}");
if !renderer.get_template_names().any(|n| n == template_name) {
continue;
}
match renderer.render(template_name, &context) { match renderer.render(template_name, &context) {
Ok(data) => configs.push(Configuration { Ok(data) => cfgs.push(Configuration::new(template_name, data)),
name: template_name.clone(), Err(e) => {
data, crit!("[!] {}", Error::source(&e).unwrap_or_else(|| &e));
}), renderer.add_raw_template(template_name, "")?;
Err(e) => Self::log_tera_error(template_name, &e), failures = true;
}
} }
} }
config_variants.push(ConfigVariant { configs.push(ConfigVariant::new(variant, cfgs));
name: String::from(variant),
configs,
});
context.remove(variant); context.remove(variant);
} }
Ok(Self { match failures {
hostname, true => Err(Box::<dyn Error>::from("Rendering failed.")),
configs: config_variants, false => Ok(Self {
spec, hostname,
platform: structure.platform, configs,
}) spec,
platform: structure.platform,
}),
}
} }
fn get_hostname(context: &tera::Context, spec: &Specification) -> String { fn get_hostname(context: &tera::Context, spec: &Specification) -> String {
@@ -77,7 +77,7 @@ impl DeviceConfigBundle {
.unwrap_or_else(|| spec.components.get_hostname()) .unwrap_or_else(|| spec.components.get_hostname())
} }
fn get_context(spec: &Specification) -> tera::Context { fn merge_context(spec: &Specification) -> tera::Context {
let mut context = tera::Context::new(); let mut context = tera::Context::new();
if let Some(data_map) = spec.compiled.get("data").and_then(|v| v.as_object()) { if let Some(data_map) = spec.compiled.get("data").and_then(|v| v.as_object()) {
for (key, val) in data_map { for (key, val) in data_map {
@@ -87,18 +87,8 @@ impl DeviceConfigBundle {
context context
} }
fn log_tera_error(name: &str, e: &tera::Error) {
let mut chain = e.to_string();
let mut source = std::error::Error::source(e);
while let Some(s) = source {
chain.push_str(&format!(" -> {}", s));
source = s.source();
}
eprintln!("[tera] {}: {}", name, chain);
}
pub fn output_artifacts(self: Self) { pub fn output_artifacts(self: Self) {
info!(&self.spec.loglevel, "Writing Output:"); info!("Writing Output:");
let device_outpath = std::path::Path::new(&self.spec.outpath).join(self.hostname); let device_outpath = std::path::Path::new(&self.spec.outpath).join(self.hostname);
if device_outpath.exists() { if device_outpath.exists() {
@@ -106,12 +96,12 @@ impl DeviceConfigBundle {
} }
std::fs::create_dir_all(&device_outpath).ok(); std::fs::create_dir_all(&device_outpath).ok();
for variant_configs in &self.configs { for variant_configs in self.configs {
let out_path = device_outpath.join(&variant_configs.name); let out_path = device_outpath.join(&variant_configs.name);
std::fs::create_dir_all(&out_path).ok(); std::fs::create_dir_all(&out_path).ok();
let merged_config_path = let merged_config_path =
device_outpath.join(format!("all.{}.{}", variant_configs.name, &self.platform)); device_outpath.join(format!("all.{}.{}", variant_configs.name, self.platform));
let mut all_file: std::fs::File = std::fs::OpenOptions::new() let mut all_file: std::fs::File = std::fs::OpenOptions::new()
.create(true) .create(true)
.append(true) .append(true)
@@ -119,17 +109,16 @@ impl DeviceConfigBundle {
.expect("unable to open"); .expect("unable to open");
for config in &variant_configs.configs { for config in &variant_configs.configs {
let config_outpath = out_path.join(format!("{}.{}", &config.name, &self.platform)); let config_outpath = out_path.join(format!("{}.{}", config.name, self.platform));
std::fs::create_dir_all(&config_outpath).ok(); verb!(" | {}", &config_outpath.display());
verb!(&self.spec.loglevel, " | {}", &config_outpath.display());
std::fs::write(&config_outpath, &config.data).ok(); std::fs::write(&config_outpath, &config.data).ok();
all_file.write_all(&config.data.as_bytes()).ok(); all_file.write_all(&config.data.as_bytes()).ok();
} }
info!(&self.spec.loglevel, " | {} ", &merged_config_path.display()); info!(" | {} ", &merged_config_path.display());
} }
let compiled_spec_path = device_outpath.join("context.yaml"); let compiled_spec_path = device_outpath.join("context.yaml");
verb!(&self.spec.loglevel, " | {}", &compiled_spec_path.display()); verb!(" | {}", &compiled_spec_path.display());
std::fs::write( std::fs::write(
&compiled_spec_path, &compiled_spec_path,
yaml_serde::to_string(&self.spec.compiled).unwrap(), yaml_serde::to_string(&self.spec.compiled).unwrap(),
@@ -137,3 +126,21 @@ impl DeviceConfigBundle {
.ok(); .ok();
} }
} }
impl ConfigVariant {
pub fn new(name: &str, configs: Vec<Configuration>) -> Self {
Self {
name: String::from(name),
configs,
}
}
}
impl Configuration {
pub fn new(name: &str, data: String) -> Self {
Self {
name: String::from(name),
data,
}
}
}

View File

@@ -10,10 +10,20 @@ use yaml_serde;
pub struct Specification { pub struct Specification {
pub compiled: Value, pub compiled: Value,
pub components: Components, pub components: Components,
pub loglevel: LogLevel,
pub outpath: PathBuf, pub outpath: PathBuf,
} }
trait Pipe: Sized {
fn tap<F: FnOnce(&Self)>(self, f: F) -> Self {
f(&self);
self
}
fn pipe<U, F: FnOnce(Self) -> U>(self, f: F) -> U {
f(self)
}
}
impl<T> Pipe for T {}
impl Specification { impl Specification {
pub fn build( pub fn build(
partitional: PathBuf, partitional: PathBuf,
@@ -21,7 +31,6 @@ impl Specification {
common: PathBuf, common: PathBuf,
zonal: PathBuf, zonal: PathBuf,
device: PathBuf, device: PathBuf,
loglevel: LogLevel,
outpath: PathBuf, outpath: PathBuf,
) -> Result<Self, Box<dyn std::error::Error>> { ) -> Result<Self, Box<dyn std::error::Error>> {
let json_values: Vec<Value> = [&partitional, &regional, &common, &zonal, &device] let json_values: Vec<Value> = [&partitional, &regional, &common, &zonal, &device]
@@ -48,7 +57,6 @@ impl Specification {
zonal, zonal,
device, device,
}, },
loglevel,
outpath, outpath,
}) })
} }
@@ -68,30 +76,26 @@ impl Specification {
/// compiles specifications from regex and paths provided by Args /// compiles specifications from regex and paths provided by Args
pub fn compile(args: &Args) -> Vec<Specification> { pub fn compile(args: &Args) -> Vec<Specification> {
let spec_list: Vec<PathBuf> = Self::filter_specs( Self::get_specs(&args.env.spec_path)
&args.devices, .pipe(|p| {
Self::get_specs(&args.env.spec_path, args.loglevel), info!(" Skyforge found {} renderable devices", p.len());
); Self::filter_specs(&args.devices, p)
info!( })
&args.loglevel, .tap(|p| info!("Matched {} devices against '{}'", p.len(), &args.devices))
"Matched {} devices against '{}'",
&spec_list.len(),
&args.devices
);
spec_list
.into_iter() .into_iter()
.map(|spec| { .map(|spec| {
verb!(&args.loglevel, " | {}", spec.display()); verb!(" | {}", spec.display());
let common = Self::get_common(&spec);
let regional = Self::get_regional(&spec);
Self::build( Self::build(
Self::get_partitional(&Self::get_common(&spec), &Self::get_regional(&spec)), Self::get_partitional(&common, &regional),
Self::get_regional(&spec), regional,
Self::get_common(&spec), common,
Self::get_zonal(&spec), Self::get_zonal(&spec),
spec, spec,
args.loglevel, args.env.out_path.clone(),
std::path::PathBuf::from(&args.env.out_path),
) )
.unwrap_or_else(|e| panic!("failed to build Specification: {}", e)) .expect("failed to build Specification")
}) })
.collect() .collect()
} }
@@ -156,7 +160,7 @@ impl Specification {
.collect() .collect()
} }
fn get_specs(spec_path: &PathBuf, dbg: LogLevel) -> Vec<PathBuf> { fn get_specs(spec_path: &PathBuf) -> Vec<PathBuf> {
let mut result = Vec::new(); let mut result = Vec::new();
let root = spec_path.as_path(); let root = spec_path.as_path();
for entry in WalkDir::new(root).into_iter().filter_map(|e| e.ok()) { for entry in WalkDir::new(root).into_iter().filter_map(|e| e.ok()) {
@@ -167,12 +171,6 @@ impl Specification {
} }
} }
} }
info!(
dbg,
"Skyforge found {} renderable devices in {}",
result.len(),
std::env::current_dir().unwrap().display()
);
result result
} }
} }
@@ -181,8 +179,8 @@ impl std::fmt::Display for Specification {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!( write!(
f, f,
"Compiled Specification: {}\nLogLevel: {}\nComponents: {}", "Compiled Specification: {}\nComponents: {}",
self.compiled, self.loglevel, self.components, self.compiled, self.components,
) )
} }
} }

View File

@@ -1,5 +1,7 @@
use crate::{LogLevel, crit, info}; use crate::{LogLevel, crit, info};
use serde::Deserialize; use serde::Deserialize;
use std::error::Error;
use std::path::PathBuf;
use yaml_serde; use yaml_serde;
#[derive(Deserialize)] #[derive(Deserialize)]
@@ -10,28 +12,23 @@ pub struct Structure {
} }
impl Structure { impl Structure {
pub fn from_file(path: &std::path::Path) -> Result<Self, Box<dyn std::error::Error>> { pub fn from_file(path: &PathBuf) -> Result<Self, Box<dyn Error>> {
let data = let data = std::fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?;
std::fs::read_to_string(path).map_err(|e| format!("{}: {}", path.display(), e))?;
Ok(yaml_serde::from_str(&data)?) Ok(yaml_serde::from_str(&data)?)
} }
pub fn load_template_data( pub fn load_template_data(&self, tmpl_dir: PathBuf) -> Vec<(String, String)> {
&self,
tmpl_dir: &std::path::Path,
dbg: LogLevel,
) -> Vec<(String, String)> {
self.files self.files
.iter() .iter()
.filter_map(|name| { .filter_map(|name| {
let template = tmpl_dir.join(format!("{}.tera", name)); let template = tmpl_dir.join(format!("{}.tera", name));
match std::fs::read_to_string(&template) { match std::fs::read_to_string(&template) {
Ok(content) => { Ok(content) => {
info!(dbg, " | {}", template.display()); info!(" | {}", template.display());
Some((name.clone(), content)) Some((name.clone(), content))
} }
Err(e) => { Err(e) => {
crit!(dbg, "Skipping {}: {}", template.display(), e); crit!("[!] {}: {e}", template.display());
None None
} }
} }