scribe/src/location.rs

70 lines
1.5 KiB
Rust

/// The Location where a log message was generated.
pub struct Location<'a>
{
/// The module path where the message was genereated.
/// <crate_name>[`::<module>`]*
module_path: &'a str,
/// The name of the file where the message was generated.
file_name: &'a str,
/// The line of the file where the message was generated.
line: u32
}
impl<'a> Location<'a>
{
/// Create a new Location.
///
/// This can easily be done using 'module_path!', 'file!',
/// 'line!'.
pub fn new(module: &'a str, file: &'a str, line_num: u32)
-> Location<'a>
{
Location
{
module_path: module,
file_name: file,
line: line_num
}
}
/// Get the module path where the message was generated.
pub fn get_module_path(&self) -> &str
{
self.module_path
}
/// Get the name of the file where the message was generated.
pub fn get_file_name(&self) -> &str
{
self.file_name
}
/// Get the line number of the file where the
/// message was generated.
pub fn get_line_number(&self) -> u32
{
self.line
}
}
impl<'a> ::std::fmt::Debug for Location<'a>
{
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result
{
write!(f, "{}", self)
}
}
impl<'a> ::std::fmt::Display for Location<'a>
{
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result
{
write!(f, "{}::{}::{}", self.get_module_path(),
self.get_file_name(), self.get_line_number())
}
}