Skip to content

Commit

Permalink
get to the example
Browse files Browse the repository at this point in the history
  • Loading branch information
naftulikay committed Mar 1, 2024
1 parent 0691447 commit 88565f9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion example_watchexec/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
./watched
watched
2 changes: 2 additions & 0 deletions example_watchexec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ path = "examples/watchexec_async.rs"
[dependencies]
anyhow.workspace = true
chrono.workspace = true
examples_common = { path = "../examples_common" }
futures.workspace = true
tokio.workspace = true
tracing.workspace = true
watchexec.workspace = true
watchexec-signals.workspace = true
19 changes: 17 additions & 2 deletions example_watchexec/examples/watchexec_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ use anyhow::Result;
use chrono::Utc;
use futures::FutureExt;
use tokio::sync::Mutex;
use tracing::Level;
use watchexec::action::ActionHandler;
use watchexec::Watchexec;

use examples_common::logging::LogLevelFilter;

const CARGO_CRATE_NAME: &str = env!("CARGO_CRATE_NAME");
const CARGO_MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");
const WATCHED_FILE_NAME: &str = "watched";

/// Read/write storage available for use during response to [watchexec] events.
#[derive(Debug)]
struct Runtime {
/// The crate root directory
Expand All @@ -30,7 +35,7 @@ impl Runtime {

/// Process an event that occurred in [watchexec].
pub async fn on_event(&mut self, action: ActionHandler) -> ActionHandler {
eprintln!("{ts}: Received event: {:?}", action, ts = Utc::now().to_rfc3339());
tracing::info!("Received event: {:?}", action);

action
}
Expand All @@ -43,6 +48,15 @@ struct EventMap {

#[tokio::main]
async fn main() -> Result<()> {
// initialize logging
examples_common::logging::init_logging(LogLevelFilter::builder()
.global(Level::WARN)
.level(CARGO_CRATE_NAME, Level::TRACE)
.level(examples_common::CRATE_NAME, Level::DEBUG)
.build());


// start work
let workdir = PathBuf::from(CARGO_MANIFEST_DIR);
let w = workdir.join(WATCHED_FILE_NAME);

Expand All @@ -51,7 +65,8 @@ async fn main() -> Result<()> {
tokio::fs::write(&w, "init").await?;
}

eprintln!("{ts}: Starting watchexec...", ts = Utc::now().to_rfc3339());
tracing::info!("Starting watchexec");
tracing::info!("Modify ./{} to trigger events", PathBuf::from(example_watchexec::CRATE_NAME).join(WATCHED_FILE_NAME).display());

let runtime = Runtime::new(workdir);

Expand Down
2 changes: 2 additions & 0 deletions example_watchexec/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// Convenience constant for the crate name
pub const CRATE_NAME: &str = env!("CARGO_CRATE_NAME");
3 changes: 0 additions & 3 deletions example_watchexec/src/main.rs

This file was deleted.

0 comments on commit 88565f9

Please sign in to comment.