Skip to content

Commit fa52bf5

Browse files
committed
ref(logger): log and trace at the same time
1 parent a0531a3 commit fa52bf5

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

src/daemon/requests/run.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use crate::{
55
};
66

77
#[cfg(feature = "daemon")]
8-
use {crate::constants::DAEMON_STATE, crate::types::SimDevice, anyhow::anyhow as err};
8+
use {
9+
crate::constants::DAEMON_STATE, crate::types::SimDevice, anyhow::anyhow as err,
10+
xcodebuild::runner::build_settings,
11+
};
912

1013
#[derive(Debug, Serialize, Deserialize)]
1114
pub struct DeviceLookup {
@@ -27,15 +30,11 @@ pub struct Run {
2730
#[async_trait::async_trait]
2831
impl Handler for Run {
2932
async fn handle(self) -> Result<()> {
30-
tracing::info!("⚙️ Running command: {:#?}", self);
33+
tracing::info!("⚙️ Running command: {}", self.config.to_string());
34+
tracing::trace!("{:#?}", self);
3135

32-
let Self {
33-
client,
34-
config,
35-
device,
36-
..
37-
} = self;
38-
let Client { pid, root } = client;
36+
let Self { config, device, .. } = self;
37+
let Client { pid, root } = self.client;
3938

4039
let direction = self.direction.clone();
4140
let state = DAEMON_STATE.clone().lock_owned().await;
@@ -58,12 +57,13 @@ impl Handler for Run {
5857
args
5958
};
6059

61-
let build_settings = xcodebuild::runner::build_settings(&root, &args).await?;
60+
let build_settings = build_settings(&root, &args).await?;
6261
let ref app_id = build_settings.product_bundle_identifier;
6362

6463
// FIX(run): When running with release path_to_app is incorrect
6564
//
66-
// Err: application bundle was not found at the provided path.\nProvide a valid path to the desired application bundle.
65+
// Err: application bundle was not found at the provided path.\nProvide a valid path to the
66+
// desired application bundle.
6767
//
6868
// Path doesn't point to local directory build
6969
let ref path_to_app = build_settings.metal_library_output_dir;

src/nvim/logger.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ impl<'a> Logger<'a> {
5858
}
5959

6060
pub async fn log(&mut self, msg: String, win: &Option<NvimWindow>) -> Result<()> {
61+
tracing::debug!("{msg}");
62+
6163
let mut c = self.get_line_count().await?;
6264
let lines = msg
6365
.split("\n")

src/types/simdevice.rs

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use anyhow::anyhow as err;
21
use anyhow::Result;
32
use serde::{Deserialize, Serialize};
43
use simctl::list::DeviceState;
54
use simctl::Device;
65
use std::hash::Hash;
76
use std::ops::{Deref, DerefMut};
87
use std::path::PathBuf;
8+
use tap::Pipe;
99

1010
use crate::nvim::Logger;
1111
use crate::nvim::NvimWindow;
@@ -60,12 +60,14 @@ impl SimDevice {
6060
win: &Option<NvimWindow>,
6161
) -> Result<()> {
6262
if let DeviceState::Shutdown = &self.state {
63-
self.boot().map_err(|e| err!("{:#?}", e))?;
64-
self.state = DeviceState::Booted;
63+
logger
64+
.log(format!("[Booting] ({})", self.name), win)
65+
.await?;
6566

66-
let msg = format!("[Booting] \"{}\"", self.name);
67-
tracing::info!("{msg}");
68-
logger.log(msg, win).await?;
67+
self.boot()
68+
.pipe(|res| self.handle_error(res, logger, win))
69+
.await?;
70+
self.state = DeviceState::Booted;
6971
}
7072

7173
Ok(())
@@ -78,11 +80,12 @@ impl SimDevice {
7880
logger: &mut Logger<'a>,
7981
win: &Option<NvimWindow>,
8082
) -> Result<()> {
81-
self.install(path_to_app).map_err(|e| err!("{:#?}", e))?;
82-
let msg = format!("[Installed] \"{}\" {app_id}", self.name);
83-
tracing::info!("{msg}");
84-
logger.log(msg, win).await?;
85-
Ok(())
83+
self.install(path_to_app)
84+
.pipe(|res| self.handle_error(res, logger, win))
85+
.await?;
86+
logger
87+
.log(format!("[Installing] ({}) {app_id}", self.name), win)
88+
.await
8689
}
8790

8891
pub async fn try_launch<'a>(
@@ -92,21 +95,49 @@ impl SimDevice {
9295
win: &Option<NvimWindow>,
9396
) -> Result<()> {
9497
if !self.is_running {
95-
tracing::info!("[Launching] \"{}\" {app_id}", self.name);
98+
logger
99+
.log(format!("[Launching] ({}) {app_id}", self.name), win)
100+
.await?;
101+
96102
self.launch(app_id)
97103
.stdout(&"/tmp/wordle_log")
98104
.stderr(&"/tmp/wordle_log")
99105
.exec()
100-
.map_err(|e| err!("{:#?}", e))?;
106+
.pipe(|res| self.handle_error(res, logger, win))
107+
.await?;
101108

102109
self.is_running = true;
103110

104-
tracing::info!("[Launched]");
105111
logger
106112
.log(string_as_section("[Launched]".into()), win)
107113
.await?;
108114
}
109115

110116
Ok(())
111117
}
118+
119+
async fn handle_error<'a, T>(
120+
&mut self,
121+
res: simctl::Result<T>,
122+
logger: &mut Logger<'a>,
123+
win: &Option<NvimWindow>,
124+
) -> Result<()> {
125+
if let Err(e) = to_anyhow_error(res) {
126+
logger.log(e.to_string(), win).await?;
127+
logger.set_status_end(false, true).await?;
128+
self.is_running = false;
129+
}
130+
Ok(())
131+
}
132+
}
133+
134+
fn to_anyhow_error<T>(v: simctl::Result<T>) -> Result<T> {
135+
v.map_err(|e| match e {
136+
simctl::Error::Output { stderr, .. } => {
137+
anyhow::anyhow!("External Command Failure: {stderr}")
138+
}
139+
simctl::Error::Io(err) => anyhow::Error::new(err),
140+
simctl::Error::Json(err) => anyhow::Error::new(err),
141+
simctl::Error::Utf8(err) => anyhow::Error::new(err),
142+
})
112143
}

0 commit comments

Comments
 (0)