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

$ORT_ALLOW_VERBOSE_LOGGING_ON_RELEASEを導入 #10

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 14 additions & 0 deletions onnxruntime/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::env;

fn main() {
if boolean_env_var("ORT_ALLOW_DEBUG_LOGGING") {
qryxip marked this conversation as resolved.
Show resolved Hide resolved
println!("cargo:rustc-cfg=allow_debug_logging");
}
println!("cargo:rerun-if-env-changed=ORT_ALLOW_DEBUG_LOGGING");
}

fn boolean_env_var(name: &str) -> bool {
// Same as `ORT_USE_CUDA`.
let var = env::var(name).unwrap_or_default();
matches!(&*var.to_lowercase(), "1" | "yes" | "true" | "on")
}
2 changes: 1 addition & 1 deletion onnxruntime/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
};

use lazy_static::lazy_static;
use tracing::{debug, error, warn};
use tracing::{error, warn};

use onnxruntime_sys as sys;

Expand Down
44 changes: 33 additions & 11 deletions onnxruntime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,26 @@ macro_rules! extern_system_fn {
($(#[$meta:meta])* $vis:vis unsafe fn $($tt:tt)*) => ($(#[$meta])* $vis unsafe extern "C" fn $($tt)*);
}

// textual scopeで`trace!`と`debug!`を宣言することにより、`tracing`のそれらの`use`を封じる
Copy link
Member Author

@qryxip qryxip Dec 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

textual scopeについての解説はこれ。
Textual Scope - Macros By Example - The Rust Reference

私も思い付きで試してみるまで知らなかったが、下のmodやブロックにおける名前解決も邪魔できる。


macro_rules! trace {
($($tt:tt)*) => {
if cfg!(allow_debug_logging) {
::tracing::trace!($($tt)*)
} else {
}
};
}

macro_rules! debug {
($($tt:tt)*) => {
if cfg!(allow_debug_logging) {
::tracing::debug!($($tt)*)
} else {
}
};
}

pub mod download;
pub mod environment;
pub mod error;
Expand Down Expand Up @@ -205,7 +225,7 @@ mod onnxruntime {
//! to Rust's tracing logging instead.

use std::ffi::CStr;
use tracing::{debug, error, info, span, trace, warn, Level};
use tracing::{error, info, span, warn, Level};

use onnxruntime_sys as sys;

Expand Down Expand Up @@ -271,16 +291,18 @@ mod onnxruntime {
// Parse the code location
let code_location: CodeLocation = code_location.into();

let span = span!(
Level::TRACE,
"onnxruntime",
category = category.to_str().unwrap_or("<unknown>"),
file = code_location.file,
line_number = code_location.line_number,
function = code_location.function,
logid = logid.to_str().unwrap_or("<unknown>"),
);
let _enter = span.enter();
let _span = cfg!(allow_verbose_logging).then(|| {
span!(
Level::TRACE,
"onnxruntime",
category = category.to_str().unwrap_or("<unknown>"),
file = code_location.file,
line_number = code_location.line_number,
function = code_location.function,
logid = logid.to_str().unwrap_or("<unknown>"),
)
.entered()
});
Comment on lines -276 to +303
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これについては完全に無害だとは思うが、挙動の一貫性のためこうしておいた。

Copy link
Member Author

@qryxip qryxip Dec 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここにおける"span"は、このonnxruntime{…}の中。

2022-12-07T14:39:09.569061Z  INFO onnxruntime{category="onnxruntime" file="graph.cc" line_number="3559" function="CleanUnusedInitializersAndNodeArgs" logid=""}: onnxruntime::onnxruntime: "Removing initializer \'664\'. It is not used by any node and should be removed from the model."
2022-12-07T14:39:09.569084Z  INFO onnxruntime{category="onnxruntime" file="graph.cc" line_number="3559" function="CleanUnusedInitializersAndNodeArgs" logid=""}: onnxruntime::onnxruntime: "Removing initializer \'661\'. It is not used by any node and should be removed from the model."
2022-12-07T14:39:09.569096Z  INFO onnxruntime{category="onnxruntime" file="graph.cc" line_number="3559" function="CleanUnusedInitializersAndNodeArgs" logid=""}: onnxruntime::onnxruntime: "Removing initializer \'660\'. It is not used by any node and should be removed from the model."

Copy link
Member Author

@qryxip qryxip Dec 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(あとallow_debug_loggingのつもりでallow_verbose_loggingと書いていたので修正しました)

やっぱりallow_verbose_loggingの方が適切だと思ったのでそうしました。


match log_level {
Level::TRACE => trace!("{:?}", message),
Expand Down
2 changes: 0 additions & 2 deletions onnxruntime/src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use tracing::debug;

use onnxruntime_sys as sys;

use tracing::error;
Expand Down
2 changes: 1 addition & 1 deletion onnxruntime/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::os::windows::ffi::OsStrExt;
use std::env;

use ndarray::Array;
use tracing::{debug, error};
use tracing::error;

use onnxruntime_sys as sys;

Expand Down
1 change: 0 additions & 1 deletion onnxruntime/src/tensor/ort_owned_tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::{fmt::Debug, ops::Deref};

use ndarray::{Array, ArrayView};
use tracing::debug;

use onnxruntime_sys as sys;

Expand Down
2 changes: 1 addition & 1 deletion onnxruntime/src/tensor/ort_tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{ffi, fmt::Debug, ops::Deref};

use ndarray::Array;
use tracing::{debug, error};
use tracing::error;

use onnxruntime_sys as sys;

Expand Down