-
Notifications
You must be signed in to change notification settings - Fork 32
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
feat: add logging macro #196
Closed
TropicalDog17
wants to merge
8
commits into
keep-starknet-strange:main
from
TropicalDog17:feat/log-macro
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
009358a
add macros
TropicalDog17 4509808
impl logging macros
TropicalDog17 c69d376
Merge branch 'main' into feat/log-macro
TropicalDog17 0c78897
lint
TropicalDog17 5cabfd8
refactor
TropicalDog17 5e061ed
refactor, add cairo test
TropicalDog17 ce7ff20
fmt
TropicalDog17 4b4809f
update
TropicalDog17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ license-file = "LICENSE" | |
|
||
[workspace.dependencies] | ||
cairo_test = "2.8.0" | ||
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "logging" | ||
version = "0.1.0" | ||
edition = "2024_07" | ||
|
||
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html | ||
[dev-dependencies] | ||
cairo_test = "2.8.2" | ||
|
||
[features] | ||
default = ["log_level_none"] | ||
log_level_trace = [] | ||
log_level_debug = [] | ||
log_level_none = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod log; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#[cfg(feature: 'log_level_trace')] | ||
pub const LOG_LEVEL_TRACE: bool = true; | ||
|
||
#[cfg(feature: 'log_level_trace')] | ||
pub const LOG_LEVEL_DEBUG: bool = true; | ||
|
||
#[cfg(feature: 'log_level_debug')] | ||
pub const LOG_LEVEL_TRACE: bool = false; | ||
|
||
#[cfg(feature: 'log_level_debug')] | ||
pub const LOG_LEVEL_DEBUG: bool = true; | ||
|
||
#[cfg(feature: 'log_level_none')] | ||
pub const LOG_LEVEL_TRACE: bool = false; | ||
|
||
#[cfg(feature: 'log_level_none')] | ||
pub const LOG_LEVEL_DEBUG: bool = false; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Cargo.toml | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Package should be called logging not macros. |
||
[package] | ||
name = "raito_macros" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
bigdecimal = "0.4.5" | ||
cairo-lang-macro = "0.1" | ||
cairo-lang-parser = "2.8.0" | ||
cairo-lang-syntax = "2.8.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "raito_macros" | ||
version = "0.1.0" | ||
edition = "2024_07" | ||
|
||
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html | ||
[cairo-plugin] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod logging; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
use cairo_lang_macro::{inline_macro, Diagnostic, ProcMacroResult, TokenStream}; | ||
use cairo_lang_parser::utils::SimpleParserDatabase; | ||
use cairo_lang_syntax::node::kind::SyntaxKind; | ||
|
||
#[inline_macro] | ||
pub fn log(token_stream: TokenStream) -> ProcMacroResult { | ||
let db = SimpleParserDatabase::default(); | ||
let (parsed, _diag) = db.parse_virtual_with_diagnostics(token_stream); | ||
|
||
let mut args = parsed | ||
.descendants(&db) | ||
.filter(|node| matches!(node.kind(&db), SyntaxKind::Arg)); | ||
|
||
// Extract log level | ||
let level = match args.next() { | ||
Some(node) => node.get_text(&db), | ||
_ => return create_error_result("Invalid log level"), | ||
}; | ||
|
||
// Validate log level | ||
if !["\"TRACE\"", "\"DEBUG\""].contains(&level.as_str()) { | ||
return create_error_result("Invalid log level. Use TRACE or DEBUG"); | ||
} | ||
|
||
// Extract format string | ||
let format_string = match args.next() { | ||
Some(node) => node.get_text(&db), | ||
_ => return create_error_result("Invalid format string"), | ||
}; | ||
|
||
// Extract remaining arguments | ||
let log_args: Vec<String> = args | ||
.map(|node| { | ||
if matches!(node.kind(&db), SyntaxKind::Arg) { | ||
Ok(node.get_text(&db)) | ||
} else { | ||
Err(()) | ||
} | ||
}) | ||
.collect::<Result<Vec<String>, ()>>() | ||
.unwrap_or_else(|_| { | ||
create_error_result("Invalid log argument"); | ||
Vec::new() | ||
}); | ||
|
||
// Generate the log statement | ||
let log_statement = generate_log_statement(&level, &format_string, &log_args); | ||
|
||
println!("Log statement: {}", log_statement); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessary? |
||
ProcMacroResult::new(TokenStream::new(log_statement)) | ||
} | ||
|
||
fn create_error_result(message: &str) -> ProcMacroResult { | ||
ProcMacroResult::new(TokenStream::empty()).with_diagnostics(Diagnostic::error(message).into()) | ||
} | ||
|
||
fn generate_log_statement(level: &str, format_string: &str, log_args: &[String]) -> String { | ||
let level_str = level.trim_matches('"'); | ||
let condition = match level_str { | ||
"TRACE" => "LOG_LEVEL_TRACE", | ||
"DEBUG" => "LOG_LEVEL_DEBUG", | ||
_ => unreachable!(), | ||
}; | ||
let level_str = format!("{}", level); | ||
let print_statement = if log_args.is_empty() { | ||
format!( | ||
"println!(\"{{}}: {{}}\", format!({}), format!({}))", | ||
level_str, format_string | ||
) | ||
} else { | ||
format!( | ||
"println!(\"{{}}: {{}}\", format!({}), format!({}, {}))", | ||
level_str, | ||
format_string, | ||
log_args.join(", ") | ||
) | ||
}; | ||
|
||
format!("if logging::{} {{ {} }}", condition, print_statement) | ||
} | ||
mod tests { | ||
use super::*; | ||
use cairo_lang_macro::TokenStream; | ||
|
||
// Helper function to expand macros | ||
#[allow(unused)] | ||
fn expand_macro(macro_fn: fn(TokenStream) -> ProcMacroResult, input: &str) -> String { | ||
let token_stream = TokenStream::new(input.to_string()); | ||
let result = macro_fn(token_stream); | ||
result.token_stream.to_string() | ||
} | ||
|
||
#[test] | ||
fn test_log_macro_debug() { | ||
let expanded = expand_macro( | ||
log_02lvnd9pht92k, | ||
r#"log!("DEBUG", "test message: {}", '123')"#, | ||
); | ||
assert_eq!( | ||
expanded, | ||
r#"if logging::LOG_LEVEL_DEBUG { println!("{}: {}", format!("DEBUG"), format!("test message: {}", '123')) }"# | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_log_macro_trace() { | ||
let expanded = expand_macro(log_02lvnd9pht92k, r#"log!("TRACE", "entering function")"#); | ||
assert_eq!( | ||
expanded, | ||
r#"if logging::LOG_LEVEL_TRACE { println!("{}: {}", format!("TRACE"), format!("entering function")) }"# | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_log_macro_invalid_level() { | ||
let expanded = expand_macro(log_02lvnd9pht92k, r#"log!("INFO", "test message")"#); | ||
assert_eq!(expanded, ""); // Empty TokenStream for error case | ||
} | ||
|
||
#[test] | ||
fn test_log_macro_empty_format() { | ||
let expanded = expand_macro(log_02lvnd9pht92k, r#"log!("DEBUG", '')"#); | ||
assert_eq!( | ||
expanded, | ||
r#"if logging::LOG_LEVEL_DEBUG { println!("{}: {}", format!("DEBUG"), format!('')) }"# | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_log_macro_multiple_arguments() { | ||
let expanded = expand_macro( | ||
log_02lvnd9pht92k, | ||
r#"log!("DEBUG", "Values: {}, {}, {}", '1', '2', '3')"#, | ||
); | ||
assert_eq!( | ||
expanded, | ||
r#"if logging::LOG_LEVEL_DEBUG { println!("{}: {}", format!("DEBUG"), format!("Values: {}, {}, {}", '1', '2', '3')) }"# | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_log_macro_exact_string() { | ||
let expanded = expand_macro( | ||
log_02lvnd9pht92k, | ||
r#"log!("DEBUG", "Exact {} string", 'test')"#, | ||
); | ||
assert_eq!( | ||
expanded, | ||
r#"if logging::LOG_LEVEL_DEBUG { println!("{}: {}", format!("DEBUG"), format!("Exact {} string", 'test')) }"# | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?