flogging_macros/src/logger.rs
Lines
100.00 %
Functions
100.00 %
Regions
100.00 %
| Line | Count | Source |
|---|---|---|
| 1 | // | |
| 2 | // File Name: logger.rs | |
| 3 | // Project Name: flogging | |
| 4 | // | |
| 5 | // Copyright (C) 2025 Bradley Willcott | |
| 6 | // | |
| 7 | // SPDX-License-Identifier: GPL-3.0-or-later | |
| 8 | // | |
| 9 | // This library (crate) is free software: you can redistribute it and/or modify | |
| 10 | // it under the terms of the GNU General Public License as published by | |
| 11 | // the Free Software Foundation, either version 3 of the License, or | |
| 12 | // (at your option) any later version. | |
| 13 | // | |
| 14 | // This library (crate) is distributed in the hope that it will be useful, | |
| 15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | // GNU General Public License for more details. | |
| 18 | // | |
| 19 | // You should have received a copy of the GNU General Public License | |
| 20 | // along with this library (crate). If not, see <https://www.gnu.org/licenses/>. | |
| 21 | // | |
| 22 | ||
| 23 | //! | |
| 24 | //! # Logger Macro Impl | |
| 25 | //! | |
| 26 | ||
| 27 | use ::TokenStream; | |
| 28 | use ::quote; | |
| 29 | use ::{,}; | |
| 30 | ||
| 31 | 11 | pub(crate) fn logger_impl(attr:item:) -> { |
| 32 | // println!("attr: (is_empty: {}) {attr}", attr.to_string().is_empty()); | |
| 33 | ||
| 34 | // Parse the input as `ItemFn` which is a type provided | |
| 35 | // by `syn` to represent a function. | |
| 36 | 11 | let= parse_macro_input!(as); |
| 37 | ||
| 38 | let{ | |
| 39 | // The function signature | |
| 40 | 11 | , |
| 41 | // The visibility specifier of this function | |
| 42 | 11 | , |
| 43 | // The function block or body | |
| 44 | 11 | , |
| 45 | // Other attributes applied to this function | |
| 46 | 11 | , |
| 47 | 11 | } =; |
| 48 | ||
| 49 | // Extract statements in the body of the functions | |
| 50 | 11 | let=; |
| 51 | ||
| 52 | // Store the function identifier for logging | |
| 53 | 11 | let= ifto_string()is_empty() { |
| 54 | 10 | clone()to_string() |
| 55 | } else { | |
| 56 | 1 | to_string() |
| 57 | }; | |
| 58 | ||
| 59 | // Reconstruct the function as output using parsed input | |
| 60 | 11 | quote!( |
| 61 | // Reapply all the other attributes on this function. | |
| 62 | // The compiler doesn't include the macro we are | |
| 63 | // currently working in this list. | |
| 64 | #(#)* | |
| 65 | // Reconstruct the function declaration | |
| 66 | ##{ | |
| 67 | // At the beginning of the function, borrow a reference to | |
| 68 | // module level static logger. | |
| 69 | let= LOGGER; | |
| 70 | let mut=borrow_mut(); | |
| 71 | set_fn_name(#); | |
| 72 | ||
| 73 | #(#)* | |
| 74 | } | |
| 75 | ) | |
| 76 | 11 | into() |
| 77 | 11 | } |