Skip to content

Commit

Permalink
tracing: add tracing feature to shim out tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Aug 7, 2024
1 parent ef21aa9 commit 20c7016
Show file tree
Hide file tree
Showing 26 changed files with 425 additions and 245 deletions.
16 changes: 0 additions & 16 deletions crates/rune-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ mod from_value;
mod function;
mod hash;
mod inst_display;
mod instrument;
mod internals;
mod item;
mod macro_;
Expand Down Expand Up @@ -260,21 +259,6 @@ pub fn __internal_impl_any(input: proc_macro::TokenStream) -> proc_macro::TokenS
builder.expand().into()
}

#[proc_macro_attribute]
#[doc(hidden)]
pub fn instrument(
attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let attr = syn::parse_macro_input!(attr as instrument::Attr);
let internal_call = syn::parse_macro_input!(item as instrument::Expander);

internal_call
.expand(&attr)
.unwrap_or_else(to_compile_errors)
.into()
}

/// Shim for an ignored `#[stable]` attribute.
#[proc_macro_attribute]
#[doc(hidden)]
Expand Down
1 change: 1 addition & 0 deletions crates/rune-modules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ These are modules that can be used with the [Rune language].
[Rune Language]: https://rune-rs.github.io

See each module for documentation:
* [base64]
* [core]
* [fmt]
* [fs]
Expand Down
25 changes: 25 additions & 0 deletions crates/rune-tracing-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "rune-tracing-macros"
version = "0.14.0"
authors = ["John-John Tedro <[email protected]>"]
edition = "2021"
rust-version = "1.79"
description = "Macros for tracing the Rune Language, an embeddable dynamic programming language for Rust."
documentation = "https://docs.rs/rune"
readme = "README.md"
homepage = "https://github.com/rune-rs/rune"
repository = "https://github.com/rune-rs/rune"
license = "MIT OR Apache-2.0"
keywords = ["language", "scripting", "scripting-language"]
categories = ["parser-implementations"]

[dependencies]
syn = { version = "2.0.16", features = ["full"] }
quote = { version = "1.0.27" }
proc-macro2 = { version = "1.0.56" }

[lib]
proc-macro = true

[dev-dependencies]
rune = { path = "../rune" }
23 changes: 23 additions & 0 deletions crates/rune-tracing-macros/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<img alt="rune logo" src="https://raw.githubusercontent.com/rune-rs/rune/main/assets/icon.png" />
<br>
<a href="https://rune-rs.github.io"><b>Visit the site 🌐</b></a>
&mdash;
<a href="https://rune-rs.github.io/book/"><b>Read the book 📖</b></a>

# rune-tracing-macros

<a href="https://github.com/rune-rs/rune"><img alt="github" src="https://img.shields.io/badge/github-rune--rs/rune-8da0cb?style=for-the-badge&logo=github" height="20"></a>
<a href="https://crates.io/crates/rune-tracing-macros"><img alt="crates.io" src="https://img.shields.io/crates/v/rune-tracing-macros.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20"></a>
<a href="https://docs.rs/rune-tracing-macros"><img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-rune--tracing--macros-66c2a5?style=for-the-badge&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20"></a>
<a href="https://github.com/rune-rs/rune/actions?query=branch%3Amain"><img alt="build status" src="https://img.shields.io/github/actions/workflow/status/rune-rs/rune/ci.yml?branch=main&style=for-the-badge" height="20"></a>
<a href="https://discord.gg/v5AeNkT"><img alt="chat on discord" src="https://img.shields.io/discord/558644981137670144.svg?logo=discord&style=flat-square" height="20"></a>
<br>
<br>

Macros for tracing the Rune Language, an embeddable dynamic programming language for Rust.

<br>

## Usage

This is part of the [Rune Language](https://rune-rs.github.io).
File renamed without changes.
61 changes: 61 additions & 0 deletions crates/rune-tracing-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! <img alt="rune logo" src="https://raw.githubusercontent.com/rune-rs/rune/main/assets/icon.png" />
//! <br>
//! <a href="https://github.com/rune-rs/rune"><img alt="github" src="https://img.shields.io/badge/github-rune--rs/rune-8da0cb?style=for-the-badge&logo=github" height="20"></a>
//! <a href="https://crates.io/crates/rune-tracing-macros"><img alt="crates.io" src="https://img.shields.io/crates/v/rune-tracing-macros.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20"></a>
//! <a href="https://docs.rs/rune-tracing-macros"><img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-rune--tracing--macros-66c2a5?style=for-the-badge&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20"></a>
//! <a href="https://discord.gg/v5AeNkT"><img alt="chat on discord" src="https://img.shields.io/discord/558644981137670144.svg?logo=discord&style=flat-square" height="20"></a>
//! <br>
//! Minimum support: Rust <b>1.79+</b>.
//! <br>
//! <br>
//! <a href="https://rune-rs.github.io"><b>Visit the site 🌐</b></a>
//! &mdash;
//! <a href="https://rune-rs.github.io/book/"><b>Read the book 📖</b></a>
//! <br>
//! <br>
//!
//! Macros for tracing the Rune Language, an embeddable dynamic programming language for Rust.
//!
//! <br>
//!
//! ## Usage
//!
//! This is part of the [Rune Language](https://rune-rs.github.io).
#![allow(clippy::manual_map)]

extern crate proc_macro;

mod instrument;

#[proc_macro_attribute]
pub fn instrument_ast(
attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let attr = syn::parse_macro_input!(attr as instrument::Attr);
let internal_call = syn::parse_macro_input!(item as instrument::Expander);

internal_call
.expand(&attr)
.unwrap_or_else(to_compile_errors)
.into()
}

fn to_compile_errors<I>(errors: I) -> proc_macro2::TokenStream
where
I: IntoIterator<Item = syn::Error>,
{
let compile_errors = errors.into_iter().map(syn::Error::into_compile_error);
::quote::quote!(#(#compile_errors)*)
}

/// Passthrough attribute macro.
#[proc_macro_attribute]
#[doc(hidden)]
pub fn passthrough(
_: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
item
}
23 changes: 23 additions & 0 deletions crates/rune-tracing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "rune-tracing"
version = "0.14.0"
authors = ["John-John Tedro <[email protected]>"]
edition = "2021"
rust-version = "1.79"
description = "Rune tracing shims for the Rune Language, an embeddable dynamic programming language for Rust."
documentation = "https://docs.rs/rune"
readme = "README.md"
homepage = "https://github.com/rune-rs/rune"
repository = "https://github.com/rune-rs/rune"
license = "MIT OR Apache-2.0"
keywords = ["language", "scripting", "scripting-language"]
categories = ["parser-implementations"]

[features]
default = []
enabled = ["dep:tracing"]

[dependencies]
rune-tracing-macros = { path = "../rune-tracing-macros", version = "=0.14.0", default-features = false }

tracing = { version = "0.1.37", default-features = false, optional = true, features = ["attributes"] }
17 changes: 17 additions & 0 deletions crates/rune-tracing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<img alt="rune logo" src="https://raw.githubusercontent.com/rune-rs/rune/main/assets/icon.png" />
<br>
<a href="https://rune-rs.github.io"><b>Visit the site 🌐</b></a>
&mdash;
<a href="https://rune-rs.github.io/book/"><b>Read the book 📖</b></a>

# rune-tracing

<a href="https://github.com/rune-rs/rune"><img alt="github" src="https://img.shields.io/badge/github-rune--rs/rune-8da0cb?style=for-the-badge&logo=github" height="20"></a>
<a href="https://crates.io/crates/rune-tracing"><img alt="crates.io" src="https://img.shields.io/crates/v/rune-tracing.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20"></a>
<a href="https://docs.rs/rune-tracing"><img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-rune--tracing-66c2a5?style=for-the-badge&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20"></a>
<a href="https://github.com/rune-rs/rune/actions?query=branch%3Amain"><img alt="build status" src="https://img.shields.io/github/actions/workflow/status/rune-rs/rune/ci.yml?branch=main&style=for-the-badge" height="20"></a>
<a href="https://discord.gg/v5AeNkT"><img alt="chat on discord" src="https://img.shields.io/discord/558644981137670144.svg?logo=discord&style=flat-square" height="20"></a>
<br>
<br>

Rune tracing shims for the Rune Language, an embeddable dynamic programming language for Rust.
56 changes: 56 additions & 0 deletions crates/rune-tracing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! <img alt="rune logo" src="https://raw.githubusercontent.com/rune-rs/rune/main/assets/icon.png" />
//! <br>
//! <a href="https://github.com/rune-rs/rune"><img alt="github" src="https://img.shields.io/badge/github-rune--rs/rune-8da0cb?style=for-the-badge&logo=github" height="20"></a>
//! <a href="https://crates.io/crates/rune-tracing"><img alt="crates.io" src="https://img.shields.io/crates/v/rune-tracing.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20"></a>
//! <a href="https://docs.rs/rune-tracing"><img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-rune--tracing-66c2a5?style=for-the-badge&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20"></a>
//! <a href="https://discord.gg/v5AeNkT"><img alt="chat on discord" src="https://img.shields.io/discord/558644981137670144.svg?logo=discord&style=flat-square" height="20"></a>
//! <br>
//! Minimum support: Rust <b>1.79+</b>.
//! <br>
//! <br>
//! <a href="https://rune-rs.github.io"><b>Visit the site 🌐</b></a>
//! &mdash;
//! <a href="https://rune-rs.github.io/book/"><b>Read the book 📖</b></a>
//! <br>
//! <br>
//!
//! Rune tracing shims for the Rune Language, an embeddable dynamic programming language for Rust.
#![no_std]

#[cfg(feature = "enabled")]
mod r#impl {
pub use rune_tracing_macros::instrument_ast;
pub use tracing::{
debug, enabled, error, event, field, if_log_enabled, info, instrument, level_enabled, span,
trace, warn, Level,
};
}

#[cfg(not(feature = "enabled"))]
mod r#impl {
pub use rune_tracing_macros::passthrough as instrument;
pub use rune_tracing_macros::passthrough as instrument_ast;

#[macro_export]
macro_rules! __noop {
($($arg:tt)*) => {
()
};
}

#[macro_export]
macro_rules! __enabled {
($($arg:tt)*) => {
false
};
}

pub use __enabled as enabled;
pub use __noop as span;
pub use __noop as trace;
pub use __noop as info;
pub use __noop as error;
pub use __noop as warn;
}

pub use self::r#impl::*;
3 changes: 2 additions & 1 deletion crates/rune/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ unexpected_cfgs = { level = "warn", check-cfg = ['cfg(rune_nightly, rune_docsrs,

[features]
default = ["emit", "std"]
tracing = ["tracing/enabled"]
emit = ["std", "codespan-reporting"]
bench = []
workspace = ["std", "toml", "semver", "relative-path", "serde-hashkey", "linked-hash-map"]
Expand All @@ -35,6 +36,7 @@ alloc = ["anyhow", "rune-alloc/alloc", "rune-core/alloc", "once_cell/alloc", "se
rune-macros = { version = "=0.14.0", path = "../rune-macros" }
rune-core = { version = "=0.14.0", path = "../rune-core", features = ["musli"] }
rune-alloc = { version = "0.14.0", path = "../rune-alloc", features = ["serde"], default-features = false }
tracing = { package = "rune-tracing", version = "0.14.0", path = "../rune-tracing" }

futures-core = { version = "0.3.28", default-features = false }
futures-util = { version = "0.3.28", default-features = false, features = ["alloc"] }
Expand All @@ -43,7 +45,6 @@ num = { version = "0.4.0", default-features = false, features = ["alloc"] }
pin-project = "1.1.0"
ryu = "1.0.13"
serde = { version = "1.0.163", default-features = false, features = ["derive", "rc"] }
tracing = { version = "0.1.37", default-features = false, features = ["attributes"] }
musli = { version = "0.0.121", default-features = false, features = ["alloc"] }
once_cell = { version = "1.18.0", default-features = false, features = ["critical-section"] }

Expand Down
11 changes: 7 additions & 4 deletions crates/rune/src/cli/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use anyhow::{anyhow, Context as _, Result};
use crate::alloc::{Vec, VecDeque};
use crate::cli::{visitor, Io, SharedFlags};
use crate::compile::FileSourceLoader;
use crate::Diagnostics;
use crate::{Context, Hash, ItemBuf, Options, Source, Sources, Unit};
use crate::{Context, Diagnostics, Hash, ItemBuf, Options, Source, Sources, Unit};

pub(super) struct Load {
pub(super) unit: Arc<Unit>,
Expand Down Expand Up @@ -46,8 +45,12 @@ pub(super) fn load(
tracing::trace!("Using cache: {}", bytecode_path.display());
Some(Arc::new(unit))
}
Err(e) => {
tracing::error!("Failed to deserialize: {}: {}", bytecode_path.display(), e);
Err(_error) => {
tracing::error!(
"Failed to deserialize: {}: {}",
bytecode_path.display(),
_error
);
None
}
}
Expand Down
Loading

0 comments on commit 20c7016

Please sign in to comment.