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

feat: add logging macro #196

Closed

Conversation

TropicalDog17
Copy link

@TropicalDog17 TropicalDog17 commented Sep 16, 2024

Guide to be added in README.md of the logging package

Prequiste for using log! macro for a package:

  • import this as a dependency in Scarb.toml
[dependencies]
`raito_macros = { path = "../macros" }`
  • add the features in Scarb.toml
[features]
default = [log_level_none]
log_level_none = []
log_level_trace = []
log_level_debug = []
  • add the logging.cairo(must be this name) file to the package root, like the package/consensus
# packages/consensus/src/logging.cairo
#[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;
  • in lib.cairo
pub mod logging;

Copy link

vercel bot commented Sep 16, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
raito ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 25, 2024 2:36pm

packages/macros/src/logging.rs Outdated Show resolved Hide resolved
packages/macros/src/logging.rs Outdated Show resolved Hide resolved
.collect();

// Generate the log statement with feature checks
let log_statement = match level.as_str() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it really necessary to have separate versions for each log level?

packages/macros/src/logging.rs Outdated Show resolved Hide resolved
@TropicalDog17 TropicalDog17 marked this pull request as ready for review September 19, 2024 07:19
@TropicalDog17 TropicalDog17 changed the title wip: add logging macro feat: add logging macro Sep 19, 2024
@@ -11,3 +11,4 @@ license-file = "LICENSE"

[workspace.dependencies]
cairo_test = "2.8.0"

Copy link
Collaborator

Choose a reason for hiding this comment

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

?

@@ -44,6 +43,18 @@ pub fn validate_timestamp(prev_timestamps: Span<u32>, block_time: u32) -> Result
}
}

#[cfg(feature: 'log_level_debug')]
Copy link
Collaborator

Choose a reason for hiding this comment

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

What are we testing here? No conditions are checked.

// Generate the log statement
let log_statement = generate_log_statement(&level, &format_string, &log_args);

println!("Log statement: {}", log_statement);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not necessary?

if matches!(node.kind(&db), SyntaxKind::Arg) {
Some(node.get_text(&db))
} else {
println!("Invalid argument: {:?} {:?}", node.get_text(&db), node.kind(&db));
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should return an error?

@@ -0,0 +1,14 @@
# Cargo.toml
Copy link
Collaborator

Choose a reason for hiding this comment

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

Package should be called logging not macros.

@@ -0,0 +1,17 @@
#[cfg(feature: 'log_level_trace')]
Copy link
Collaborator

Choose a reason for hiding this comment

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

This file should go to macros(or rather logging) package.

Copy link
Author

@TropicalDog17 TropicalDog17 Sep 22, 2024

Choose a reason for hiding this comment

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

I can't find a way to import the logging constant to the desired package to use the constants. seems that the procedural macros package(logging package) behaves differently.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe try to make package with macros a dependency of loggin package(which would be cairo only)?

Copy link
Author

@TropicalDog17 TropicalDog17 Sep 23, 2024

Choose a reason for hiding this comment

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

very weird even the compiler is happy after the feature is enabled, maybe this is cairo bug @maciejka

error: Identifier not found.
 --> //raito/packages/consensus/src/codec.cairo:14:30
        if logging::logging::LOG_LEVEL_DEBUG { println!("{}: {}", format!("DEBUG"), format!("tx_encoded: {}", dest)) }
                             ^*************^

error: could not compile `consensus` due to previous error

image

Copy link
Collaborator

Choose a reason for hiding this comment

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

It should be logging::log::LOG_LEVEL_DEBUG not logging::logging::LOG_LEVEL_DEBUG:
image

Copy link
Author

Choose a reason for hiding this comment

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

@maciejka i previously rename the file then logging::logging::LOG_LEVEL_DEBUG. also logging::log::LOG_LEVEL_DEBUG also not work even though the language server recognize when the feature is enabled.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You are right:
image

Copy link
Collaborator

Choose a reason for hiding this comment

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

I asked in scarb group: https://t.me/c/1788692421/1887

// Generate the log statement
let log_statement = generate_log_statement(&level, &format_string, &log_args);

println!("Log statement: {}", log_statement);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove the trace

[dependencies]
bigdecimal = "0.4.5"
cairo-lang-macro = "0.1"
cairo-lang-parser = "2.7.0"
Copy link
Collaborator

Choose a reason for hiding this comment

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

don't we want 2.8.0?

@@ -0,0 +1,17 @@
#[cfg(feature: 'log_level_trace')]
Copy link
Collaborator

Choose a reason for hiding this comment

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

It should be logging::log::LOG_LEVEL_DEBUG not logging::logging::LOG_LEVEL_DEBUG:
image

@maciejka
Copy link
Collaborator

How is it going @TropicalDog17? It seems like this one is close to be finished.

@TropicalDog17
Copy link
Author

How is it going @TropicalDog17? It seems like this one is close to be finished.

Yeah @maciejka I think these feature-based constants cannot be imported from an external package, I tried some workaround but it's no luck, maybe the language hasn't supported that feature yet. It appears that for the logging feature to work correctly, the constant file needs to be placed within the specific package where we want to enable logging.

@maciejka
Copy link
Collaborator

Please check: #224. I had to try it myself. Indeed there is a problem with conditional compilation in a multipackage project. Waiting for scarb developers response.

@maciejka
Copy link
Collaborator

Please check: #224. I had to try it myself. Indeed there is a problem with conditional compilation in a multipackage project. Waiting for scarb developers response.

image

@maciejka
Copy link
Collaborator

Please check: #224. I had to try it myself. Indeed there is a problem with conditional compilation in a multipackage project. Waiting for scarb developers response.

image

Which means we might need to wait until they implement it.

@TropicalDog17
Copy link
Author

Please check: #224. I had to try it myself. Indeed there is a problem with conditional compilation in a multipackage project. Waiting for scarb developers response.

image

Which means we might need to wait until they implement it.

yeah I see, and very weird that the language server still recognize that feature.

@maciejka
Copy link
Collaborator

yeah I see, and very weird that the language server still recognize that feature.

true

@maciejka
Copy link
Collaborator

I am closing it for now. We will need to revisit this issue once feature support in Scarb is more mature. The work with my small fixes is parked in branch: feat/logging-macro

@maciejka maciejka closed this Sep 26, 2024
@maciejka
Copy link
Collaborator

@TropicalDog17 please contact me on tg: @aundumla

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feat] create a log! macro
2 participants