[control] Windows Caddy mode: Caddy's admin API on loopback TCP where there is no Unix socket, applies go through Caddy without an edge, control and the Caddy guard as Windows services, a log file

This commit is contained in:
2026-11-17 13:59:12 +01:00
parent fc9e16f1b2
commit acf69a8ed2
8 changed files with 302 additions and 60 deletions
+40 -7
View File
@@ -12,6 +12,8 @@ use log::{Level, LevelFilter, Log, Metadata, Record};
struct Stderr {
own: LevelFilter,
/// `$IANITOR_LOG_FILE` or `--log-file`: append here instead of stderr (Windows services).
file: Option<std::sync::Mutex<std::fs::File>>,
}
impl Log for Stderr {
@@ -33,12 +35,27 @@ impl Log for Stderr {
Level::Debug => "DEBUG",
Level::Trace => "TRACE",
};
let _ = writeln!(
std::io::stderr().lock(),
"{lvl} {}: {}",
r.target(),
r.args()
);
match &self.file {
Some(f) => {
if let Ok(mut f) = f.lock() {
let _ = writeln!(
f,
"{} {lvl} {}: {}",
crate::util::rfc3339(crate::util::now()),
r.target(),
r.args()
);
}
}
None => {
let _ = writeln!(
std::io::stderr().lock(),
"{lvl} {}: {}",
r.target(),
r.args()
);
}
}
}
}
@@ -48,6 +65,22 @@ impl Log for Stderr {
}
pub fn init(level: Option<&str>) {
init_to(
level,
std::env::var_os("IANITOR_LOG_FILE").map(std::path::PathBuf::from),
);
}
/// Like [`init`], appending to `file` when given.
pub fn init_to(level: Option<&str>, file: Option<std::path::PathBuf>) {
let file = file.and_then(|p| {
std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(p)
.ok()
.map(std::sync::Mutex::new)
});
let own = match level.map(str::to_ascii_lowercase).as_deref() {
Some("error") => LevelFilter::Error,
Some("warn") => LevelFilter::Warn,
@@ -55,7 +88,7 @@ pub fn init(level: Option<&str>) {
Some("trace") => LevelFilter::Trace,
_ => LevelFilter::Info,
};
if log::set_boxed_logger(Box::new(Stderr { own })).is_ok() {
if log::set_boxed_logger(Box::new(Stderr { own, file })).is_ok() {
log::set_max_level(own);
}
}