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

Add debug logging #285

Closed
wants to merge 17 commits into from
3 changes: 3 additions & 0 deletions .github/workflows/build-cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
- main
pull_request:

env:
WEB5_SDK_LOG_LEVEL: debug

jobs:
build_aarch64_apple_darwin:
runs-on: macos-latest
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ Cargo.lock

# Do not put native binaries in source control
bound/kt/src/main/resources/*.dylib
bound/kt/src/main/resources/*.so
bound/kt/src/main/resources/*.so

# Created by crate on build
crates/web5/src/resources/git_sha.txt
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"crates/web5",
"crates/web5_cli",
"crates/web5_proc_macros",
"bindings/web5_uniffi",
"bindings/web5_uniffi_wrapper",
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ COPY bindings/web5_uniffi_wrapper ./bindings/web5_uniffi_wrapper
COPY bindings/web5_uniffi ./bindings/web5_uniffi
COPY crates/web5 ./crates/web5
COPY crates/web5_cli ./crates/web5_cli
COPY crates/web5_proc_macros ./crates/web5_proc_macros

# Execute the build
RUN cargo build --release --package web5_uniffi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ COPY bindings/web5_uniffi_wrapper ./bindings/web5_uniffi_wrapper
COPY bindings/web5_uniffi ./bindings/web5_uniffi
COPY crates/web5 ./crates/web5
COPY crates/web5_cli ./crates/web5_cli
COPY crates/web5_proc_macros ./crates/web5_proc_macros

# Build the static lib (override the lib type)
RUN sed -i 's/crate-type = \["cdylib"\]/crate-type = \["staticlib"\]/' bindings/web5_uniffi/Cargo.toml
Expand Down
1 change: 0 additions & 1 deletion bindings/web5_uniffi_wrapper/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,3 @@ impl From<InnerWeb5Error> for Web5Error {
}

pub type Result<T> = std::result::Result<T, Web5Error>;

2 changes: 1 addition & 1 deletion bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ internal val logLevel = System.getenv("WEB5_SDK_LOG_LEVEL")?.lowercase()

internal fun log(message: String) {
if (logLevel == "debug") {
println("web5 sdk SystemArchitecture $message")
println("web5 sdk SystemArchitecture: $message")
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/web5/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ simple-dns = "0.7.0"
thiserror = { workspace = true }
url = "2.5.0"
uuid = { workspace = true }
web5_proc_macros = { path = "../web5_proc_macros" }
zbase32 = "0.1.2"

[dev-dependencies]
Expand Down
18 changes: 18 additions & 0 deletions crates/web5/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::{fs, process::Command};

fn main() {
// Execute the `git rev-parse HEAD` command to get the current commit hash
let output = Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.expect("Failed to execute git command");

// Convert the output to a string
let git_hash = String::from_utf8(output.stdout).expect("Invalid UTF-8 sequence");

// Remove the newline character from the commit hash
let git_hash_trimmed = git_hash.trim();

let dest_path = format!("src/resources/git_sha.txt");
fs::write(dest_path, git_hash_trimmed).expect("Unable to write file");
}
43 changes: 42 additions & 1 deletion crates/web5/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{env, sync::LazyLock};

pub mod credentials;
pub mod crypto;
pub mod dids;
Expand All @@ -9,4 +11,43 @@ pub mod rfc3339;
#[cfg(test)]
mod test_helpers;
#[cfg(test)]
mod test_vectors;
mod test_vectors;

pub const GIT_COMMIT_HASH: &str = include_str!("resources/git_sha.txt");

// TODO: https://github.com/TBD54566975/web5-rs/issues/287
#[allow(dead_code)]
static LOG_LEVEL: LazyLock<Option<String>> = LazyLock::new(|| env::var("WEB5_SDK_LOG_LEVEL").ok());

pub(crate) mod logging {
KendallWeihe marked this conversation as resolved.
Show resolved Hide resolved
#[macro_export]
macro_rules! log_dbg {
($msg:literal $(, $arg:tt)*) => {
if let Some(ref level) = *$crate::LOG_LEVEL {
if level.to_lowercase() == "debug" {
println!("[DEBUG] {}: {}", $crate::GIT_COMMIT_HASH, format!($msg, $($arg)*));
}
}
};
($closure:expr) => {
if let Some(ref level) = *$crate::LOG_LEVEL {
if level.to_lowercase() == "debug" {
let msg = $closure();
println!("[DEBUG] {}: {}", $crate::GIT_COMMIT_HASH, msg);
}
}
};
}
}

#[cfg(test)]
mod test {
use crate::log_dbg;

#[test]
fn can_log_dbg() {
log_dbg!("Log debugging without arguments");
log_dbg!("Log debugging with arguments {}", "Some value");
log_dbg!(|| { 2 + 2 });
}
}
Empty file.
1 change: 1 addition & 0 deletions crates/web5_cli/build/x86_64_unknown_linux_gnu/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ COPY bindings/web5_uniffi_wrapper ./bindings/web5_uniffi_wrapper
COPY bindings/web5_uniffi ./bindings/web5_uniffi
COPY crates/web5 ./crates/web5
COPY crates/web5_cli ./crates/web5_cli
COPY crates/web5_proc_macros ./crates/web5_proc_macros

# Execute the build
RUN cargo build --release --package web5_cli
Expand Down
1 change: 1 addition & 0 deletions crates/web5_cli/build/x86_64_unknown_linux_musl/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ COPY bindings/web5_uniffi_wrapper ./bindings/web5_uniffi_wrapper
COPY bindings/web5_uniffi ./bindings/web5_uniffi
COPY crates/web5 ./crates/web5
COPY crates/web5_cli ./crates/web5_cli
COPY crates/web5_proc_macros ./crates/web5_proc_macros

RUN cargo build --release --package web5_cli

Expand Down
15 changes: 15 additions & 0 deletions crates/web5_proc_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "web5_proc_macros"
version = "0.1.0"
edition = "2021"
homepage.workspace = true
repository.workspace = true
license-file.workspace = true

[dependencies]
proc-macro2 = "1.0.86"
quote = "1.0.36"
syn = "2.0.75"

[lib]
proc-macro = true
23 changes: 23 additions & 0 deletions crates/web5_proc_macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use std::process::Command;

#[proc_macro]
pub fn git_sha(_input: TokenStream) -> TokenStream {
let output = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
.expect("Failed to execute git command");

let git_hash = String::from_utf8(output.stdout)
.expect("Invalid UTF-8 sequence")
.trim()
.to_string();

let expanded = quote! {
#git_hash
};

TokenStream::from(expanded)
}