Skip to content

Commit 401a3d9

Browse files
committed
Route Rhai's debug function to the faux-mgs log.
- The `debug("message")` function is routed to the faux-mgs slog logging. Prefixing a message with "crit|", "trace|", "error|", "warn|", "error|", or "debug|" will log at that corresponding level. Leaving off the prefix or using some other prefix will log at the debug level. print still goes to stdout.
1 parent 73d7947 commit 401a3d9

File tree

5 files changed

+77
-19
lines changed

5 files changed

+77
-19
lines changed

faux-mgs/src/rhaiscript.rs

+59-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
use crate::anyhow;
55
use crate::debug;
6+
use crate::info;
7+
use crate::warn;
8+
use slog::crit;
9+
use slog::error;
10+
use slog::trace;
11+
612
use crate::fs;
713
use crate::json;
814
use crate::run_command;
@@ -255,7 +261,9 @@ pub async fn interpreter(
255261
let interface = sp.interface().to_string().to_owned();
256262
let reset_watchdog_timeout_ms = sp.reset_watchdog_timeout_ms() as i64;
257263

264+
let thread_log = log.clone();
258265
let handle = std::thread::spawn(move || {
266+
let log = thread_log;
259267
// Create Engine
260268
let mut engine = Engine::new();
261269

@@ -320,15 +328,14 @@ pub async fn interpreter(
320328
// Offer proper JSON to Dynamic::Map conversion
321329
.register_fn("json_to_map", move |v: Dynamic| -> Dynamic {
322330
match v.into_string() {
323-
Ok(s) => {
324-
match serde_json::from_str::<Dynamic>(&s) {
325-
Ok(v) => v,
326-
Err(e) => {
327-
let err = format!("{{\"error\": \"{:?}\"}}", e).to_string();
328-
serde_json::from_str::<Dynamic>(&err).unwrap()
329-
}
331+
Ok(s) => match serde_json::from_str::<Dynamic>(&s) {
332+
Ok(v) => v,
333+
Err(e) => {
334+
let err = format!("{{\"error\": \"{:?}\"}}", e)
335+
.to_string();
336+
serde_json::from_str::<Dynamic>(&err).unwrap()
330337
}
331-
}
338+
},
332339
Err(e) => {
333340
let err =
334341
format!("{{\"error\": \"{:?}\"}}", e).to_string();
@@ -369,6 +376,50 @@ pub async fn interpreter(
369376
},
370377
);
371378

379+
// A script can log via debug at any level:
380+
// debug("INFO|log message at INFO level");
381+
// debug("CRIT|log message at CRIT level");
382+
// etc.
383+
let rhai_log = log.clone();
384+
engine.on_debug(move |x, src, pos| {
385+
let src = if src.is_some() {
386+
format!("{}@", src.unwrap())
387+
} else {
388+
"".to_string()
389+
};
390+
let x: Vec<&str> = x.trim_matches('"').splitn(2, '|').collect();
391+
let (level, msg) = if x.len() == 1 {
392+
("info".to_string(), x[0].to_string())
393+
} else {
394+
let level = x[0].to_string().to_lowercase();
395+
let msg = x[1].to_string();
396+
match level.as_str() {
397+
"info" => ("info".to_string(), msg),
398+
"crit" => ("crit".to_string(), msg),
399+
"error" => ("error".to_string(), msg),
400+
"trace" => ("trace".to_string(), msg),
401+
"warn" => ("warn".to_string(), msg),
402+
"debug" => ("debug".to_string(), msg),
403+
_ => ("debug".to_string(), format!("{}|{}", level, msg)),
404+
}
405+
};
406+
let src = if src.len() > 0 {
407+
format!("{}@", src)
408+
} else {
409+
"".to_string()
410+
};
411+
let msg = format!("{}pos={:?} {}", src, pos, msg);
412+
match level.as_str() {
413+
"crit" => crit!(rhai_log, "{msg}"),
414+
"debug" => debug!(rhai_log, "{msg}"),
415+
"error" => error!(rhai_log, "{msg}"),
416+
"info" => info!(rhai_log, "{msg}"),
417+
"trace" => trace!(rhai_log, "{msg}"),
418+
"warn" => warn!(rhai_log, "{msg}"),
419+
_ => unreachable!(),
420+
}
421+
});
422+
372423
// Print registered functions if you're interested.
373424

374425
// engine.gen_fn_signatures(false).into_iter().for_each(|func| println!("{func}"));

gateway-sp-comms/src/single_sp/update.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use gateway_messages::UpdateStatus;
2222
use hubtools::Error as HubtoolsError;
2323
use hubtools::RawHubrisArchive;
2424
use slog::debug;
25-
use slog::trace;
2625
use slog::error;
2726
use slog::info;
2827
use slog::warn;
@@ -657,7 +656,7 @@ async fn send_update_in_chunks(
657656
let id = update_id.into();
658657
while !CursorExt::is_empty(&image) {
659658
let prior_pos = image.position();
660-
trace!(
659+
debug!(
661660
log, "sending update chunk";
662661
"id" => %update_id,
663662
"offset" => offset,

scripts/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ Rhai calls the script's `main() -> i64 {}`.
5858
- rhai_fs::FilesystemPackage - file system access
5959
- [rhai_chrono::ChronoPackage](https://github.com/iganev/rhai-chrono) - standard time formats.
6060

61+
### Modified Rhai behaviod
62+
- The `debug("message")` function is routed to the faux-mgs slog logging.
63+
Prefixing a message with "crit|", "trace|", "error|", "warn|", "error|", or "debug|"
64+
will log at that corresponding level. Leaving off the prefix or using some other
65+
prefix will log at the debug level.
66+
6167
### Custom functions:
6268

6369
- faux_mgs(["arg0", .., "argN"]) -> #{} // Run any faux-mgs command -> map

scripts/targets.json

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
2-
"host_system": "slab",
3-
42
"repo-home": "${HOME}/Oxide/src",
53
"base_repo": "${repo-home}/hubris/master",
64
"ut_repo": "${repo-home}/hubris/epoch",

scripts/upgrade-rollback.rhai

+11-7
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ fn main() {
2929

3030
print("\n## Determine initial FW state and update if needed");
3131

32-
// There should be no updates in progress.
33-
// If there was a previous RoT update in progress and
34-
// the SP has since been reset, then update-status
35-
// for SP and RoT will return None.
36-
// However, the RoT will refuse requests to reset or update.
32+
// Generally, there should no updates in progress
33+
// and we can proceed with installation of the
34+
// baseline images if needed.
35+
// .
36+
// If a previous test was aborted, some recovery may
37+
// be necessary.
38+
//
39+
// A corner case that may require a hard-reset is described in:
3740
// https://github.com/oxidecomputer/hubris/issues/1022
41+
3842
let problems = 0;
3943
for attempt in 1..=2 {
4044
problems = 0;
@@ -50,7 +54,7 @@ fn main() {
5054
let id = r?.id;
5155
debug(`r=${r}`);
5256
debug(`id=${id}`);
53-
print(`Failed: component ${component}: ${r}`);
57+
debug(`Failed: component ${component}: ${r}`);
5458
print(`The SP probably needs to be reset before continuing`);
5559
print(`Trying to abort the update with id=${id}`);
5660
let r = faux_mgs(["update-abort", component, `${id}`]);
@@ -93,7 +97,7 @@ fn main() {
9397
let flash_rot = result?.ok?.rot;
9498

9599
print("");
96-
print(`Elapsed time so far: ${start_ts.elapsed}`);
100+
print(`Elapsed time: ${start_ts.elapsed}`);
97101
print(`Now=${datetime_local()}`);
98102
print("");
99103

0 commit comments

Comments
 (0)