{"id":16037181,"url":"https://github.com/elichai/log-derive","last_synced_at":"2025-04-04T15:11:04.459Z","repository":{"id":46265880,"uuid":"168507953","full_name":"elichai/log-derive","owner":"elichai","description":"A procedural macro for auto logging output of functions","archived":false,"fork":false,"pushed_at":"2021-11-03T13:53:59.000Z","size":92,"stargazers_count":188,"open_issues_count":7,"forks_count":12,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-09T22:11:06.384Z","etag":null,"topics":["logging","metaprogramming","proc-macro","rust"],"latest_commit_sha":null,"homepage":"https://docs.rs/log-derive","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/elichai.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-31T10:37:44.000Z","updated_at":"2024-09-06T20:48:29.000Z","dependencies_parsed_at":"2022-09-09T03:01:09.321Z","dependency_job_id":null,"html_url":"https://github.com/elichai/log-derive","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elichai%2Flog-derive","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elichai%2Flog-derive/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elichai%2Flog-derive/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elichai%2Flog-derive/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/elichai","download_url":"https://codeload.github.com/elichai/log-derive/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247198463,"owners_count":20900080,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["logging","metaprogramming","proc-macro","rust"],"created_at":"2024-10-08T22:11:10.880Z","updated_at":"2025-04-04T15:11:04.424Z","avatar_url":"https://github.com/elichai.png","language":"Rust","readme":"# log-derive\n[![Build Status](https://travis-ci.org/elichai/log-derive.svg?branch=master)](https://travis-ci.org/elichai/log-derive)\n[![Latest version](https://img.shields.io/crates/v/log-derive.svg)](https://crates.io/crates/log-derive)\n[![Documentation](https://docs.rs/log-derive/badge.svg)](https://docs.rs/log-derive)\n![License](https://img.shields.io/crates/l/log-derive.svg)\n[![dependency status](https://deps.rs/repo/github/elichai/log-derive/status.svg)](https://deps.rs/repo/github/elichai/log-derive)\n\nA Rust macro to part of the [log](https://crates.io/crates/log) facade that auto generates loggings for functions output. \n\n* [Documentation](https://docs.rs/log-derive)\n\n## Usage\n\nAdd this to your `Cargo.toml`:\n\n```toml\n[dependencies]\nlog-derive = \"0.3\"\nlog = \"0.4\"\n\n```\n\nand for Rust Edition 2015 add this to your crate root:\n\n```rust\n#[macro_use]\nextern crate log_derive;\nextern crate log;\n```\nIn Rust Edition 2018 you can simply do:\n```rust\nuse log_derive::logfn;\n```\n\nAfter that all you need is to add the according macro above a function that,  \u003cbr\u003e\neither returns an output or receive an input that implements the `Debug` trait.\n\n# Examples\n\n```rust\n #[logfn(Err = \"Error\", fmt = \"Failed Sending Packet: {:?}\")]\n fn send_hi(addr: SocketAddr) -\u003e Result\u003c(), io::Error\u003e {\n     let mut stream = TcpStream::connect(addr)?;\n     stream.write(b\"Hi!\")?;\n     Ok( () )\n }\n\n```\n\n```rust\n#[logfn(Trace)]\n#[logfn_inputs(Info)]\nfn test_log(a: u8) -\u003e String {\n  (a*2).to_string()\n}\n\n```\n\n```rust\n#[logfn(Trace, fmt = \"testing the num: {:?}\")]\nfn test_log(a: u8) -\u003e String {\n  (a*2).to_string()\n}\n\n```\n\n# Output\nThe output of the [fibonacci](./examples/fibonacci.rs) example:\n```\n17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 5)\n17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 4)\n17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 3)\n17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 2)\n17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 1)\n17:15:24 [ INFO] fibonacci() -\u003e 1\n17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 0)\n17:15:24 [ INFO] fibonacci() -\u003e 1\n17:15:24 [ INFO] fibonacci() -\u003e 2\n17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 1)\n17:15:24 [ INFO] fibonacci() -\u003e 1\n17:15:24 [ INFO] fibonacci() -\u003e 3\n17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 2)\n17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 1)\n17:15:24 [ INFO] fibonacci() -\u003e 1\n17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 0)\n17:15:24 [ INFO] fibonacci() -\u003e 1\n17:15:24 [ INFO] fibonacci() -\u003e 2\n17:15:24 [ INFO] fibonacci() -\u003e 5\n17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 3)\n17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 2)\n17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 1)\n17:15:24 [ INFO] fibonacci() -\u003e 1\n17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 0)\n17:15:24 [ INFO] fibonacci() -\u003e 1\n17:15:24 [ INFO] fibonacci() -\u003e 2\n17:15:24 [TRACE] (1) fibonacci: [examples/fibonacci.rs:16] fibonacci(n: 1)\n17:15:24 [ INFO] fibonacci() -\u003e 1\n17:15:24 [ INFO] fibonacci() -\u003e 3\n17:15:24 [ INFO] fibonacci() -\u003e 8\n```\n\nIf you expand the output of the `#[logfn]` macro the resulting code will look something like this:\n```rust\nfn fibonacci(n: u32) -\u003e u32 {\n    let result = (move || match n {\n        0 =\u003e 1,\n        1 =\u003e 1,\n        _ =\u003e fibonacci(n - 1) + fibonacci(n - 2),\n    })();\n    log::log!(log::Level::Info, \"fibonacci() -\u003e {}\", result);\n    result\n}\n```\nIf the function returns a `Result` it will match through it to split between the `Ok` LogLevel and the `Err` LogLevel\n\nThe expansion of the `#[logfn_inputs]` macro will look something like this:\n```rust\nfn fibonacci(n: u32) -\u003e u32 {\n    log::log!(log::Level::Info, \"fibonacci(n: {:?})\", n);\n    match n {\n        0 =\u003e 1,\n        1 =\u003e 1,\n        _ =\u003e fibonacci(n - 1) + fibonacci(n - 2),\n    }\n}\n```\n\nOf course the `log!` macro will be expanded too and it will be a bit more messy.\n\n## Note\nThe `log_ts` feature will fail your compilation in a `no-std` enviroment. \nit can only be used where `std` is available. (as it uses `std::time::Instant`)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felichai%2Flog-derive","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felichai%2Flog-derive","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felichai%2Flog-derive/lists"}