Git Product home page Git Product logo

log-macro's Introduction

Simple Demo of an Attribute Macro

What the #[log] macro does

If a function is annotated with the log-macro the function's name gets logged on entry and exit:

  • Log output before the actual function body executes
> my_function_name
  • Log output just before the function returns
< my_function_name

This is inspired by how logging can be realized with concepts from Aspect Oriented Programming (AOP). Although this example is extremely simplified, it shows how additional logging can be added, merely through applying an attribute. Very similar to how annotation-based logging works, like for example in Java.

How the macro works

The "annotated"[1] function is parsed into an ItemFn, using the syn crate. It reads out the function signature (especially name and return type) and the body. Than the code is reconstructed with quote. While an entry- and an exit-log-statement gets "injected" in the user-code.

Little interesting aside. It is important that the macro checks, whether the function returns anything else but the Unit () type. Because if that is the case, the very last statment of the function has to be quoted before the exit-log-statment.

How the repo is structured

The parent crate is use-log-macro. And it, like the name implies, uses the #[log] macro on some example functions, in it's main.rs.

The log-macro is an attribute-macro and has to live in it's own crate. Here it is nested as a library inside of use-log-macro, which references log by relative path declaration in it's Cargo.toml.

What else could this style be use for

I think this pattern shows how it might be possible to inject all sorts of runtime measurement logic, simply by adding attributes to functions. For example benchmark calculations. Or reporting logic could be added and reduced in this very simple way. Maybe even by writing to a database. All without cluttering the primary logic of the functions.

Notes

[1] I know it is not called 'annotation' in Rust. But what would you say then: "The attributeapplied function...?"

log-macro's People

Contributors

niilz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.