bp3d_debug/util.rs
1// Copyright (c) 2024, BlockProject 3D
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification,
6// are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of BlockProject 3D nor the names of its contributors
14// may be used to endorse or promote products derived from this software
15// without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29/// Extracts the target name and the module path (without the target name) from a full module path string.
30///
31/// # Arguments
32///
33/// * `base_string`: a full module path string (ex: bp3d_logger::util::extract_target_module).
34///
35/// returns: (&str, &str)
36pub fn extract_target_module(base_string: &str) -> (&str, &str) {
37 let target = base_string
38 .find("::")
39 .map(|v| &base_string[..v])
40 .unwrap_or(base_string);
41 let module = base_string.find("::").map(|v| &base_string[(v + 2)..]);
42 (target, module.unwrap_or("main"))
43}
44
45/// The context of a log message.
46#[derive(Clone, Copy, Debug)]
47pub struct Location {
48 module_path: &'static str,
49 file: &'static str,
50 line: u32,
51}
52
53impl Location {
54 /// Creates a new instance of a log message location.
55 ///
56 /// This function is const to let the caller store location structures in statics.
57 ///
58 /// # Arguments
59 ///
60 /// * `module_path`: the module path obtained from the [module_path](module_path) macro.
61 /// * `file`: the source file obtained from the [file](file) macro.
62 /// * `line`: the line number in the source file obtained from the [line](line) macro.
63 ///
64 /// returns: Metadata
65 pub const fn new(module_path: &'static str, file: &'static str, line: u32) -> Self {
66 Self {
67 module_path,
68 file,
69 line,
70 }
71 }
72
73 /// The module path which issued this log message.
74 pub fn module_path(&self) -> &'static str {
75 self.module_path
76 }
77
78 /// The source file which issued this log message.
79 pub fn file(&self) -> &'static str {
80 self.file
81 }
82
83 /// The line in the source file which issued this log message.
84 pub fn line(&self) -> u32 {
85 self.line
86 }
87
88 /// Extracts the target name and the module name from the module path.
89 pub fn get_target_module(&self) -> (&'static str, &'static str) {
90 extract_target_module(self.module_path)
91 }
92}
93
94/// Generate a [Location](crate::Location) structure.
95#[macro_export]
96macro_rules! location {
97 () => {
98 $crate::util::Location::new(module_path!(), file!(), line!())
99 };
100}