Skip to content

Commit 6a6fa0e

Browse files
committed
Added formatting arguments
1 parent 50ac89f commit 6a6fa0e

File tree

5 files changed

+108
-50
lines changed

5 files changed

+108
-50
lines changed

examples/main.rs

+7
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,11 @@ fn main() {
99
logging_rs::error!(logger, "Error!");
1010
logging_rs::fatal!(logger, "Fatal error!");
1111
logging_rs::log!(logger, "Log message");
12+
13+
logging_rs::debug!(logger, "Debug message with {{more_info}}", "more_info" = "additional information");
14+
logging_rs::info!(logger, "Info and {{details}}", "details" = "more stuff");
15+
logging_rs::warn!(logger, "Warning: {{name}} is bad", "name" = "War");
16+
logging_rs::error!(logger, "Error! {{stuff}} went wrong", "stuff" = "Everything");
17+
logging_rs::fatal!(logger, "Fatal error! Code {{code}}", "code" = "404");
18+
logging_rs::log!(logger, "Log message and {{more}}", "more" = "more");
1219
}

src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![doc = include_str!("../.github/errors.md")]
22
// logging-rs errors
3-
// Version: 1.0.0
3+
// Version: 1.1.0
44

55
// Copyright (c) 2023-present ElBe Development.
66

src/lib.rs

+97-47
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![doc = include_str!("../.github/README.md")]
2-
// Logging-rs.
3-
// Version: 1.0.0
2+
// Logging-rs
3+
// Version: 1.1.0
44

55
// Copyright (c) 2023-present I Language Development.
66

@@ -62,25 +62,6 @@ pub enum Level {
6262
}
6363

6464

65-
//////////////////
66-
// HTTP METHODS //
67-
//////////////////
68-
69-
// HTTP methods
70-
/*#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
71-
pub enum HTTPMethod {
72-
/// GET request. The default value
73-
#[default]
74-
GET,
75-
/// POST request
76-
POST,
77-
/// PUT request
78-
PUT,
79-
/// PATCH request
80-
PATCH
81-
}*/
82-
83-
8465
/////////////////
8566
// OUTPUT TYPE //
8667
/////////////////
@@ -97,12 +78,7 @@ pub enum Output {
9778
FILE {
9879
/// File path
9980
path: String
100-
},
101-
// Web request (not currently implemented)
102-
/*REQUEST {
103-
method: HTTPMethod,
104-
url: String
105-
}*/
81+
}
10682
}
10783

10884

@@ -217,7 +193,8 @@ impl Formatter {
217193
/// - [`Formatter`]
218194
/// - [`Output`]
219195
/// - [`Level`]
220-
pub fn format<'a>(&self, output: Output, level: Level, message: &'a str, mut arguments: Vec<(&str, String)>) -> String {
196+
pub fn format<'a>(&self, output: Output, level: Level, message: &'a str, mut extra_arguments: Vec<(&str, String)>) -> String {
197+
let mut arguments: Vec<(&str, String)> = vec![];
221198
let mut colors: Vec<(&str, String)> = vec![
222199
// Formatting codes
223200
("end", "\x1b[0m".to_string()),
@@ -286,6 +263,7 @@ impl Formatter {
286263

287264
arguments.push(("message", message.to_string()));
288265
arguments.push(("timestamp", chrono::Local::now().format(&self.timestamp_format).to_string()));
266+
arguments.append(&mut extra_arguments);
289267

290268
let mut result: String = match output {
291269
Output::STDOUT | Output::STDERR => {
@@ -384,6 +362,7 @@ impl Logger {
384362
/// - `message`: The message to log
385363
/// - `level`: The log [`Level`] to use for logging
386364
/// - `path`: The path of the calling file
365+
/// - `arguments`: A list of arguments to use when formatting the message
387366
///
388367
/// # Returns
389368
///
@@ -397,7 +376,8 @@ impl Logger {
397376
/// logger.log(
398377
/// "Some message",
399378
/// logging_rs::Level::default(),
400-
/// "src/lib.rs"
379+
/// "src/lib.rs",
380+
/// vec![]
401381
/// );
402382
/// ```
403383
///
@@ -411,9 +391,10 @@ impl Logger {
411391
/// - [`log!()`]
412392
/// - [`Logger`]
413393
/// - [`Level`]
414-
pub fn log(&self, message: &str, level: Level, path: &str) {
394+
pub fn log(&self, message: &str, level: Level, path: &str, mut arguments: Vec<(&str, String)>) {
395+
arguments.push(("path", path.to_string()));
415396
for writable in self.writable_list.clone() {
416-
let formatted: String = self.formatter.format(writable.clone(), level, message, vec![("path", path.to_string())]);
397+
let formatted: String = self.formatter.format(writable.clone(), level, message, arguments.clone());
417398

418399
match writable {
419400
Output::STDOUT => println!("{}", formatted),
@@ -429,16 +410,7 @@ impl Logger {
429410
if let Err(error) = write {
430411
errors::Error::new("Writing error", "The file could not be edited", 2).raise(format!("File: {}\nText: {}\nError: {}", path, formatted, error).as_str());
431412
}
432-
},
433-
// TODO (ElBe): Add HTTP support
434-
/*Output::REQUEST { ref method, ref url } => {
435-
match method {
436-
HTTPMethod::GET => {},
437-
HTTPMethod::POST => {},
438-
HTTPMethod::PUT => {},
439-
HTTPMethod::PATCH => {},
440-
}
441-
}*/
413+
}
442414
}
443415
}
444416
}
@@ -462,6 +434,7 @@ impl Logger {
462434
/// # use logging_rs;
463435
/// # let logger: logging_rs::Logger = logging_rs::Logger::default();
464436
/// logging_rs::debug!(logger, "A message");
437+
/// logging_rs::debug!(logger, "A message with more {{details}}", "details" = "stuff");
465438
/// ```
466439
///
467440
/// # See also
@@ -476,7 +449,19 @@ impl Logger {
476449
macro_rules! debug {
477450
($logger:expr, $message:expr) => {
478451
{
479-
$logger.log($message, $crate::Level::DEBUG, std::panic::Location::caller().file());
452+
$logger.log($message, $crate::Level::DEBUG, std::panic::Location::caller().file(), vec![]);
453+
}
454+
};
455+
456+
($logger:expr, $message:expr, $($argument_name:literal = $argument_value:literal),* $(,)?) => {
457+
{
458+
let mut arguments: Vec<(&str, String)> = vec![];
459+
460+
$(
461+
arguments.push(($argument_name, $argument_value.to_string()));
462+
)*
463+
464+
$logger.log($message, $crate::Level::DEBUG, std::panic::Location::caller().file(), arguments);
480465
}
481466
};
482467
}
@@ -494,6 +479,7 @@ macro_rules! debug {
494479
/// # use logging_rs;
495480
/// # let logger: logging_rs::Logger = logging_rs::Logger::default();
496481
/// logging_rs::info!(logger, "A message");
482+
/// logging_rs::info!(logger, "A message with more {{details}}", "details" = "stuff");
497483
/// ```
498484
///
499485
/// # See also
@@ -508,9 +494,21 @@ macro_rules! debug {
508494
macro_rules! info {
509495
($logger:expr, $message:expr) => {
510496
{
511-
$logger.log($message, $crate::Level::INFO, std::panic::Location::caller().file());
497+
$logger.log($message, $crate::Level::INFO, std::panic::Location::caller().file(), vec![]);
512498
}
513499
};
500+
501+
($logger:expr, $message:expr, $($argument_name:literal = $argument_value:literal),* $(,)?) => {
502+
{
503+
let mut arguments: Vec<(&str, String)> = vec![];
504+
505+
$(
506+
arguments.push(($argument_name, $argument_value.to_string()));
507+
)*
508+
509+
$logger.log($message, $crate::Level::INFO, std::panic::Location::caller().file(), arguments);
510+
}
511+
}
514512
}
515513

516514
/// Logs the given message with logging level [`Level::WARN`].
@@ -526,6 +524,7 @@ macro_rules! info {
526524
/// # use logging_rs;
527525
/// # let logger: logging_rs::Logger = logging_rs::Logger::default();
528526
/// logging_rs::warn!(logger, "A message");
527+
/// logging_rs::warn!(logger, "A message with more {{details}}", "details" = "stuff");
529528
/// ```
530529
///
531530
/// # See also
@@ -540,9 +539,21 @@ macro_rules! info {
540539
macro_rules! warn {
541540
($logger:expr, $message:expr) => {
542541
{
543-
$logger.log($message, $crate::Level::WARN, std::panic::Location::caller().file());
542+
$logger.log($message, $crate::Level::WARN, std::panic::Location::caller().file(), vec![]);
544543
}
545544
};
545+
546+
($logger:expr, $message:expr, $($argument_name:literal = $argument_value:literal),* $(,)?) => {
547+
{
548+
let mut arguments: Vec<(&str, String)> = vec![];
549+
550+
$(
551+
arguments.push(($argument_name, $argument_value.to_string()));
552+
)*
553+
554+
$logger.log($message, $crate::Level::WARN, std::panic::Location::caller().file(), arguments);
555+
}
556+
}
546557
}
547558

548559
/// Logs the given message with logging level [`Level::ERROR`].
@@ -558,6 +569,7 @@ macro_rules! warn {
558569
/// # use logging_rs;
559570
/// # let logger: logging_rs::Logger = logging_rs::Logger::default();
560571
/// logging_rs::error!(logger, "A message");
572+
/// logging_rs::error!(logger, "A message with more {{details}}", "details" = "stuff");
561573
/// ```
562574
///
563575
/// # See also
@@ -572,9 +584,21 @@ macro_rules! warn {
572584
macro_rules! error {
573585
($logger:expr, $message:expr) => {
574586
{
575-
$logger.log($message, $crate::Level::ERROR, std::panic::Location::caller().file());
587+
$logger.log($message, $crate::Level::ERROR, std::panic::Location::caller().file(), vec![]);
576588
}
577589
};
590+
591+
($logger:expr, $message:expr, $($argument_name:literal = $argument_value:literal),* $(,)?) => {
592+
{
593+
let mut arguments: Vec<(&str, String)> = vec![];
594+
595+
$(
596+
arguments.push(($argument_name, $argument_value.to_string()));
597+
)*
598+
599+
$logger.log($message, $crate::Level::ERROR, std::panic::Location::caller().file(), arguments);
600+
}
601+
}
578602
}
579603

580604
/// Logs the given message with logging level [`Level::FATAL`].
@@ -590,6 +614,7 @@ macro_rules! error {
590614
/// # use logging_rs;
591615
/// # let logger: logging_rs::Logger = logging_rs::Logger::default();
592616
/// logging_rs::fatal!(logger, "A message");
617+
/// logging_rs::fatal!(logger, "A message with more {{details}}", "details" = "stuff");
593618
/// ```
594619
///
595620
/// # See also
@@ -604,9 +629,21 @@ macro_rules! error {
604629
macro_rules! fatal {
605630
($logger:expr, $message:expr) => {
606631
{
607-
$logger.log($message, $crate::Level::FATAL, std::panic::Location::caller().file());
632+
$logger.log($message, $crate::Level::FATAL, std::panic::Location::caller().file(), vec![]);
608633
}
609634
};
635+
636+
($logger:expr, $message:expr, $($argument_name:literal = $argument_value:literal),* $(,)?) => {
637+
{
638+
let mut arguments: Vec<(&str, String)> = vec![];
639+
640+
$(
641+
arguments.push(($argument_name, $argument_value.to_string()));
642+
)*
643+
644+
$logger.log($message, $crate::Level::FATAL, std::panic::Location::caller().file(), arguments);
645+
}
646+
}
610647
}
611648

612649
/// Logs the given message with logging level [`Level::MESSAGE`].
@@ -622,6 +659,7 @@ macro_rules! fatal {
622659
/// # use logging_rs;
623660
/// # let logger: logging_rs::Logger = logging_rs::Logger::default();
624661
/// logging_rs::log!(logger, "A message");
662+
/// logging_rs::log!(logger, "A message with more {{details}}", "details" = "stuff");
625663
/// ```
626664
///
627665
/// # See also
@@ -636,7 +674,19 @@ macro_rules! fatal {
636674
macro_rules! log {
637675
($logger:expr, $message:expr) => {
638676
{
639-
$logger.log($message, $crate::Level::MESSAGE, std::panic::Location::caller().file());
677+
$logger.log($message, $crate::Level::MESSAGE, std::panic::Location::caller().file(), vec![]);
640678
}
641679
};
680+
681+
($logger:expr, $message:expr, $($argument_name:literal = $argument_value:literal),* $(,)?) => {
682+
{
683+
let mut arguments: Vec<(&str, String)> = vec![];
684+
685+
$(
686+
arguments.push(($argument_name, $argument_value.to_string()));
687+
)*
688+
689+
$logger.log($message, $crate::Level::MESSAGE, std::panic::Location::caller().file(), arguments);
690+
}
691+
}
642692
}

tests/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// logging-rs error tests
2-
// Version: 1.0.0
2+
// Version: 1.1.0
33

44
// Copyright (c) 2023-present ElBe Development.
55

tests/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// logging-rs tests
2-
// Version: 1.0.0
2+
// Version: 1.1.0
33

44
// Copyright (c) 2023-present ElBe Development.
55

@@ -27,6 +27,7 @@
2727

2828
#[allow(unused_imports)]
2929
use chrono;
30+
#[allow(unused_imports)]
3031
use logging_rs;
3132

3233

0 commit comments

Comments
 (0)