Browse Source

initial meow

main
xenua 1 month ago
commit
5082b205d9
Signed by: xenua
GPG Key ID: 8F93B68BD37255B8
  1. 1
      .gitignore
  2. 430
      Cargo.lock
  3. 10
      Cargo.toml
  4. 307
      src/lib.rs

1
.gitignore vendored

@ -0,0 +1 @@ @@ -0,0 +1 @@
/target

430
Cargo.lock generated

@ -0,0 +1,430 @@ @@ -0,0 +1,430 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "autocfg"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80"
[[package]]
name = "bumpalo"
version = "3.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
[[package]]
name = "cc"
version = "1.0.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2678b2e3449475e95b0aa6f9b506a28e61b3dc8996592b983695e8ebb58a8b41"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a0d04d43504c61aa6c7531f1871dd0d418d91130162063b789da00fd7057a5e"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-targets",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
[[package]]
name = "gay_tracing_formatter"
version = "0.1.0"
dependencies = [
"chrono",
"owo-colors",
"tracing",
"tracing-subscriber",
]
[[package]]
name = "iana-time-zone"
version = "0.1.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "js-sys"
version = "0.3.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "log"
version = "0.4.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
[[package]]
name = "nu-ansi-term"
version = "0.46.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
dependencies = [
"overload",
"winapi",
]
[[package]]
name = "num-traits"
version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "overload"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
[[package]]
name = "owo-colors"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "caff54706df99d2a78a5a4e3455ff45448d81ef1bb63c22cd14052ca0e993a3f"
[[package]]
name = "pin-project-lite"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"
[[package]]
name = "proc-macro2"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "sharded-slab"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
dependencies = [
"lazy_static",
]
[[package]]
name = "smallvec"
version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]]
name = "syn"
version = "2.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thread_local"
version = "1.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c"
dependencies = [
"cfg-if",
"once_cell",
]
[[package]]
name = "tracing"
version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef"
dependencies = [
"pin-project-lite",
"tracing-attributes",
"tracing-core",
]
[[package]]
name = "tracing-attributes"
version = "0.1.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tracing-core"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
dependencies = [
"once_cell",
"valuable",
]
[[package]]
name = "tracing-log"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3"
dependencies = [
"log",
"once_cell",
"tracing-core",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b"
dependencies = [
"nu-ansi-term",
"sharded-slab",
"smallvec",
"thread_local",
"tracing-core",
"tracing-log",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "wasm-bindgen"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-core"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675"
[[package]]
name = "windows_i686_gnu"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3"
[[package]]
name = "windows_i686_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8"

10
Cargo.toml

@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
[package]
name = "gay_tracing_formatter"
version = "0.1.0"
edition = "2021"
[dependencies]
chrono = "0.4.37"
owo-colors = "4.0.0"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"

307
src/lib.rs

@ -0,0 +1,307 @@ @@ -0,0 +1,307 @@
use std::{
cell::RefCell,
env,
fmt::{self, Display},
io::{stdout, IsTerminal, Write},
marker::PhantomData,
};
use chrono::{DateTime, Local, TimeZone};
use owo_colors::{AnsiColors, OwoColorize};
use tracing::{
field::{Field, Visit},
Event, Level, Metadata, Subscriber,
};
use tracing_subscriber::{
fmt::{format::Writer, MakeWriter},
Layer,
};
pub struct GayFormattingLayer<
S,
W: for<'w> MakeWriter<'w> + 'static = fn() -> std::io::Stdout,
Tz: TimeZone = Local,
> {
timer: DateTime<Tz>,
with_ansi: bool,
colors: GayColors,
make_writer: W,
_inner: PhantomData<fn(S)>,
}
struct GayVisitor<'w> {
writer: Writer<'w>,
indent: usize,
injected_message: Option<String>,
}
pub struct GayColors {
pub crate_name: AnsiColors,
pub trace: AnsiColors,
pub debug: AnsiColors,
pub info: AnsiColors,
pub warn: AnsiColors,
pub error: AnsiColors,
pub timer: AnsiColors,
}
impl<S> Default for GayFormattingLayer<S> {
fn default() -> Self {
Self::new()
}
}
impl<S, W> Layer<S> for GayFormattingLayer<S, W>
where
S: Subscriber,
W: for<'w> MakeWriter<'w> + 'static,
{
fn on_event(&self, event: &Event<'_>, _ctx: tracing_subscriber::layer::Context<'_, S>) {
thread_local! {
static BUF: RefCell<String> = RefCell::new(String::new());
}
BUF.with(|buf: &RefCell<String>| {
let borrow = buf.try_borrow_mut();
let mut a;
let mut b;
let buf = match borrow {
Ok(buf) => {
a = buf;
&mut *a
}
_ => {
b = String::new();
&mut b
}
};
let res = self.format_event(event, tracing_subscriber::fmt::format::Writer::new(buf));
if res.is_ok() {
let mut writer = self.make_writer.make_writer();
if let Err(fuck) = writer.write_all(buf.as_bytes()) {
eprintln!("[gay tracing formatter crate] unable to write event: {fuck}\n")
}
} else {
eprintln!(
"[gay tracing formatter crate] unable to format event; name: {}; fields: {:?}",
event.metadata().name(),
event.fields()
);
}
});
}
}
impl<'w> Visit for GayVisitor<'w> {
fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
match field.name() {
"message" => {
write!(self.writer, " {:?}", value).ok();
if let Some(msg) = self.injected_message.take() {
write!(self.writer, "\n{}", msg).ok();
}
}
_ => {
if let Some(msg) = self.injected_message.take() {
write!(self.writer, "\n{}", msg).ok();
}
write!(
self.writer,
"\n{:>indent$}: {:?}",
field.name(),
value,
indent = self.indent - 1
)
.ok();
}
}
}
}
impl Default for GayColors {
fn default() -> Self {
Self {
crate_name: AnsiColors::Green,
trace: AnsiColors::Magenta,
debug: AnsiColors::Blue,
info: AnsiColors::White,
warn: AnsiColors::BrightYellow,
error: AnsiColors::Red,
timer: AnsiColors::BrightBlack,
}
}
}
impl<S> GayFormattingLayer<S> {
pub fn new() -> Self {
Self {
timer: Local::now(),
with_ansi: stdout().is_terminal(),
colors: GayColors::default(),
make_writer: std::io::stdout,
_inner: PhantomData,
}
}
}
impl<S, W, Tz> GayFormattingLayer<S, W, Tz>
where
W: for<'w> MakeWriter<'w> + 'static,
Tz: TimeZone,
Tz::Offset: Display,
{
pub fn timer(self, timer: DateTime<Tz>) -> Self {
let Self {
with_ansi,
colors,
make_writer,
_inner,
..
} = self;
Self {
timer,
with_ansi,
colors,
make_writer,
_inner,
}
}
pub fn with_ansi(self, with_ansi: bool) -> Self {
let Self {
timer,
colors,
make_writer,
_inner,
..
} = self;
Self {
timer,
with_ansi,
colors,
make_writer,
_inner,
}
}
pub fn colors(self, colors: GayColors) -> Self {
let Self {
timer,
with_ansi,
colors: _,
make_writer,
_inner,
} = self;
Self {
timer,
with_ansi,
colors,
make_writer,
_inner,
}
}
pub fn make_writer(self, make_writer: W) -> Self {
let Self {
timer,
with_ansi,
colors,
_inner,
..
} = self;
Self {
timer,
with_ansi,
colors,
make_writer,
_inner,
}
}
const TIME_LEN: usize = "YYYY-MM-DD HH-MM-SS".len();
fn make_from_crate_string(&self, meta: &Metadata<'static>) -> Option<String> {
static CURRENT_CRATE_NAME: &'static str = env!("CARGO_PKG_NAME");
match meta.module_path() {
Some(path) => path
.split_once("::")
.map(|(crate_name, _)| crate_name)
.filter(|name| *name != CURRENT_CRATE_NAME)
.map(|name| {
name.to_owned()
.colorize_if_true(self.with_ansi, self.colors.crate_name)
})
.map(|name| format!("{:>indent$} {name}", "from crate", indent = Self::TIME_LEN)),
None => Some(format!(
"{:>indent$}",
"from unknown crate",
indent = Self::TIME_LEN,
)),
}
}
fn format_event(
&self,
event: &Event,
mut writer: tracing_subscriber::fmt::format::Writer,
) -> std::fmt::Result {
let meta = event.metadata();
write!(writer, "{} {}", self.datetime(), self.level(meta.level()))?;
let mut v = GayVisitor::new(
writer.by_ref(),
Self::TIME_LEN,
self.make_from_crate_string(meta),
);
event.record(&mut v);
write!(writer, "\n")?;
Ok(())
}
fn datetime(&self) -> String {
self.timer
.format("%F %T")
.to_string()
.colorize_if_true(self.with_ansi, self.colors.timer)
}
fn level(&self, level: &Level) -> String {
let (icon, color) = match *level {
Level::TRACE => ("TRACE", self.colors.trace),
Level::DEBUG => ("DEBUG", self.colors.debug),
Level::INFO => ("INFO ", self.colors.info),
Level::WARN => ("WARN ", self.colors.warn),
Level::ERROR => ("ERROR", self.colors.error),
};
icon.to_string().colorize_if_true(self.with_ansi, color)
}
}
impl<'w> GayVisitor<'w> {
pub fn new(writer: Writer<'w>, indent: usize, message: Option<String>) -> Self {
GayVisitor {
writer,
indent,
injected_message: message,
}
}
}
trait ColorizeIfTrue {
fn colorize_if_true(self, cond: bool, color: AnsiColors) -> Self;
}
impl ColorizeIfTrue for String {
fn colorize_if_true(self, cond: bool, color: AnsiColors) -> Self {
if cond {
self.color(color).to_string()
} else {
self
}
}
}
Loading…
Cancel
Save