Skip to content

Commit

Permalink
chore: tidy some code randomly (#26)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun authored Aug 2, 2024
1 parent ddfbfa1 commit 7046423
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 36 deletions.
5 changes: 2 additions & 3 deletions src/filter/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ use crate::filter::Filter;
use crate::filter::FilterResult;

pub struct CustomFilter {
#[allow(clippy::type_complexity)]
f: Box<dyn Fn(&log::Metadata) -> FilterResult + Send + Sync + 'static>,
f: Box<dyn Fn(&Metadata) -> FilterResult + Send + Sync + 'static>,
}

impl Debug for CustomFilter {
Expand All @@ -31,7 +30,7 @@ impl Debug for CustomFilter {
}

impl CustomFilter {
pub fn new(filter: impl Fn(&log::Metadata) -> FilterResult + Send + Sync + 'static) -> Self {
pub fn new(filter: impl Fn(&Metadata) -> FilterResult + Send + Sync + 'static) -> Self {
CustomFilter {
f: Box::new(filter),
}
Expand Down
23 changes: 13 additions & 10 deletions src/layout/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,30 @@ use std::fmt::Arguments;
use std::fmt::Debug;
use std::fmt::Formatter;

use log::Record;

use crate::layout::Layout;

// TODO(tisonkun): use trait alias when it's stable - https://github.com/rust-lang/rust/issues/41517
// then we can use the alias for both `dyn` and `impl`.
type FormatFunction = dyn Fn(&Record, &dyn Fn(Arguments) -> anyhow::Result<()>) -> anyhow::Result<()>
+ Send
+ Sync
+ 'static;

pub struct CustomLayout {
#[allow(clippy::type_complexity)]
f: Box<
dyn Fn(&log::Record, &dyn Fn(Arguments) -> anyhow::Result<()>) -> anyhow::Result<()>
+ Send
+ Sync
+ 'static,
>,
f: Box<FormatFunction>,
}

impl Debug for CustomLayout {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "CustomLayout {{ ... }}")
}
}

impl CustomLayout {
pub fn new(
layout: impl Fn(&log::Record, &dyn Fn(Arguments) -> anyhow::Result<()>) -> anyhow::Result<()>
layout: impl Fn(&Record, &dyn Fn(Arguments) -> anyhow::Result<()>) -> anyhow::Result<()>
+ Send
+ Sync
+ 'static,
Expand All @@ -46,7 +49,7 @@ impl CustomLayout {
}
}

pub(crate) fn format<F>(&self, record: &log::Record, f: &F) -> anyhow::Result<()>
pub(crate) fn format<F>(&self, record: &Record, f: &F) -> anyhow::Result<()>
where
F: Fn(Arguments) -> anyhow::Result<()>,
{
Expand Down
42 changes: 26 additions & 16 deletions src/layout/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
// limitations under the License.

use std::fmt::Arguments;
use std::path::Path;
use std::time::SystemTime;

use humantime::Rfc3339Timestamp;
use log::Record;
use serde::Deserialize;
use serde::Serialize;
use serde_json::Map;
use serde_json::Value;
Expand All @@ -44,17 +43,33 @@ impl<'a, 'kvs> log::kv::Visitor<'kvs> for KvCollector<'a> {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize)]
struct RecordLine<'a> {
timestamp: String,
level: String,
#[serde(serialize_with = "serialize_timestamp")]
timestamp: Rfc3339Timestamp,
level: &'a str,
module_path: &'a str,
file: &'a str,
line: u32,
message: String,
#[serde(serialize_with = "serialize_args")]
message: &'a Arguments<'a>,
kvs: Map<String, Value>,
}

fn serialize_args<S>(args: &Arguments, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.collect_str(args)
}

fn serialize_timestamp<S>(timestamp: &Rfc3339Timestamp, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.collect_str(&format_args!("{timestamp}"))
}

impl JsonLayout {
pub(crate) fn format<F>(&self, record: &Record, f: &F) -> anyhow::Result<()>
where
Expand All @@ -64,18 +79,13 @@ impl JsonLayout {
let mut visitor = KvCollector { kvs: &mut kvs };
record.key_values().visit(&mut visitor)?;

let timestamp = humantime::format_rfc3339_micros(SystemTime::now());
let record_line = RecordLine {
timestamp: format!("{timestamp}"),
level: record.level().to_string(),
module_path: record.module_path().unwrap_or(""),
file: record
.file()
.and_then(|file| Path::new(file).file_name())
.and_then(|name| name.to_str())
.unwrap_or_default(),
timestamp: humantime::format_rfc3339_micros(SystemTime::now()),
level: record.level().as_str(),
module_path: record.module_path().unwrap_or_default(),
file: record.file().unwrap_or_default(),
line: record.line().unwrap_or(0),
message: record.args().to_string(),
message: record.args(),
kvs,
};

Expand Down
9 changes: 2 additions & 7 deletions src/layout/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

use std::fmt::Arguments;
use std::path::Path;
use std::time::SystemTime;

use colored::Color;
Expand Down Expand Up @@ -65,12 +64,8 @@ impl TextLayout {

let time = humantime::format_rfc3339_micros(SystemTime::now());
let level = ColoredString::from(record.level().to_string()).color(color);
let module = record.module_path().unwrap_or("");
let file = record
.file()
.and_then(|file| Path::new(file).file_name())
.and_then(|name| name.to_str())
.unwrap_or_default();
let module = record.module_path().unwrap_or_default();
let file = record.file().unwrap_or_default();
let line = record.line().unwrap_or(0);
let message = record.args();
let kvs = KvDisplay::new(record.key_values());
Expand Down

0 comments on commit 7046423

Please sign in to comment.