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
2 changes: 1 addition & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ setup:
source bin/activate-hermit
git submodule update --init --recursive
if [[ "$(cargo 2>&1)" == *"rustup could not choose a version of cargo to run"* ]]; then
rustup default 1.78.0
rustup default 1.80.0
rustup target add aarch64-apple-darwin
fi

Expand Down
43 changes: 43 additions & 0 deletions bound/kt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,49 @@
<jvmTarget>${kotlin.jvm.target}</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<includes>
<include>*Test</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>get-git-commit-id</id>
<goals>
<goal>exec</goal>
</goals>

<phase>initialize</phase>

<configuration>
<executable>git</executable>
<arguments>
<argument>rev-parse</argument>
<argument>--short</argument>
<argument>HEAD</argument>
</arguments>
<outputFile>${project.build.directory}/git-commit-id.txt</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
11 changes: 10 additions & 1 deletion bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@ package web5.sdk.rust
import java.io.File

internal val logLevel = System.getenv("WEB5_SDK_LOG_LEVEL")?.lowercase()
internal val gitCommitHash = run {
val commitFile = File("target/git-commit-id.txt")
if (commitFile.exists()) {
commitFile.readText().trim()
} else {
println("Git commit hash not found.")
""
}
}

internal fun log(message: String) {
if (logLevel == "debug") {
println("web5 sdk SystemArchitecture $message")
println("web5 sdk SystemArchitecture $gitCommitHash: $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 @@ -15,6 +15,7 @@ josekit = "0.8.6"
jsonpath-rust = "0.5.1"
jsonschema = { version = "0.18.0", default-features = false }
k256 = { version = "0.13.3", features = ["ecdsa", "jwk"] }
lazy_static = "1.5.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

can we update the Justfile to 1.80.0 and use the new LazyLock? I'm doing that over here https://github.com/TBD54566975/web5-rs/pull/283/files#diff-8b2fda406bc73cf365197448d20bd9ea1d05db17e9b8dca69881ad9dd9755f5d (scroll down to the tests)

tokio = "1.38.0"
rand = { workspace = true }
regex = "1.10.4"
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::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();

// Pass the commit hash to the compiler as an environment variable
println!("cargo:rustc-env=WEB5_GIT_COMMIT_HASH={}", git_hash_trimmed);
KendallWeihe marked this conversation as resolved.
Show resolved Hide resolved
}
27 changes: 27 additions & 0 deletions crates/web5/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,30 @@ pub mod dids;

#[cfg(test)]
mod test_helpers;

lazy_static::lazy_static! {
Copy link
Contributor

Choose a reason for hiding this comment

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

could we use the LazyLock or LazyCell here and then remove the addition of lazy_static in the Cargo.toml

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Upd8d

pub(crate) static ref LOG_LEVEL: Option<String> = {
std::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:expr, $($arg:tt)*) => {
if let Some(ref level) = *$crate::LOG_LEVEL {
if level == "DEBUG" {
println!("[DEBUG] {}:{}", env!("GIT_COMMIT_HASH"), format!($msg, $($arg)*));
}
}
};
($closure:expr) => {
if let Some(ref level) = *$crate::LOG_LEVEL {
if level == "DEBUG" {
let msg = $closure();
println!("[DEBUG] {}:{}", env!("GIT_COMMIT_HASH"), msg);
}
}
};
}
}
Loading