From f52370f19281c0424d2690e2686f2a5ef85559b6 Mon Sep 17 00:00:00 2001 From: Allstreamer <48365544+Allstreamer@users.noreply.github.com> Date: Sun, 3 Apr 2022 18:45:47 +0200 Subject: [PATCH 1/2] Added Formatting - Added Formating Functionality to logging macros --- README.md | 2 +- src/lib.rs | 24 ++++++++++++------------ src/main.rs | 15 +++++++++------ 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 456f2a6..823867a 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ - [x] Logging Levels - [x] Coloured Output - [ ] Options for Datetime, Current Function, Line Number, Custom Colours, etc. -- [ ] Custom Formatting +- [x] Custom Formatting - [ ] File support And much more to come... soon™! diff --git a/src/lib.rs b/src/lib.rs index ed8777d..d1dd487 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -158,7 +158,7 @@ impl Display for Level { /// ``` #[macro_export] macro_rules! trace { - ($str:expr) => {{ + ($($arg:tt)*) => {{ use std::io::Write; use termcolor::WriteColor; @@ -171,7 +171,7 @@ macro_rules! trace { .set_bold(true), ) .unwrap(); - writeln!(&mut stream, "[{} {}] {}", now, rall::Level::TRACE, $str).unwrap(); + writeln!(&mut stream, "[{} {}] {}", now, rall::Level::TRACE, format!($($arg)*)).unwrap(); stream.reset().unwrap(); }}; } @@ -193,7 +193,7 @@ macro_rules! trace { /// ``` #[macro_export] macro_rules! debug { - ($str:expr) => {{ + ($($arg:tt)*) => {{ use std::io::Write; use termcolor::WriteColor; @@ -206,7 +206,7 @@ macro_rules! debug { .set_bold(true), ) .unwrap(); - writeln!(&mut stream, "[{} {}] {}", now, rall::Level::DEBUG, $str).unwrap(); + writeln!(&mut stream, "[{} {}] {}", now, rall::Level::DEBUG, format!($($arg)*)).unwrap(); stream.reset().unwrap(); }}; } @@ -228,7 +228,7 @@ macro_rules! debug { /// ``` #[macro_export] macro_rules! info { - ($str:expr) => { + ($($arg:tt)*) => { use std::io::Write; use termcolor::WriteColor; @@ -241,7 +241,7 @@ macro_rules! info { .set_bold(true), ) .unwrap(); - writeln!(&mut stream, "[{} {}] {}", now, rall::Level::INFO, $str).unwrap(); + writeln!(&mut stream, "[{} {}] {}", now, rall::Level::INFO, format!($($arg)*)).unwrap(); stream.reset().unwrap(); }; } @@ -264,7 +264,7 @@ macro_rules! info { /// ``` #[macro_export] macro_rules! warn { - ($str:expr) => {{ + ($($arg:tt)*) => {{ use std::io::Write; use termcolor::WriteColor; @@ -277,7 +277,7 @@ macro_rules! warn { .set_bold(true), ) .unwrap(); - writeln!(&mut stream, "[{} {}] {}", now, rall::Level::WARN, $str).unwrap(); + writeln!(&mut stream, "[{} {}] {}", now, rall::Level::WARN, format!($($arg)*)).unwrap(); stream.reset().unwrap(); }}; } @@ -300,7 +300,7 @@ macro_rules! warn { /// ``` #[macro_export] macro_rules! error { - ($str:expr) => {{ + ($($arg:tt)*) => {{ use std::io::Write; use termcolor::WriteColor; @@ -313,7 +313,7 @@ macro_rules! error { .set_intense(true), ) .unwrap(); - writeln!(&mut stream, "[{} {}] {}", now, rall::Level::ERROR, $str).unwrap(); + writeln!(&mut stream, "[{} {}] {}", now, rall::Level::ERROR, format!($($arg)*)).unwrap(); stream.reset().unwrap(); }}; } @@ -336,7 +336,7 @@ macro_rules! error { /// ``` #[macro_export] macro_rules! fatal { - ($str:expr) => {{ + ($($arg:tt)*) => {{ use std::io::Write; use termcolor::WriteColor; @@ -349,7 +349,7 @@ macro_rules! fatal { .set_bold(true), ) .unwrap(); - writeln!(&mut stream, "[{} {}] {}", now, rall::Level::FATAL, $str).unwrap(); + writeln!(&mut stream, "[{} {}] {}", now, rall::Level::FATAL, format!($($arg)*)).unwrap(); stream.reset().unwrap(); }}; } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 04ed941..561058a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,13 @@ use rall::{debug, error, fatal, info, trace, warn}; fn main() { - trace!("My Best Friend Hazel :D"); - debug!("My Best Friend Hazel :D"); - info!("My Best Friend Hazel :D"); - warn!("My Best Friend Hazel :D"); - error!("My Best Friend Hazel :D"); - fatal!("My Best Friend Hazel :D"); + trace!("My Best Friend Hazel {}", ":D"); + debug!("My Best Friend Hazel {}", ":D"); + info!("My Best Friend Hazel {}{}", ":", ")"); + warn!("My Best Friend Hazel {}", ":)"); + error!("My Best Friend Hazel {}", ":P"); + fatal!("My Best Friend Hazel {}", ":D"); + + let very_important_value = String::from(";)"); + debug!("Look at this: {}", very_important_value); } \ No newline at end of file From 1fc6396045ae9830c3d304b93d36b26868a016bb Mon Sep 17 00:00:00 2001 From: Allstreamer <48365544+Allstreamer@users.noreply.github.com> Date: Sun, 3 Apr 2022 19:44:32 +0200 Subject: [PATCH 2/2] Added Requested Changes - Checked Tick box in lib.rs - Used format_args to avoid heap allocations - Reverted main.rs & Added Formating Examples to main.rs --- src/lib.rs | 14 +++++++------- src/main.rs | 9 +++------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d1dd487..ff72f8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,7 @@ //! - [x] Logging Levels //! - [x] Datetime & Coloured Output //! - [ ] Options for Datetime, Current Function, Line Number, Custom Colours, etc. -//! - [ ] Custom Formatting +//! - [x] Custom Formatting //! - [ ] File support //! //! And much more to come... soon™! @@ -171,7 +171,7 @@ macro_rules! trace { .set_bold(true), ) .unwrap(); - writeln!(&mut stream, "[{} {}] {}", now, rall::Level::TRACE, format!($($arg)*)).unwrap(); + writeln!(&mut stream, "[{} {}] {}", now, rall::Level::TRACE, format_args!($($arg)*)).unwrap(); stream.reset().unwrap(); }}; } @@ -206,7 +206,7 @@ macro_rules! debug { .set_bold(true), ) .unwrap(); - writeln!(&mut stream, "[{} {}] {}", now, rall::Level::DEBUG, format!($($arg)*)).unwrap(); + writeln!(&mut stream, "[{} {}] {}", now, rall::Level::DEBUG, format_args!($($arg)*)).unwrap(); stream.reset().unwrap(); }}; } @@ -241,7 +241,7 @@ macro_rules! info { .set_bold(true), ) .unwrap(); - writeln!(&mut stream, "[{} {}] {}", now, rall::Level::INFO, format!($($arg)*)).unwrap(); + writeln!(&mut stream, "[{} {}] {}", now, rall::Level::INFO, format_args!($($arg)*)).unwrap(); stream.reset().unwrap(); }; } @@ -277,7 +277,7 @@ macro_rules! warn { .set_bold(true), ) .unwrap(); - writeln!(&mut stream, "[{} {}] {}", now, rall::Level::WARN, format!($($arg)*)).unwrap(); + writeln!(&mut stream, "[{} {}] {}", now, rall::Level::WARN, format_args!($($arg)*)).unwrap(); stream.reset().unwrap(); }}; } @@ -313,7 +313,7 @@ macro_rules! error { .set_intense(true), ) .unwrap(); - writeln!(&mut stream, "[{} {}] {}", now, rall::Level::ERROR, format!($($arg)*)).unwrap(); + writeln!(&mut stream, "[{} {}] {}", now, rall::Level::ERROR, format_args!($($arg)*)).unwrap(); stream.reset().unwrap(); }}; } @@ -349,7 +349,7 @@ macro_rules! fatal { .set_bold(true), ) .unwrap(); - writeln!(&mut stream, "[{} {}] {}", now, rall::Level::FATAL, format!($($arg)*)).unwrap(); + writeln!(&mut stream, "[{} {}] {}", now, rall::Level::FATAL, format_args!($($arg)*)).unwrap(); stream.reset().unwrap(); }}; } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 561058a..facbf70 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,11 +3,8 @@ use rall::{debug, error, fatal, info, trace, warn}; fn main() { trace!("My Best Friend Hazel {}", ":D"); debug!("My Best Friend Hazel {}", ":D"); - info!("My Best Friend Hazel {}{}", ":", ")"); - warn!("My Best Friend Hazel {}", ":)"); - error!("My Best Friend Hazel {}", ":P"); + info!("My Best Friend Hazel {}", ":D"); + warn!("My Best Friend Hazel {}", ":D"); + error!("My Best Friend Hazel {}", ":D"); fatal!("My Best Friend Hazel {}", ":D"); - - let very_important_value = String::from(";)"); - debug!("Look at this: {}", very_important_value); } \ No newline at end of file