Skip to content

Commit 3425e9b

Browse files
committed
cli: add a utility function for initializing tracing
Refactor the tracing initialization code into a utility function, so that it can be shared with future CLIs that we'll add. Signed-off-by: Omer Tuchfeld <[email protected]>
1 parent d45864d commit 3425e9b

File tree

6 files changed

+28
-17
lines changed

6 files changed

+28
-17
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ platforms = ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "powerpc64
1919
anyhow = { workspace = true }
2020
bootc-lib = { version = "1.0", path = "../lib" }
2121
clap = { workspace = true }
22+
bootc-utils = { path = "../utils" }
2223
tokio = { workspace = true, features = ["macros"] }
2324
log = "0.4.21"
2425
tracing = { workspace = true }
25-
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
2626

2727
[lints]
2828
workspace = true

cli/src/main.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
11
//! The main entrypoint for bootc, which just performs global initialization, and then
22
//! calls out into the library.
3-
43
use anyhow::Result;
54

65
/// The code called after we've done process global init and created
76
/// an async runtime.
87
async fn async_main() -> Result<()> {
9-
// Don't include timestamps and such because they're not really useful and
10-
// too verbose, and plus several log targets such as journald will already
11-
// include timestamps.
12-
let format = tracing_subscriber::fmt::format()
13-
.without_time()
14-
.with_target(false)
15-
.compact();
16-
// Log to stderr by default
17-
tracing_subscriber::fmt()
18-
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
19-
.event_format(format)
20-
.with_writer(std::io::stderr)
21-
.init();
22-
tracing::trace!("starting");
8+
bootc_utils::initialize_tracing();
9+
10+
tracing::trace!("starting bootc");
11+
2312
// As you can see, the role of this file is mostly to just be a shim
2413
// to call into the code that lives in the internal shared library.
2514
bootc_lib::cli::run_from_iter(std::env::args()).await

utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ serde_json = { workspace = true }
1414
tempfile = { workspace = true }
1515
tracing = { workspace = true }
1616
tokio = { workspace = true, features = ["process"] }
17+
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
1718

1819
[dev-dependencies]
1920
similar-asserts = { workspace = true }

utils/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
//! "core" crates.
44
//!
55
mod command;
6+
mod tracing_util;
67
pub use command::*;
8+
pub use tracing_util::*;

utils/src/tracing_util.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! Helpers related to tracing, used by main entrypoints
2+
3+
/// Initialize tracing with the default configuration.
4+
pub fn initialize_tracing() {
5+
// Don't include timestamps and such because they're not really useful and
6+
// too verbose, and plus several log targets such as journald will already
7+
// include timestamps.
8+
let format = tracing_subscriber::fmt::format()
9+
.without_time()
10+
.with_target(false)
11+
.compact();
12+
// Log to stderr by default
13+
tracing_subscriber::fmt()
14+
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
15+
.event_format(format)
16+
.with_writer(std::io::stderr)
17+
.init();
18+
}

0 commit comments

Comments
 (0)