src/macros.rs

Lines

100.00 %

Functions

100.00 %

Regions

100.00 %

LineCountSource
1
//
2
// File Name:    macros.rs
3
// Directory:    src
4
// Project Name: flogging
5
//
6
// Copyright (C) 2025 Bradley Willcott
7
//
8
// SPDX-License-Identifier: GPL-3.0-or-later
9
//
10
// This library (crate) is free software: you can redistribute it and/or modify
11
// it under the terms of the GNU General Public License as published by
12
// the Free Software Foundation, either version 3 of the License, or
13
// (at your option) any later version.
14
//
15
// This library (crate) is distributed in the hope that it will be useful,
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
// GNU General Public License for more details.
19
//
20
// You should have received a copy of the GNU General Public License
21
// along with this library (crate).  If not, see <https://www.gnu.org/licenses/>.
22
//
23
24
//!
25
//! # Macros
26
//!
27
28
use crate::Logger;
29
30
///
31
/// Setup module level logger access.
32
///
33
/// The basic macro syntax is:
34
///
35
/// ```text
36
/// const_logger!({/* the block of Rust code to build a Logger goes here */});
37
/// ```
38
/// Notice there are curly braces "`{}`" wrapping the inner Rust code.
39
/// **They are required.**
40
///
41
/// The code you put in here will depend on what configuration of `Logger` you
42
/// want to setup.
43
///
44
/// # Examples
45
/// ```
46
/// extern crate flogging;
47
/// use flogging::*;
48
///
49
/// const_logger!({
50
///     Logger::builder(module_path!())
51
///         .set_level(Level::FINEST)
52
///         .add_console_handler()
53
///         .add_file_handler_with("rdb.log", FormatType::Iso8601, None)
54
///         .build()
55
/// });
56
/// ```
57
#[macro_export]
58
macro_rules! const_logger {
59
    ($block:block) => {
60
        use flogging::Logger as FLogger;
61
        use std::cell::{LazyCell as FLazyCell, RefCell as FRefCell};
62
63
        // Setup module level logger access.
6411
        const LOGGER: FLazyCell<FRefCell<FLogger>> = FLazyCell::new(|| FRefCell::new({ $block }));
65
    };
66
}