Fix README example for on_print and on_debug.
This commit is contained in:
13
README.md
13
README.md
@@ -1979,17 +1979,20 @@ engine.on_print(|x| println!("hello: {}", x));
|
||||
engine.on_debug(|x| println!("DEBUG: {}", x));
|
||||
|
||||
// Example: quick-'n-dirty logging
|
||||
let mut log: Vec<String> = Vec::new();
|
||||
let logbook = Arc::new(RwLock::new(Vec::<String>::new()));
|
||||
|
||||
// Redirect print/debug output to 'log'
|
||||
engine.on_print(|s| log.push(format!("entry: {}", s)));
|
||||
engine.on_debug(|s| log.push(format!("DEBUG: {}", s)));
|
||||
let log = logbook.clone();
|
||||
engine.on_print(move |s| log.write().unwrap().push(format!("entry: {}", s)));
|
||||
|
||||
let log = logbook.clone();
|
||||
engine.on_debug(move |s| log.write().unwrap().push(format!("DEBUG: {}", s)));
|
||||
|
||||
// Evaluate script
|
||||
engine.eval::<()>(script)?;
|
||||
|
||||
// 'log' captures all the 'print' and 'debug' output
|
||||
for entry in log {
|
||||
// 'logbook' captures all the 'print' and 'debug' output
|
||||
for entry in logbook.read().unwrap().iter() {
|
||||
println!("{}", entry);
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user