Skip to content

Commit

Permalink
ref(logger): append logger title to all msgs
Browse files Browse the repository at this point in the history
  • Loading branch information
kkharji committed May 26, 2022
1 parent 25a4e56 commit f32fd6b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 33 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog
## [unreleased]
### <!-- 1 -->Bug Fixes
- `(Logger)` <a href="https://github.com/tami5/xbase/commit/25a4e56"> Incorrect title for build once requests</a>
### <!-- 2 -->Refactor
- `(Logger)` <a href="https://github.com/tami5/xbase/commit/32e9555"> Append logger title to all msgs</a>

## [0.1.0] - 2022-05-26
### <!-- 0 -->Features
- `(Build)` <a href="https://github.com/tami5/xbase/commit/611ef4d"> Pretty print</a>
- `(Compile)` <a href="https://github.com/tami5/xbase/commit/8855851"> Generate compile flags for files</a>
Expand Down
2 changes: 1 addition & 1 deletion src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
18 changes: 6 additions & 12 deletions src/nvim/logger.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
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<NvimConnection>,
title: String,
buf: Buffer<NvimConnection>,
open_cmd: Option<String>,
current_line_count: Option<i64>,
}

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
}

Expand All @@ -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::<Vec<String>>();
let lines_len = lines.len() as i64;

Expand All @@ -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<NvimWindow> {
let windows = self.nvim.list_wins().await.ok()?;
Expand Down
10 changes: 6 additions & 4 deletions src/run/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
pub fn new(target: String, client: Client, mut process: Process, key: String) -> Result<Self> {
let mut stream = process.spawn_and_stream()?;
let kill_send = process.clone_kill_sender().unwrap();

Expand All @@ -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;
}
Expand Down
11 changes: 8 additions & 3 deletions src/run/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl std::fmt::Display for RunService {
impl RunService {
pub async fn new(state: &mut MutexGuard<'_, State>, req: RunRequest) -> Result<Self> {
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)?;
Expand All @@ -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,
Expand Down Expand Up @@ -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(())
}
Expand Down
22 changes: 11 additions & 11 deletions src/run/simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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))
Expand All @@ -73,7 +73,7 @@ impl Simulator {
}

pub async fn launch<'a>(&self, logger: &mut Logger<'a>) -> Result<Process> {
logger.log(self.launching_msg()).await?;
logger.append(self.launching_msg()).await?;
let mut process = Process::new("xcrun");

process.args(&[
Expand All @@ -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)
}
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions src/xcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,11 @@ pub async fn build_with_logger<'a, P: AsRef<Path>>(
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?;
Expand Down

0 comments on commit f32fd6b

Please sign in to comment.