Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify dbg.logln statements with dlogln! #4

Merged
merged 1 commit into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/debug_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ pub fn u64_from_debugvalue(v: DEBUG_VALUE) -> Result<u64> {
Ok(value)
}

/// Macro to make it nicer to invoke `DebugClient::logln` / `DebugClient::log`
/// by avoiding to `format!` everytime the arguments.
#[macro_export]
macro_rules! dlogln {
($dbg:ident, $($arg:tt)*) => {{
$dbg.logln(format!($($arg)*))
}};
}

#[macro_export]
macro_rules! dlog {
($dbg:ident, $($arg:tt)*) => {{
$dbg.log(format!($($arg)*))
}};
}

pub struct DebugClient {
control: IDebugControl3,
registers: IDebugRegisters,
Expand Down Expand Up @@ -93,11 +109,11 @@ impl DebugClient {
}

/// Log a message in the debugging window.
#[allow(dead_code)]
pub fn log<Str>(&self, args: Str) -> Result<()>
where
Str: Into<Vec<u8>>,
{
self.output(DEBUG_OUTPUT_NORMAL, "[snapshot] ")?;
self.output(DEBUG_OUTPUT_NORMAL, args)
}

Expand All @@ -106,7 +122,8 @@ impl DebugClient {
where
Str: Into<Vec<u8>>,
{
self.log(args)?;
self.output(DEBUG_OUTPUT_NORMAL, "[snapshot] ")?;
self.output(DEBUG_OUTPUT_NORMAL, args)?;
self.output(DEBUG_OUTPUT_NORMAL, "\n")
}

Expand Down
16 changes: 7 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,18 +380,16 @@ fn snapshot_inner(dbg: &DebugClient, args: SnapshotArgs) -> Result<()> {
fix_number_nodes(&mut json);

// Dump the CPU register into a `regs.json` file.
dbg.logln(format!(
"Dumping the CPU state into {}..",
regs_path.display()
))?;
dlogln!(dbg, "Dumping the CPU state into {}..", regs_path.display())?;

let regs_file = File::create(regs_path)?;
serde_json::to_writer_pretty(regs_file, &json)?;

dbg.logln(format!(
dlogln!(
dbg,
"Dumping the memory state into {}..",
mem_path.display()
))?;
)?;

// Generate the `mem.dmp`.
dbg.exec(format!(
Expand All @@ -407,7 +405,7 @@ fn snapshot_inner(dbg: &DebugClient, args: SnapshotArgs) -> Result<()> {
mem_path
))?;

dbg.logln("Done!")?;
dlogln!(dbg, "Done!")?;

Ok(())
}
Expand Down Expand Up @@ -441,14 +439,14 @@ fn wrap<P: Parser>(
let args = match P::try_parse_from(args.split_whitespace()) {
Ok(a) => a,
Err(e) => {
let _ = dbg.logln(format!("{e}"));
let _ = dlogln!(dbg, "{e}");
return E_ABORT;
}
};

match callback(&dbg, args) {
Err(e) => {
let _ = dbg.logln(format!("Ran into an error: {e:?}"));
let _ = dlogln!(dbg, "Ran into an error: {e:?}");

E_ABORT
}
Expand Down