Skip to content

Commit

Permalink
implement log macro
Browse files Browse the repository at this point in the history
  • Loading branch information
kfatyuip committed Aug 4, 2024
1 parent 292d114 commit cadc9ab
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 39 deletions.
2 changes: 0 additions & 2 deletions docs/HOTRELOAD.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ $ kill -SIGHUP $(pidof zest)
```

or using the script in scripts/ directory,

and after a socket request (chose it for better performance), it will be reloaded
Empty file modified scripts/hotreload.sh
100644 → 100755
Empty file.
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[macro_use]
pub mod macros;

pub mod config;
pub mod init;
pub mod route;
Expand Down
68 changes: 68 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#[cfg(feature = "log")]
#[macro_export]
macro_rules! info {
($fmt:expr) => {
logger().log(
&log::Record::builder()
.level(log::Level::Info)
.target("access")
.args(format_args!($fmt))
.build(),
);
};
($fmt:expr, $($args:tt)*) => {
logger().log(
&log::Record::builder()
.level(log::Level::Info)
.target("access")
.args(format_args!($fmt, $($args)*))
.build(),
);
};
}

#[cfg(feature = "log")]
#[macro_export]
macro_rules! error {
($fmt:expr) => {
logger().log(
&log::Record::builder()
.level(log::Level::Error)
.target("error")
.args(format_args!($fmt))
.build(),
);
};
($fmt:expr, $($args:tt)*) => {
logger().log(
&log::Record::builder()
.level(log::Level::Error)
.target("error")
.args(format_args!($fmt, $($args)*))
.build(),
);
};
}

#[cfg(feature = "log")]
#[macro_export]
macro_rules! warn {
($fmt:expr) => {
logger().log(
&log::Record::builder()
.level(log::Level::Warn)
.target("access")
.args(format_args!($fmt))
.build(),
);
};
($fmt:expr, $($args:tt)*) => {
logger().log(
&log::Record::builder()
.level(log::Level::Warn)
.target("access")
.args(format_args!($fmt, $($args)*))
.build(),
);
};
}
42 changes: 5 additions & 37 deletions src/zest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,8 @@ where
'handle: loop {
if T.try_read().unwrap().is_none() {
#[cfg(feature = "log")]
logger().log(
&log::Record::builder()
.level(log::Level::Info)
.target("access")
.args(format_args!("config reloaded!"))
.build(),
);
info!("config reloaded!");

return Ok(());
}

Expand Down Expand Up @@ -340,40 +335,13 @@ where
{
match _status_code {
200 => {
logger().log(
&log::Record::builder()
.level(log::Level::Info)
.target("access")
.args(format_args!(
"\"{}\" {} - {}",
_req, _status_code, _addr
))
.build(),
);
info!("\"{}\" {} - {}", _req, _status_code, _addr);
}
400.. => {
logger().log(
&log::Record::builder()
.level(log::Level::Error)
.target("error")
.args(format_args!(
"\"{}\" {} - {}",
_req, _status_code, _addr
))
.build(),
);
error!("\"{}\" {} - {}", _req, _status_code, _addr);
}
_ => {
logger().log(
&log::Record::builder()
.level(log::Level::Warn)
.target("access")
.args(format_args!(
"\"{}\" {} - {}",
_req, _status_code, _addr
))
.build(),
);
warn!("\"{}\" {} - {}", _req, _status_code, _addr);
}
};
}
Expand Down

0 comments on commit cadc9ab

Please sign in to comment.