Skip to content

Commit

Permalink
v0.4.8
Browse files Browse the repository at this point in the history
  • Loading branch information
MXWXZ committed Nov 12, 2024
1 parent ad40f84 commit c834f3a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.4.8
## Changes
1. `handler` function can be async.

# 0.4.7
## Changes
1. Logger can configure `handler` through the function.
Expand Down
2 changes: 1 addition & 1 deletion actix-cloud/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "actix-cloud"
version = "0.4.7"
version = "0.4.8"
edition = "2021"
authors = ["MXWXZ <matrixwxz@gmail.com>"]
description = "Actix Cloud is an all-in-one web framework based on Actix Web."
Expand Down
14 changes: 8 additions & 6 deletions actix-cloud/src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{
fmt::Write as _,
future::Future,
io::{self, stderr, stdout, Write},
pin::Pin,
str::FromStr,
thread::{self, JoinHandle},
};
Expand Down Expand Up @@ -132,7 +134,7 @@ impl Logger {
pub type WriterFn = Box<dyn Fn(LogItem, Box<dyn Write>) -> Result<()> + Send>;
pub type FilterFn = Box<dyn Fn(&LogItem) -> bool + Send>;
pub type TransformerFn = Box<dyn Fn(LogItem) -> LogItem + Send>;
pub type HandlerFn = Box<dyn Fn(&Map<String, Value>) -> bool + Send>;
pub type HandlerFn = Box<dyn Fn(&Map<String, Value>) -> Pin<Box<dyn Future<Output = bool>>> + Send>;

pub struct LoggerGuard {
stop_tx: UnboundedSender<()>,
Expand Down Expand Up @@ -263,7 +265,7 @@ impl LoggerBuilder {

pub fn handler<F>(mut self, handler: F) -> Self
where
F: Fn(&Map<String, Value>) -> bool + Send + 'static,
F: Fn(&Map<String, Value>) -> Pin<Box<dyn Future<Output = bool>>> + Send + 'static,
{
self.handler = Some(Box::new(handler));
self
Expand Down Expand Up @@ -300,9 +302,9 @@ impl LoggerBuilder {
.init();

let join = thread::spawn(move || {
let handler = |v: Map<String, Value>| {
let handler = |v: Map<String, Value>| async {
if let Some(x) = &self.handler {
if !x(&v) {
if !x(&v).await {
return;
}
}
Expand Down Expand Up @@ -345,11 +347,11 @@ impl LoggerBuilder {
loop {
select! {
Some(v) = rx.recv() => {
handler(v);
handler(v).await;
},
_ = stop_rx.recv() => {
while let Ok(v) = rx.try_recv(){
handler(v);
handler(v).await;
}
break;
}
Expand Down

0 comments on commit c834f3a

Please sign in to comment.