Closed lopopolo closed 2 years ago
Moving forward with a PR is blocked on https://github.com/tokio-rs/tracing/pull/1173 because Artichoke denies time
in its dep tree.
https://github.com/artichoke/artichoke/pull/1576 takes the approach to remove the dependency on log
since the types of things being logged tended to be fatal errors which are better served by writing directly to stderr.
tracing
is much more feature-ful thanlog
and seems to be where the ecosystem is moving.tracing
can instrument Artichoke in more ways than just plaintext logs, e.g. timing functions.Below is a WIP patch that I put together, but this needs a more thought out design. This is what the patch looks like with debug logging enabled:
WIP patch
```patch diff --git a/Cargo.lock b/Cargo.lock index 910ef8373f..d533c71e3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,6 +18,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + [[package]] name = "artichoke" version = "0.1.0-pre.0" @@ -28,6 +37,8 @@ dependencies = [ "rustyline", "target-lexicon", "termcolor", + "tracing", + "tracing-subscriber", ] [[package]] @@ -40,7 +51,6 @@ dependencies = [ "cc", "intaglio", "itoa", - "log", "once_cell", "onig", "quickcheck", @@ -56,6 +66,7 @@ dependencies = [ "spinoso-symbol", "spinoso-time", "target-lexicon", + "tracing", ] [[package]] @@ -119,6 +130,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "byteorder" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b" + [[package]] name = "cc" version = "1.0.66" @@ -185,7 +202,7 @@ version = "2.33.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" dependencies = [ - "ansi_term", + "ansi_term 0.11.0", "atty", "bitflags", "strsim", @@ -291,6 +308,15 @@ dependencies = [ "cfg-if 0.1.10", ] +[[package]] +name = "matchers" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f099785f7595cc4b4553a174ce30dd7589ef93391ff414dbb67f62392b9e0ce1" +dependencies = [ + "regex-automata", +] + [[package]] name = "memchr" version = "2.3.4" @@ -381,6 +407,12 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" +[[package]] +name = "pin-project-lite" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439697af366c49a6d0a010c56a0d97685bc140ce0d377b13a2ea2aa42d64a827" + [[package]] name = "pkg-config" version = "0.3.19" @@ -472,6 +504,16 @@ dependencies = [ "thread_local", ] +[[package]] +name = "regex-automata" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae1ded71d66a4a97f5e961fd0cb25a5f366a42a41570d16a763a69c092c26ae4" +dependencies = [ + "byteorder", + "regex-syntax", +] + [[package]] name = "regex-syntax" version = "0.6.22" @@ -521,6 +563,15 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "sharded-slab" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79c719719ee05df97490f80a45acfc99e5a30ce98a1e4fb67aee422745ae14e3" +dependencies = [ + "lazy_static", +] + [[package]] name = "shlex" version = "0.1.1" @@ -617,6 +668,17 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +[[package]] +name = "syn" +version = "1.0.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc60a3d73ea6594cd712d830cc1f0390fd71542d8c8cd24e70cc54cdfd5e05d5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "target-lexicon" version = "0.11.1" @@ -650,6 +712,54 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "tracing" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f47026cdc4080c07e49b37087de021820269d996f581aac150ef9e5583eefe3" +dependencies = [ + "cfg-if 1.0.0", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80e0ccfc3378da0cce270c946b676a376943f5cd16aeba64568e7939806f4ada" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f50de3927f93d202783f4513cda820ab47ef17f624b03c096e86ef00c67e6b5f" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "tracing-subscriber" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1fa8f0c8f4c594e4fc9debc1990deab13238077271ba84dd853d54902ee3401" +dependencies = [ + "ansi_term 0.12.1", + "lazy_static", + "matchers", + "regex", + "sharded-slab", + "thread_local", + "tracing", + "tracing-core", +] + [[package]] name = "unicode-segmentation" version = "1.7.1" diff --git a/Cargo.toml b/Cargo.toml index 841fb82c5f..9a6a8d5da7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,12 +21,19 @@ categories = ["command-line-utilities"] clap = { version = "2.33", optional = true } rustyline = { version = "7", default-features = false, optional = true } termcolor = { version = "1.1", optional = true } +tracing = { version = "0.1", default-features = false, optional = true } [dependencies.artichoke-backend] version = "0.1" path = "artichoke-backend" default-features = false +[dependencies.tracing-subscriber] +version = "0.2" +default-features = false +features = ["env-filter", "fmt", "ansi", "registry"] +optional = true + [build-dependencies] chrono = { version = "0.4.19", default-features = false, features = ["clock"] } target-lexicon = "0.11.1" @@ -70,7 +77,7 @@ default = [ ] # Enable a CLI frontend for Artichoke, including a `ruby`-equivalent CLI and # REPL. -cli = ["backtrace", "clap", "rustyline"] +cli = ["backtrace", "clap", "rustyline", "tracing", "tracing-subscriber"] # Enable a module for formtting backtraces from Ruby exceptions. backtrace = ["termcolor"] # Enable all features of Ruby Core, Standard Library, and the underlying VM. diff --git a/artichoke-backend/Cargo.toml b/artichoke-backend/Cargo.toml index 25f127eb95..007a415309 100644 --- a/artichoke-backend/Cargo.toml +++ b/artichoke-backend/Cargo.toml @@ -15,7 +15,6 @@ artichoke-core = { version = "0.7", path = "../artichoke-core" } bstr = { version = "0.2, >= 0.2.2", default-features = false, features = ["std"] } intaglio = "1.2" itoa = "0.4" -log = "0.4, >= 0.4.5" once_cell = "1" regex = "1" scolapasta-string-escape = { version = "0.1", path = "../scolapasta-string-escape" } @@ -28,6 +27,7 @@ spinoso-regexp = { version = "0.1", path = "../spinoso-regexp", default-features spinoso-securerandom = { version = "0.1", path = "../spinoso-securerandom", optional = true } spinoso-symbol = { version = "0.1", path = "../spinoso-symbol" } spinoso-time = { version = "0.1", path = "../spinoso-time", optional = true } +tracing = { version = "0.1", default-features = false } [dependencies.onig] version = "6.1" diff --git a/artichoke-backend/src/artichoke.rs b/artichoke-backend/src/artichoke.rs index 2c73b93eab..70bbb2358a 100644 --- a/artichoke-backend/src/artichoke.rs +++ b/artichoke-backend/src/artichoke.rs @@ -1,6 +1,7 @@ use std::ffi::c_void; use std::ops::{Deref, DerefMut}; use std::ptr::NonNull; +use tracing::{span, trace, Level}; use crate::ffi::{self, InterpreterExtractError}; use crate::state::State; @@ -69,6 +70,9 @@ impl Artichoke { F: FnOnce(*mut sys::mrb_state) -> T, { if let Some(state) = self.state.take() { + let span = span!(Level::TRACE, "with_ffi_boundary"); + let _enter = span.enter(); + // Ensure we don't create multiple mutable references by moving the // `mrb` out of the `Artichoke` and converting to a raw pointer. // @@ -89,6 +93,7 @@ impl Artichoke { // moved back into the `mrb`. // 9. Replace `self` with the new interpreter. + trace!("Serializing interpreter"); // Step 1 let mrb = self.mrb.as_ptr(); @@ -96,9 +101,11 @@ impl Artichoke { (*mrb).ud = Box::into_raw(state) as *mut c_void; // Steps 5-7 + trace!("Crossing FFI boundary"); let result = func(mrb); // Step 8 + trace!("Deserializing interpreter"); let extracted = ffi::from_user_data(mrb)?; // Step 9 @@ -132,6 +139,9 @@ impl Artichoke { /// Consume an interpreter and free all live objects. pub fn close(mut self) { + let span = span!(Level::DEBUG, "interpreter close"); + let _enter = span.enter(); + // Safety: // // It is permissible to directly access the `*mut sys::mrb_state` @@ -161,8 +171,12 @@ impl Artichoke { // for Ruby types defined with type tag `MRB_TT_DATA`. unsafe { if let Some(parser) = parser { + let parser_close_span = span!(Level::DEBUG, "parser close"); + let _parser_close_enter = parser_close_span.enter(); parser.close(mrb); } + let mrb_close_span = span!(Level::DEBUG, "mrb_close"); + let _mrb_close_enter = mrb_close_span.enter(); sys::mrb_close(mrb); } diff --git a/artichoke-backend/src/def.rs b/artichoke-backend/src/def.rs index 52f8ef65ac..711251859e 100644 --- a/artichoke-backend/src/def.rs +++ b/artichoke-backend/src/def.rs @@ -3,6 +3,7 @@ use std::error; use std::ffi::{c_void, CStr}; use std::fmt; use std::ptr::NonNull; +use tracing::{error, span, Level}; use crate::class; use crate::class_registry::ClassRegistry; @@ -37,6 +38,9 @@ pub unsafe extern "C" fn box_unbox_free