Skip to content

Commit ef0596d

Browse files
Merge pull request #542 from theseus-rs/optimize-logging-startup
feat: optimize logging startup
2 parents f50c725 + 33c5648 commit ef0596d

File tree

10 files changed

+15
-38
lines changed

10 files changed

+15
-38
lines changed

Cargo.lock

Lines changed: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ test-log = "0.2.18"
6868
thiserror = "2.0.12"
6969
thread-priority = "2.1.0"
7070
tokio = { version = "1.47.1", default-features = false, features = ["macros", "rt", "sync"] }
71-
tracing = "0.1.41"
71+
tracing = { version = "0.1.41", default-features = false, features = ["release_max_level_info", "std"] }
7272
tracing-subscriber = "0.3.19"
7373
walkdir = "2.5.0"
7474
whoami = "1.6.0"

ristretto_classloader/src/class_path.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::Result;
33
use crate::class_path_entry::ClassPathEntry;
44
use ristretto_classfile::ClassFile;
55
use std::fmt::Display;
6-
use tracing::{info, instrument};
6+
use tracing::info;
77

88
/// Represents a class path.
99
///
@@ -50,7 +50,6 @@ impl ClassPath {
5050
/// # Errors
5151
///
5252
/// if the class file is not found or cannot be read.
53-
#[instrument(level = "trace", fields(name = ?name.as_ref()), skip(self))]
5453
pub async fn read_class<S: AsRef<str>>(&self, name: S) -> Result<ClassFile> {
5554
let name = name.as_ref();
5655

ristretto_classloader/src/class_path_entry/directory.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use ristretto_classfile::ClassFile;
44
use std::fmt::Debug;
55
use std::path::PathBuf;
66
use std::{fs, io};
7-
use tracing::instrument;
87
use walkdir::WalkDir;
98

109
/// A directory in the class path.
@@ -35,7 +34,6 @@ impl Directory {
3534
/// # Errors
3635
///
3736
/// if the class file is not found or cannot be read.
38-
#[instrument(level = "trace", fields(name = ?name.as_ref()), skip(self))]
3937
pub fn read_class<S: AsRef<str>>(&self, name: S) -> Result<ClassFile> {
4038
let name = name.as_ref();
4139
let parts = name.split('.').collect::<Vec<_>>();

ristretto_classloader/src/class_path_entry/jar.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::str::FromStr;
99
use std::sync::Arc;
1010
use std::{fs, io};
1111
use tokio::sync::RwLock;
12-
use tracing::instrument;
1312
use zip::ZipArchive;
1413

1514
/// A jar or zip in the class path.
@@ -80,7 +79,6 @@ impl Jar {
8079
/// # Errors
8180
///
8281
/// if the file is not found or cannot be read.
83-
#[instrument(level = "trace", fields(name = ?name.as_ref()), skip(self))]
8482
pub async fn read_file<S: AsRef<str>>(&self, name: S) -> Result<Option<Vec<u8>>> {
8583
let mut archive = self.archive.write().await;
8684
archive.load_file(name.as_ref()).await
@@ -91,7 +89,6 @@ impl Jar {
9189
/// # Errors
9290
///
9391
/// if the class file is not found or cannot be read.
94-
#[instrument(level = "trace", fields(name = ?name.as_ref()), skip(self))]
9592
pub async fn read_class<S: AsRef<str>>(&self, name: S) -> Result<ClassFile> {
9693
let name = name.as_ref();
9794
let mut archive = self.archive.write().await;
@@ -236,7 +233,6 @@ impl Archive {
236233
/// # Errors
237234
///
238235
/// if the jar cannot be read or the class file cannot be loaded.
239-
#[instrument(level = "trace")]
240236
async fn load_class_file(&mut self, class_name: &str) -> Result<Option<ClassFile>> {
241237
let class_file_name = format!("{class_name}.class");
242238
let file = self.load_file(&class_file_name).await?;
@@ -254,7 +250,6 @@ impl Archive {
254250
/// # Errors
255251
///
256252
/// if the jar cannot be read or the class file cannot be loaded.
257-
#[instrument(level = "trace")]
258253
async fn load_file(&mut self, file_name: &str) -> Result<Option<Vec<u8>>> {
259254
let zip_archive = self.zip_archive().await?;
260255
if let Some(index) = zip_archive.index_for_name(file_name) {

ristretto_classloader/src/class_path_entry/model.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::class_path_entry::jar::Jar;
44
use ristretto_classfile::ClassFile;
55
use std::fmt::Debug;
66
use std::path::PathBuf;
7-
use tracing::instrument;
87

98
/// Represents a class path entry.
109
#[derive(Clone, Debug, PartialEq)]
@@ -44,7 +43,6 @@ impl ClassPathEntry {
4443
/// # Errors
4544
///
4645
/// if the class file cannot be read.
47-
#[instrument(level = "trace", fields(name = ?name.as_ref()), skip(self))]
4846
pub async fn read_class<S: AsRef<str>>(&self, name: S) -> Result<ClassFile> {
4947
match self {
5048
ClassPathEntry::Directory(directory) => directory.read_class(name),

ristretto_classloader/src/runtime/bootstrap.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::path::{Path, PathBuf};
77
use std::sync::Arc;
88
use std::{env, io};
99
use tar::Archive;
10-
use tracing::{debug, instrument, warn};
10+
use tracing::{debug, warn};
1111

1212
pub const DEFAULT_JAVA_VERSION: &str = "21.0.8.9.1";
1313

@@ -26,8 +26,7 @@ pub async fn default_class_loader() -> Result<(PathBuf, String, Arc<ClassLoader>
2626
/// # Errors
2727
///
2828
/// An error will be returned if the class loader cannot be created.
29-
#[instrument(level = "debug")]
30-
pub async fn home_class_loader(java_home: &PathBuf) -> Result<(PathBuf, String, Arc<ClassLoader>)> {
29+
pub async fn home_class_loader(java_home: &Path) -> Result<(PathBuf, String, Arc<ClassLoader>)> {
3130
let version_file = java_home.join("version.txt");
3231
// Corretto version 8 does not have a release file, but includes a version.txt file. Since most
3332
// versions of Corretto include a version.txt file, and it should be faster to process, we can
@@ -65,7 +64,7 @@ pub async fn home_class_loader(java_home: &PathBuf) -> Result<(PathBuf, String,
6564
let class_path = get_class_path(&java_version, java_home)?;
6665
let class_loader = ClassLoader::new("bootstrap", class_path);
6766
register_primitives(&class_loader).await?;
68-
Ok((java_home.clone(), java_version, class_loader))
67+
Ok((java_home.to_path_buf(), java_version, class_loader))
6968
}
7069

7170
/// Get a class loader for the given Java runtime version. If the version is not installed, the
@@ -75,7 +74,6 @@ pub async fn home_class_loader(java_home: &PathBuf) -> Result<(PathBuf, String,
7574
/// # Errors
7675
///
7776
/// An error will be returned if the class loader cannot be created.
78-
#[instrument(level = "debug")]
7977
pub async fn version_class_loader(version: &str) -> Result<(PathBuf, String, Arc<ClassLoader>)> {
8078
let mut version = version.to_string();
8179
#[cfg(target_family = "wasm")]
@@ -172,7 +170,6 @@ fn get_class_path(version: &str, installation_dir: &Path) -> Result<ClassPath> {
172170
/// # Errors
173171
///
174172
/// An error will be returned if the archive cannot be extracted.
175-
#[instrument(level = "debug", skip(archive))]
176173
async fn extract_archive(
177174
version: &str,
178175
file_name: &str,

ristretto_classloader/src/runtime/util.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use reqwest::header;
55
use std::env;
66
use std::env::consts;
77
use std::sync::LazyLock;
8-
use tracing::{debug, instrument};
8+
use tracing::debug;
99

1010
const DEFAULT_MAJOR_VERSION: u64 = 21;
1111
const GITHUB_API_VERSION_HEADER: &str = "X-GitHub-Api-Version";
@@ -36,7 +36,6 @@ static USER_AGENT: LazyLock<String> = LazyLock::new(|| {
3636
/// # Errors
3737
///
3838
/// An error will be returned if the request fails or if the version requirement is not supported.
39-
#[instrument(level = "debug")]
4039
pub(crate) async fn get_runtime_archive(version: &str) -> Result<(String, String, Vec<u8>)> {
4140
let version = if version == "*" {
4241
DEFAULT_MAJOR_VERSION.to_string()
@@ -73,7 +72,6 @@ pub(crate) async fn get_runtime_archive(version: &str) -> Result<(String, String
7372
/// # Errors
7473
///
7574
/// An error will be returned if the request fails
76-
#[instrument(level = "debug")]
7775
async fn download_archive(version: &str) -> Result<(String, Vec<u8>)> {
7876
let client = Client::new();
7977
let mut headers = header::HeaderMap::new();
@@ -132,7 +130,6 @@ async fn download_archive(version: &str) -> Result<(String, Vec<u8>)> {
132130
/// # Errors
133131
///
134132
/// An error will be returned if the request fails
135-
#[instrument(level = "debug")]
136133
async fn get_release_versions(major_version: &str) -> Result<Vec<String>> {
137134
let url = format!("https://api.github.com/repos/corretto/corretto-{major_version}/releases");
138135
let client = Client::new();

ristretto_cli/src/logging.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@ use ristretto_vm::Result;
33
use tracing_subscriber::filter::EnvFilter;
44
use tracing_subscriber::fmt;
55

6+
const LOG_ENV_VAR: &str = "RISTRETTO_LOG";
7+
68
/// Initializes the logging system.
79
pub(crate) fn initialize() -> Result<()> {
10+
if std::env::var_os(LOG_ENV_VAR).is_none() {
11+
return Ok(());
12+
}
13+
814
let format = tracing_subscriber::fmt::format()
915
.with_level(true)
10-
.with_target(false)
11-
.with_thread_ids(false)
1216
.with_thread_names(true)
1317
.with_timer(fmt::time::uptime())
1418
.compact();
1519

1620
let cranelift_directive = "cranelift=warn"
1721
.parse()
1822
.map_err(|error| InternalError(format!("{error}")))?;
19-
let filter = EnvFilter::from_env("JAVA_LOG").add_directive(cranelift_directive);
23+
let filter = EnvFilter::from_env(LOG_ENV_VAR).add_directive(cranelift_directive);
2024
tracing_subscriber::fmt()
2125
.with_env_filter(filter)
2226
.fmt_fields(fmt::format::DefaultFields::new())

ristretto_cli/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ async fn main() -> Result<()> {
3737
async fn main() -> Result<()> {
3838
startup_trace!("[cli] entry point");
3939
let cli = Cli::parse();
40+
startup_trace!("[cli] argument parse");
4041
logging::initialize()?;
41-
startup_trace!("[cli] initialization");
42+
startup_trace!("[cli] logging initialize");
4243
if common_main(cli).await.is_err() {
4344
std::process::exit(1);
4445
}

0 commit comments

Comments
 (0)