Skip to content

Commit

Permalink
WIP on logging proc macro
Browse files Browse the repository at this point in the history
  • Loading branch information
gbin committed May 23, 2024
1 parent f58b10c commit c18335f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
members = ["copper", "copper_derive", "copper_derive_test", "examples/config-gen", "examples/pluginload", "examples/simplelogger", "examples/v4lsrc"]
members = ["copper", "copper_derive", "copper_derive_test", "copper_log", "examples/config-gen", "examples/pluginload", "examples/simplelogger", "examples/v4lsrc"]
resolver = "2"
1 change: 1 addition & 0 deletions copper_derive_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ copper-derive = { path = "../copper_derive" }
simplelogger = { path = "../examples/simplelogger" }
v4lsrc = { path = "../examples/v4lsrc" }
copper = { path = "../copper" }
copper-log = { path = "../copper_log" }

[build-dependencies]
copper = { path = "../copper" }
Expand Down
3 changes: 3 additions & 0 deletions copper_derive_test/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use copper_derive::copper_runtime;
use copper_log::debug;

#[copper_runtime(config = "copperconfig.ron")]
struct MyApplication {}

fn main() {
debug!("Application created.");
let application = MyApplication::new().expect("Failed to create runtime.");
debug!("End of program.");
}
13 changes: 13 additions & 0 deletions copper_log/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "copper-log"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true


[dependencies]
proc-macro2 = { version = "1.0.83", features = ["span-locations"] }
quote = "1.0.36"
syn = { version = "2.0.65", features = ["full"] }
6 changes: 6 additions & 0 deletions copper_log/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use std::env;

fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
println!("cargo:rustc-env=OUT_DIR={}", out_dir);
}
26 changes: 26 additions & 0 deletions copper_log/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::{parse_macro_input, LitStr};

#[proc_macro]
pub fn debug(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as LitStr);
let message = input.value();

let expanded = quote! {
{
let log_message = #message;
println!("Log: {:?}", log_message);
log_message
}
};
println!("Found logging string {}", message);

// print the OUT env vairable
println!("OUT: {}", std::env::var("OUT_DIR").unwrap());

TokenStream::from(expanded)
}

0 comments on commit c18335f

Please sign in to comment.