Skip to content

Commit 0d3fbb4

Browse files
committed
Add logging
1 parent ad5a7ad commit 0d3fbb4

File tree

6 files changed

+22
-3
lines changed

6 files changed

+22
-3
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Additionally:
2222
This is done by using the "type state" pattern. Each state/input/output struct is wrapped by an internal enum that is automatically generated by the macro.
2323
- Each transition can have an optional **guard** (a predicate function).
2424
- The input or output can be missing (e.g. for a Moore machine). This is internally implemented by a special `Nothing` symbol.
25+
- No dynamic memory allocations and minimal stack memory usage.
2526

2627
## [Examples](https://github.com/michalsustr/rust-automata/tree/main/examples)
2728

@@ -113,8 +114,8 @@ Major:
113114

114115
Minor (soon):
115116
- [x] Guards as bool expressions
116-
- [ ] Performance improvements
117-
- [ ] Logging transitions -- tracing?
117+
- [x] Performance improvements
118+
- [x] Logging transitions
118119
- [ ] Configurable `should_panic` for invalid transitions
119120
- [ ] Better error messages
120121
- sig checks for guards

examples/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ edition = "2021"
55

66
[dependencies]
77
rust-automata = { path = "../rust-automata", version = "0.0.3", features = ["mermaid", "dsl"] }
8+
env_logger = "0.11"

examples/src/vikings.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ impl Torch {
125125
#[test]
126126
fn vikings_successfully_cross() {
127127
use events::*;
128+
env_logger::init();
128129

129130
let clock = ManualClock::new();
130131

rust-automata-macros/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,10 @@ pub fn state_machine(attr: TokenStream, item: TokenStream) -> TokenStream {
590590
(_, _) => None,
591591
}
592592
}
593+
594+
fn name() -> &'static str {
595+
stringify!(#machine_ident)
596+
}
593597
}
594598
}
595599
};

rust-automata/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ aquamarine = { version = "0.6", optional = true }
2222
rust-automata-macros = { path = "../rust-automata-macros", version = "0.0.3" }
2323
chrono = "0.4"
2424
serde = { version = "1", features = ["derive"] }
25+
log = "0.4"
26+
env_logger = "0.11"
2527

2628
[dev-dependencies]
2729
trybuild = "1.0"

rust-automata/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub mod timestamp;
1717
use core::fmt::Display;
1818
use std::hash::Hash;
1919
use std::marker::PhantomData;
20+
use log;
2021

2122
#[doc(hidden)]
2223
pub use takeable::Takeable;
@@ -112,6 +113,8 @@ pub trait StateMachineImpl {
112113
state: &Self::State,
113114
input: EnumId<Self::Input>,
114115
) -> Option<EnumId<Self::Output>>;
116+
/// The name of the state machine.
117+
fn name() -> &'static str;
115118
}
116119

117120
/// Encapsulates the state and other SM data and expose transition functions.
@@ -171,8 +174,15 @@ where
171174
let from_str = T::State::get_variant(&from_id);
172175
let input_str = T::Input::get_variant(&input_id);
173176
panic!("Invalid transition from {from_str} using input {input_str}");
177+
} else {
178+
log::debug!("{}: ({}, {}) -> ({}, {})",
179+
T::name(),
180+
T::State::get_variant(&from_id),
181+
T::Input::get_variant(&input_id),
182+
T::State::get_variant(&self.state.as_ref().enum_id()),
183+
T::Output::get_variant(&output.enum_id()),
184+
);
174185
}
175-
176186
O::from(output)
177187
}
178188

0 commit comments

Comments
 (0)