From f32fd6bb6384050f85061308a75a7a1aa19f3ad8 Mon Sep 17 00:00:00 2001 From: tami5 Date: Thu, 26 May 2022 20:40:10 +0300 Subject: [PATCH] ref(logger): append logger title to all msgs --- CHANGELOG.md | 6 ++++++ src/compile/mod.rs | 2 +- src/nvim/logger.rs | 18 ++++++------------ src/run/handler.rs | 10 ++++++---- src/run/service.rs | 11 ++++++++--- src/run/simulator.rs | 22 +++++++++++----------- src/xcode.rs | 3 +-- 7 files changed, 39 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 340bb46..1c3716f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog ## [unreleased] +### Bug Fixes +- `(Logger)` Incorrect title for build once requests +### Refactor +- `(Logger)` Append logger title to all msgs + +## [0.1.0] - 2022-05-26 ### Features - `(Build)` Pretty print - `(Compile)` Generate compile flags for files diff --git a/src/compile/mod.rs b/src/compile/mod.rs index 6c193a8..b86047a 100644 --- a/src/compile/mod.rs +++ b/src/compile/mod.rs @@ -264,7 +264,7 @@ pub async fn ensure_server_support<'a>( logger.set_running().await.ok(); logger.open_win().await.ok(); - logger.log(err.to_string()).await.ok(); + logger.append(err.to_string()).await.ok(); logger.set_status_end(false, true).await.ok(); } diff --git a/src/nvim/logger.rs b/src/nvim/logger.rs index 1eabdd8..817e25f 100644 --- a/src/nvim/logger.rs +++ b/src/nvim/logger.rs @@ -1,12 +1,12 @@ use super::{NvimClient, NvimConnection, NvimWindow}; +use crate::nvim::BufferDirection; use crate::Result; -use crate::{nvim::BufferDirection, util::fmt}; use nvim_rs::{Buffer, Window}; pub struct Logger<'a> { pub nvim: &'a NvimClient, - pub title: String, - pub buf: Buffer, + title: String, + buf: Buffer, open_cmd: Option, current_line_count: Option, } @@ -14,7 +14,7 @@ pub struct Logger<'a> { impl<'a> Logger<'a> { /// Set logger title pub fn set_title(&mut self, title: String) -> &mut Self { - self.title = fmt::as_section(title); + self.title = title; self } @@ -41,14 +41,13 @@ impl<'a> Logger<'a> { }) } - // TODO(logger): append title - pub async fn log(&mut self, msg: String) -> Result<()> { + pub async fn append(&mut self, msg: String) -> Result<()> { tracing::debug!("{msg}"); let mut c = self.get_line_count().await?; let lines = msg .split("\n") - .map(ToString::to_string) + .map(|s| format!("[{}] {}", self.title, s)) .collect::>(); let lines_len = lines.len() as i64; @@ -66,11 +65,6 @@ impl<'a> Logger<'a> { Ok(()) } - pub async fn log_title(&mut self) -> Result<()> { - self.log(self.title.clone()).await?; - Ok(()) - } - /// Get window if it's available pub async fn win(&self) -> Option { let windows = self.nvim.list_wins().await.ok()?; diff --git a/src/run/handler.rs b/src/run/handler.rs index 8dd06dc..958a38c 100644 --- a/src/run/handler.rs +++ b/src/run/handler.rs @@ -11,7 +11,7 @@ pub struct RunServiceHandler { impl RunServiceHandler { // Change the status of the process to running - pub fn new(client: Client, mut process: Process, key: String) -> Result { + pub fn new(target: String, client: Client, mut process: Process, key: String) -> Result { let mut stream = process.spawn_and_stream()?; let kill_send = process.clone_kill_sender().unwrap(); @@ -30,20 +30,22 @@ impl RunServiceHandler { } }; + logger.set_title(format!("Run:{target}")); + use process_stream::ProcessItem::*; match output { Output(msg) => { if !msg.contains("ignoring singular matrix") { - logger.log(msg).await?; + logger.append(msg).await?; } } Error(msg) => { - logger.log(format!("[Error] {msg}")).await?; + logger.append(format!("[Error] {msg}")).await?; } // TODO: this should be skipped when user re-run the app Exit(code) => { let success = &code == "0"; - logger.log(format!("[Exit] {code}")).await?; + logger.append(format!("[Exit] {code}")).await?; logger.set_status_end(success, !success).await?; break; } diff --git a/src/run/service.rs b/src/run/service.rs index ee527b5..345c663 100644 --- a/src/run/service.rs +++ b/src/run/service.rs @@ -29,7 +29,7 @@ impl std::fmt::Display for RunService { impl RunService { pub async fn new(state: &mut MutexGuard<'_, State>, req: RunRequest) -> Result { let key = req.to_string(); - let ref target = req.config.target; + let target = req.config.target.clone(); let ref root = req.client.root; let device = state.devices.from_lookup(req.device); let build_args = req.config.args(root, &device)?; @@ -54,7 +54,7 @@ impl RunService { let medium = RunMedium::from_device_or_settings(device, build_settings, req.config)?; let process = medium.run(logger).await?; - let handler = RunServiceHandler::new(req.client.clone(), process, key.clone())?; + let handler = RunServiceHandler::new(target, req.client.clone(), process, key.clone())?; Ok(Self { client: req.client, @@ -92,7 +92,12 @@ impl Watchable for RunService { }; let process = self.medium.run(logger).await?; - *handler = RunServiceHandler::new(self.client.clone(), process, self.key.clone())?; + *handler = RunServiceHandler::new( + config.target.clone(), + self.client.clone(), + process, + self.key.clone(), + )?; Ok(()) } diff --git a/src/run/simulator.rs b/src/run/simulator.rs index ab6f8ff..983ab17 100644 --- a/src/run/simulator.rs +++ b/src/run/simulator.rs @@ -32,30 +32,30 @@ impl Simulator { Err(Error::NotFound(_, _)) => { let msg = format!("[Simulator] Launching"); tracing::info!("{msg}"); - logger.log(msg).await?; + logger.append(msg).await?; tokio::process::Command::new("open") .args(&["-a", "Simulator"]) .spawn()? .wait() .await?; let msg = format!("[Simulator] Connected"); - logger.log(msg).await?; - logger.log(fmt::separator()).await?; + logger.append(msg).await?; + logger.append(fmt::separator()).await?; } Err(err) => { let msg = err.to_string(); tracing::error!("{msg}"); - logger.log(msg).await?; + logger.append(msg).await?; } _ => {} } - logger.log(self.booting_msg()).await?; + logger.append(self.booting_msg()).await?; if let Err(e) = self.device.boot() { let err: Error = e.into(); let err_msg = err.to_string(); if !err_msg.contains("current state Booted") { - logger.log(err_msg).await?; + logger.append(err_msg).await?; logger.set_status_end(false, true).await?; return Err(err); } @@ -64,7 +64,7 @@ impl Simulator { } pub async fn install<'a>(&self, logger: &mut Logger<'a>) -> Result<()> { - logger.log(self.installing_msg()).await?; + logger.append(self.installing_msg()).await?; self.device .install(&self.output_dir()) .pipe(|res| self.ok_or_abort(res, logger)) @@ -73,7 +73,7 @@ impl Simulator { } pub async fn launch<'a>(&self, logger: &mut Logger<'a>) -> Result { - logger.log(self.launching_msg()).await?; + logger.append(self.launching_msg()).await?; let mut process = Process::new("xcrun"); process.args(&[ @@ -86,8 +86,8 @@ impl Simulator { process.arg(&self.info.product_bundle_identifier); process.kill_on_drop(true); - logger.log(self.connected_msg()).await?; - logger.log(fmt::separator()).await?; + logger.append(self.connected_msg()).await?; + logger.append(fmt::separator()).await?; Ok(process) } @@ -99,7 +99,7 @@ impl Simulator { ) -> Result<()> { if let Err(e) = res { let error: Error = e.into(); - logger.log(error.to_string()).await?; + logger.append(error.to_string()).await?; logger.set_status_end(false, true).await?; Err(error) } else { diff --git a/src/xcode.rs b/src/xcode.rs index 44b7d86..4eebc27 100644 --- a/src/xcode.rs +++ b/src/xcode.rs @@ -81,12 +81,11 @@ pub async fn build_with_logger<'a, P: AsRef>( let mut success = true; logger.set_running().await?; - logger.log_title().await?; while let Some(line) = stream.next().await { line.contains("FAILED").then(|| success = false); - logger.log(line).await?; + logger.append(line).await?; } logger.set_status_end(success, open).await?;