From 7b79205148e0d50e5fb6c8a5e4c522b1354c4c80 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Sun, 12 May 2024 20:27:13 -0500 Subject: [PATCH] Better logging for tailwind subprocess --- src/main.rs | 33 +++++++++++++++++++++++++++++---- templates/page.html.jinja | 1 + 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index aa0efb8..130140b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,13 +4,15 @@ use axum::routing::get; use axum::{http::StatusCode, response::Html, serve, Router}; use minijinja::{context, Environment}; use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher}; +use std::process::Stdio; use std::sync::Arc; use std::{path::PathBuf, str::FromStr, sync::OnceLock}; +use tokio::io::{AsyncBufReadExt, BufReader}; use tokio::sync::mpsc::channel; use tokio::sync::Mutex; use tower_http::services::ServeDir; use tower_livereload::{LiveReloadLayer, Reloader}; -pub use tracing::{debug, error, info, warn}; +pub use tracing::{debug, error, event, info, span, warn, Level}; #[derive(Debug)] struct Berr(Box); @@ -48,10 +50,33 @@ async fn main() -> Besult<()> { // TODO: only start tailwind if in dev mode? tokio::spawn(async move { info!("Starting tailwind..."); - let tw = tokio::process::Command::new("tailwindcss") + match tokio::process::Command::new("tailwindcss") .args(["-i", "src/style.css", "-o", "static/style.css", "--watch"]) - .spawn(); - info!("Tailwind spawned. {tw:#?}"); + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + { + Ok(mut tw) => { + info!("Tailwind spawned!"); + let mut stdout_reader = BufReader::new(tw.stdout.take().unwrap()).lines(); + tokio::spawn(async move { + while let Ok(Some(l)) = stdout_reader.next_line().await { + if l.trim().len() > 0 { + event!(target: "tailwind::stdout", Level::INFO, "{l}"); + } + } + }); + let mut stderr_reader = BufReader::new(tw.stderr.take().unwrap()).lines(); + tokio::spawn(async move { + while let Ok(Some(l)) = stderr_reader.next_line().await { + if l.trim().len() > 0 { + event!(target: "tailwind::stderr", Level::INFO, "{l}"); + } + } + }); + } + Err(e) => error!("Failed to spawn Tailwind: {e}"), + } }); let templates = Arc::new(Mutex::new(Environment::new())); diff --git a/templates/page.html.jinja b/templates/page.html.jinja index 67cda10..c57df17 100644 --- a/templates/page.html.jinja +++ b/templates/page.html.jinja @@ -7,6 +7,7 @@ +Page Template {% block body %}{% endblock %}