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

tools: cargo-style logging for x86 bootimager #336

Merged
merged 2 commits into from
Oct 2, 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
6 changes: 2 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
{
i{
"rust-analyzer.check.invocationLocation": "workspace",
"rust-analyzer.cargo.buildScripts.invocationLocation": "workspace",
"rust-analyzer.cargo.buildScripts.overrideCommand": [
"cargo",
"mn",
"clippy",
"--quiet",
"--message-format=json",
"--all-targets"
],
"rust-analyzer.check.overrideCommand": [
"cargo",
"mn",
"clippy",
"--quiet",
"--message-format=json",
"--all-targets"
],
"rust-analyzer.workspace.symbol.search.scope": "workspace_and_dependencies",
"rust-analyzer.typing.continueCommentsOnNewline": false,
}
}
9 changes: 6 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

source scripts/_util.sh

if confirm "test"; then
echo "ok you confirmed"
else
echo "you didn't confirm"
fi
5 changes: 4 additions & 1 deletion tools/x86_64-bootimager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ edition = "2021"
[dependencies]
anyhow = "1"
camino = "1"
heck = "0.5"
# cargo_metadata = "0.18.1"
# used for UEFI booting in QEMU
ovmf-prebuilt = "0.1.0-alpha.1"
owo-colors = "4"
clap = { version = "4.5", features = ["derive", "env"] }
bootloader = "0.11"
bootloader-boot-config = "0.11.3"
# escargot = "0.5"
tracing = "0.1"
tracing-subscriber = "0.3"
tracing-subscriber = { version = "0.3", features = ["fmt", "tracing-log"] }
supports-color = "3"
37 changes: 22 additions & 15 deletions tools/x86_64-bootimager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use camino::{Utf8Path, Utf8PathBuf};
use clap::{Args, Parser, ValueEnum, ValueHint};
use std::fmt;

pub mod output;

#[derive(Debug, Parser)]
#[command(next_help_heading = "Build Options")]
pub struct Builder {
Expand Down Expand Up @@ -176,22 +178,22 @@ impl QemuOptions {
qemu_args,
} = self;

tracing::info!(qemu = %qemu_path, args = ?qemu_args, "Booting mnemOS VM");

let mut cmd = std::process::Command::new(qemu_path);
if !qemu_args.is_empty() {
cmd.args(qemu_args.iter());
}

cmd.arg("-drive")
.arg(format!("format=raw,file={bootimage_path}"));

if let BootMode::Uefi = boot_mode {
cmd.arg("-bios").arg(ovmf_prebuilt::ovmf_pure_efi());
}
tracing::info!(
?qemu_args,
%qemu_path,
%boot_mode,
"booting in QEMU: {bootimage_path}"
);
cmd.arg("-drive")
.arg(format!("format=raw,file={bootimage_path}"));

tracing::debug!("Running QEMU command: {cmd:?}");

let mut qemu = cmd.spawn().context("failed to spawn QEMU child process")?;
let status = qemu.wait().context("QEMU child process failed")?;

Expand Down Expand Up @@ -239,21 +241,22 @@ impl BootloaderOptions {
bootcfg.frame_buffer.minimum_framebuffer_width = self.framebuffer_width;
}
tracing::debug!(
?bootcfg.log_level,
bootcfg.frame_buffer_logging,
bootcfg.serial_logging,
?bootcfg.frame_buffer
log.info = ?bootcfg.log_level,
log.framebuffer = bootcfg.frame_buffer_logging,
log.serial = bootcfg.serial_logging,
"Configuring bootloader",
);
bootcfg
}
}

impl Builder {
pub fn build_bootimage(&self) -> anyhow::Result<Utf8PathBuf> {
let t0 = std::time::Instant::now();
tracing::info!(
boot_mode = ?self.bootloader.mode,
boot_mode = %self.bootloader.mode,
kernel = %self.kernel_bin,
"Building boot image."
"Building boot image"
);

let canonical_kernel_bin: Utf8PathBuf = self
Expand Down Expand Up @@ -290,7 +293,11 @@ impl Builder {
}
};

tracing::info!("Created bootable disk image ({path})",);
tracing::info!(
"Finished bootable disk image [{}] in {:.02?} ({path})",
self.bootloader.mode,
t0.elapsed(),
);

Ok(path)
}
Expand Down
28 changes: 5 additions & 23 deletions tools/x86_64-bootimager/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use clap::{Args, Parser};
use mnemos_x86_64_bootimager::{Builder, QemuOptions};
use clap::Parser;
use mnemos_x86_64_bootimager::{output, Builder, QemuOptions};

fn main() -> anyhow::Result<()> {
use tracing_subscriber::prelude::*;

let App {
cmd,
builder,
output,
} = App::parse();
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().without_time().pretty())
.with(output.trace_filter)
.init();
output.init()?;
tracing::info!("Assuming direct control over the build!");

let bootimage_path = builder.build_bootimage()?;
let mode = builder.bootloader.mode;
Expand All @@ -38,7 +34,7 @@ struct App {
builder: Builder,

#[clap(flatten)]
output: OutputOptions,
output: output::Options,
}

#[derive(Debug, Clone, Parser)]
Expand All @@ -52,17 +48,3 @@ enum Subcommand {
#[clap(alias = "run")]
Qemu(QemuOptions),
}

#[derive(Clone, Debug, Args)]
#[command(next_help_heading = "Output Options")]
struct OutputOptions {
/// Tracing filter for the bootimage builder.
#[clap(
long = "trace",
alias = "log",
env = "RUST_LOG",
default_value = "info",
global = true
)]
trace_filter: tracing_subscriber::filter::Targets,
}
Loading
Loading