forked from nonsensical-dev/zenyx-engine
feat: event based non-blocking logger
This commit is contained in:
parent
37abc3f52d
commit
f215c10d0e
8 changed files with 534 additions and 671 deletions
415
Cargo.lock
generated
415
Cargo.lock
generated
|
@ -85,15 +85,6 @@ version = "1.0.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b"
|
||||
|
||||
[[package]]
|
||||
name = "ash"
|
||||
version = "0.38.0+1.3.281"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0bb44936d800fea8f016d7f2311c6a4f97aebd5dc86f09906139ec848cf3a46f"
|
||||
dependencies = [
|
||||
"libloading",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atomic-waker"
|
||||
version = "1.1.2"
|
||||
|
@ -124,7 +115,7 @@ version = "0.5.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2c132eebf10f5cad5289222520a4a058514204aed6d791f1cf4fe8088b82d15f"
|
||||
dependencies = [
|
||||
"objc2 0.5.2",
|
||||
"objc2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -138,20 +129,6 @@ name = "bytemuck"
|
|||
version = "1.22.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6b1fc10dbac614ebc03540c9dbd60e83887fda27794998c6528f1782047d540"
|
||||
dependencies = [
|
||||
"bytemuck_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bytemuck_derive"
|
||||
version = "1.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7ecc273b49b3205b83d648f0690daa588925572cc5063745bfe547fe7ec8e1a1"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
|
@ -273,74 +250,24 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam"
|
||||
version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1137cd7e7fc0fb5d3c5a8678be38ec56e819125d8d7907411fe24ccb943faca8"
|
||||
dependencies = [
|
||||
"crossbeam-channel",
|
||||
"crossbeam-deque",
|
||||
"crossbeam-epoch",
|
||||
"crossbeam-queue",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-channel"
|
||||
version = "0.5.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2"
|
||||
dependencies = [
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-deque"
|
||||
version = "0.8.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"
|
||||
dependencies = [
|
||||
"crossbeam-epoch",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-epoch"
|
||||
version = "0.9.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
|
||||
dependencies = [
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-queue"
|
||||
version = "0.3.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0f58bbc28f91df819d0aa2a2c00cd19754769c2fad90579b3592b1c9ba7a3115"
|
||||
dependencies = [
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
|
||||
|
||||
[[package]]
|
||||
name = "crunchy"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929"
|
||||
|
||||
[[package]]
|
||||
name = "cursor-icon"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "96a6ac251f4a2aca6b3f91340350eab87ae57c3f127ffeb585e92bd336717991"
|
||||
|
||||
[[package]]
|
||||
name = "diff"
|
||||
version = "0.1.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
|
||||
|
||||
[[package]]
|
||||
name = "dispatch"
|
||||
version = "0.2.0"
|
||||
|
@ -384,12 +311,6 @@ dependencies = [
|
|||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "foldhash"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
|
||||
|
||||
[[package]]
|
||||
name = "foreign-types"
|
||||
version = "0.5.0"
|
||||
|
@ -450,41 +371,18 @@ dependencies = [
|
|||
"wasi 0.14.2+wasi-0.2.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "half"
|
||||
version = "2.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "459196ed295495a68f7d7fe1d84f6c4b7ff0e21fe3017b2f283c6fac3ad803c9"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"cfg-if",
|
||||
"crunchy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.15.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc"
|
||||
|
||||
[[package]]
|
||||
name = "humantime"
|
||||
version = "2.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b112acc8b3adf4b107a8ec20977da0273a8c386765a3ec0229bd500a1443f9f"
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "2.9.0"
|
||||
|
@ -495,12 +393,6 @@ dependencies = [
|
|||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
|
||||
|
||||
[[package]]
|
||||
name = "jni"
|
||||
version = "0.21.1"
|
||||
|
@ -582,16 +474,6 @@ version = "0.4.15"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.27"
|
||||
|
@ -622,12 +504,6 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "minimal-lexical"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
||||
|
||||
[[package]]
|
||||
name = "ndk"
|
||||
version = "0.9.0"
|
||||
|
@ -658,16 +534,6 @@ dependencies = [
|
|||
"jni-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "7.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
"minimal-lexical",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nu-ansi-term"
|
||||
version = "0.46.0"
|
||||
|
@ -715,15 +581,6 @@ dependencies = [
|
|||
"objc2-encode",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "objc2"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3531f65190d9cff863b77a99857e74c314dd16bf56c538c4b57c7cbc3f3a6e59"
|
||||
dependencies = [
|
||||
"objc2-encode",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "objc2-app-kit"
|
||||
version = "0.2.2"
|
||||
|
@ -733,11 +590,11 @@ dependencies = [
|
|||
"bitflags 2.9.0",
|
||||
"block2",
|
||||
"libc",
|
||||
"objc2 0.5.2",
|
||||
"objc2",
|
||||
"objc2-core-data",
|
||||
"objc2-core-image",
|
||||
"objc2-foundation 0.2.2",
|
||||
"objc2-quartz-core 0.2.2",
|
||||
"objc2-foundation",
|
||||
"objc2-quartz-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -748,9 +605,9 @@ checksum = "74dd3b56391c7a0596a295029734d3c1c5e7e510a4cb30245f8221ccea96b009"
|
|||
dependencies = [
|
||||
"bitflags 2.9.0",
|
||||
"block2",
|
||||
"objc2 0.5.2",
|
||||
"objc2",
|
||||
"objc2-core-location",
|
||||
"objc2-foundation 0.2.2",
|
||||
"objc2-foundation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -760,8 +617,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "a5ff520e9c33812fd374d8deecef01d4a840e7b41862d849513de77e44aa4889"
|
||||
dependencies = [
|
||||
"block2",
|
||||
"objc2 0.5.2",
|
||||
"objc2-foundation 0.2.2",
|
||||
"objc2",
|
||||
"objc2-foundation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -772,18 +629,8 @@ checksum = "617fbf49e071c178c0b24c080767db52958f716d9eabdf0890523aeae54773ef"
|
|||
dependencies = [
|
||||
"bitflags 2.9.0",
|
||||
"block2",
|
||||
"objc2 0.5.2",
|
||||
"objc2-foundation 0.2.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "objc2-core-foundation"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "daeaf60f25471d26948a1c2f840e3f7d86f4109e3af4e8e4b5cd70c39690d925"
|
||||
dependencies = [
|
||||
"bitflags 2.9.0",
|
||||
"objc2 0.6.0",
|
||||
"objc2",
|
||||
"objc2-foundation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -793,9 +640,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "55260963a527c99f1819c4f8e3b47fe04f9650694ef348ffd2227e8196d34c80"
|
||||
dependencies = [
|
||||
"block2",
|
||||
"objc2 0.5.2",
|
||||
"objc2-foundation 0.2.2",
|
||||
"objc2-metal 0.2.2",
|
||||
"objc2",
|
||||
"objc2-foundation",
|
||||
"objc2-metal",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -805,9 +652,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "000cfee34e683244f284252ee206a27953279d370e309649dc3ee317b37e5781"
|
||||
dependencies = [
|
||||
"block2",
|
||||
"objc2 0.5.2",
|
||||
"objc2",
|
||||
"objc2-contacts",
|
||||
"objc2-foundation 0.2.2",
|
||||
"objc2-foundation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -826,18 +673,7 @@ dependencies = [
|
|||
"block2",
|
||||
"dispatch",
|
||||
"libc",
|
||||
"objc2 0.5.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "objc2-foundation"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3a21c6c9014b82c39515db5b396f91645182611c97d24637cf56ac01e5f8d998"
|
||||
dependencies = [
|
||||
"bitflags 2.9.0",
|
||||
"objc2 0.6.0",
|
||||
"objc2-core-foundation",
|
||||
"objc2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -847,9 +683,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "a1a1ae721c5e35be65f01a03b6d2ac13a54cb4fa70d8a5da293d7b0020261398"
|
||||
dependencies = [
|
||||
"block2",
|
||||
"objc2 0.5.2",
|
||||
"objc2",
|
||||
"objc2-app-kit",
|
||||
"objc2-foundation 0.2.2",
|
||||
"objc2-foundation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -860,19 +696,8 @@ checksum = "dd0cba1276f6023976a406a14ffa85e1fdd19df6b0f737b063b95f6c8c7aadd6"
|
|||
dependencies = [
|
||||
"bitflags 2.9.0",
|
||||
"block2",
|
||||
"objc2 0.5.2",
|
||||
"objc2-foundation 0.2.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "objc2-metal"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "01c41bc8b0e50ea7a5304a56f25e0066f526e99641b46fd7b9ad4421dd35bff6"
|
||||
dependencies = [
|
||||
"bitflags 2.9.0",
|
||||
"objc2 0.6.0",
|
||||
"objc2-foundation 0.3.0",
|
||||
"objc2",
|
||||
"objc2-foundation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -883,22 +708,9 @@ checksum = "e42bee7bff906b14b167da2bac5efe6b6a07e6f7c0a21a7308d40c960242dc7a"
|
|||
dependencies = [
|
||||
"bitflags 2.9.0",
|
||||
"block2",
|
||||
"objc2 0.5.2",
|
||||
"objc2-foundation 0.2.2",
|
||||
"objc2-metal 0.2.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "objc2-quartz-core"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6fb3794501bb1bee12f08dcad8c61f2a5875791ad1c6f47faa71a0f033f20071"
|
||||
dependencies = [
|
||||
"bitflags 2.9.0",
|
||||
"objc2 0.6.0",
|
||||
"objc2-core-foundation",
|
||||
"objc2-foundation 0.3.0",
|
||||
"objc2-metal 0.3.0",
|
||||
"objc2",
|
||||
"objc2-foundation",
|
||||
"objc2-metal",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -907,8 +719,8 @@ version = "0.2.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0a684efe3dec1b305badae1a28f6555f6ddd3bb2c2267896782858d5a78404dc"
|
||||
dependencies = [
|
||||
"objc2 0.5.2",
|
||||
"objc2-foundation 0.2.2",
|
||||
"objc2",
|
||||
"objc2-foundation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -919,14 +731,14 @@ checksum = "b8bb46798b20cd6b91cbd113524c490f1686f4c4e8f49502431415f3512e2b6f"
|
|||
dependencies = [
|
||||
"bitflags 2.9.0",
|
||||
"block2",
|
||||
"objc2 0.5.2",
|
||||
"objc2",
|
||||
"objc2-cloud-kit",
|
||||
"objc2-core-data",
|
||||
"objc2-core-image",
|
||||
"objc2-core-location",
|
||||
"objc2-foundation 0.2.2",
|
||||
"objc2-foundation",
|
||||
"objc2-link-presentation",
|
||||
"objc2-quartz-core 0.2.2",
|
||||
"objc2-quartz-core",
|
||||
"objc2-symbols",
|
||||
"objc2-uniform-type-identifiers",
|
||||
"objc2-user-notifications",
|
||||
|
@ -939,8 +751,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "44fa5f9748dbfe1ca6c0b79ad20725a11eca7c2218bceb4b005cb1be26273bfe"
|
||||
dependencies = [
|
||||
"block2",
|
||||
"objc2 0.5.2",
|
||||
"objc2-foundation 0.2.2",
|
||||
"objc2",
|
||||
"objc2-foundation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -951,9 +763,9 @@ checksum = "76cfcbf642358e8689af64cee815d139339f3ed8ad05103ed5eaf73db8d84cb3"
|
|||
dependencies = [
|
||||
"bitflags 2.9.0",
|
||||
"block2",
|
||||
"objc2 0.5.2",
|
||||
"objc2",
|
||||
"objc2-core-location",
|
||||
"objc2-foundation 0.2.2",
|
||||
"objc2-foundation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -986,29 +798,6 @@ dependencies = [
|
|||
"ttf-parser",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
"parking_lot_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot_core"
|
||||
version = "0.9.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"redox_syscall 0.5.11",
|
||||
"smallvec",
|
||||
"windows-targets 0.52.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
version = "2.3.1"
|
||||
|
@ -1062,6 +851,16 @@ dependencies = [
|
|||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pretty_assertions"
|
||||
version = "1.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3ae130e2f271fbc2ac3a40fb1d07180839cdbbe443c7a27e1e3c13c5cac0116d"
|
||||
dependencies = [
|
||||
"diff",
|
||||
"yansi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-crate"
|
||||
version = "3.3.0"
|
||||
|
@ -1110,18 +909,6 @@ version = "0.6.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539"
|
||||
|
||||
[[package]]
|
||||
name = "raw-window-metal"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "40d213455a5f1dc59214213c7330e074ddf8114c9a42411eb890c767357ce135"
|
||||
dependencies = [
|
||||
"objc2 0.6.0",
|
||||
"objc2-core-foundation",
|
||||
"objc2-foundation 0.3.0",
|
||||
"objc2-quartz-core 0.3.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.4.1"
|
||||
|
@ -1203,12 +990,6 @@ version = "1.0.20"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2"
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
|
||||
|
||||
[[package]]
|
||||
name = "same-file"
|
||||
version = "1.0.6"
|
||||
|
@ -1224,12 +1005,6 @@ version = "1.0.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294"
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
||||
|
||||
[[package]]
|
||||
name = "sctk-adwaita"
|
||||
version = "0.10.1"
|
||||
|
@ -1263,18 +1038,6 @@ dependencies = [
|
|||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.140"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sharded-slab"
|
||||
version = "0.1.7"
|
||||
|
@ -1299,12 +1062,6 @@ dependencies = [
|
|||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "slabbin"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9db491c0d4152a069911a0fbdaca959691bf0b9d7110d98a7ed1c8e59b79ab30"
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.15.0"
|
||||
|
@ -1551,59 +1308,6 @@ version = "0.9.5"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
||||
|
||||
[[package]]
|
||||
name = "vk-parse"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3859da4d7b98bec73e68fb65815d47a263819c415c90eed42b80440a02cbce8c"
|
||||
dependencies = [
|
||||
"xml-rs",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vulkano"
|
||||
version = "0.35.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08840c2b51759a6f88f26f5ea378bc8b5c199a5b4760ddda292304be087249c4"
|
||||
dependencies = [
|
||||
"ash",
|
||||
"bytemuck",
|
||||
"crossbeam-queue",
|
||||
"foldhash",
|
||||
"half",
|
||||
"heck",
|
||||
"indexmap",
|
||||
"libloading",
|
||||
"nom",
|
||||
"once_cell",
|
||||
"parking_lot",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"raw-window-handle",
|
||||
"raw-window-metal",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"slabbin",
|
||||
"smallvec",
|
||||
"thread_local",
|
||||
"vk-parse",
|
||||
"vulkano-macros",
|
||||
"x11-dl",
|
||||
"x11rb",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vulkano-macros"
|
||||
version = "0.35.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1dc929c42c9336fd082079ac3ea30126e4a0dfe36fd2e2b3581303f7d140d20f"
|
||||
dependencies = [
|
||||
"proc-macro-crate",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.5.0"
|
||||
|
@ -2088,9 +1792,9 @@ dependencies = [
|
|||
"libc",
|
||||
"memmap2",
|
||||
"ndk",
|
||||
"objc2 0.5.2",
|
||||
"objc2",
|
||||
"objc2-app-kit",
|
||||
"objc2-foundation 0.2.2",
|
||||
"objc2-foundation",
|
||||
"objc2-ui-kit",
|
||||
"orbclient",
|
||||
"percent-encoding",
|
||||
|
@ -2193,23 +1897,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "b9cc00251562a284751c9973bace760d86c0276c471b4be569fe6b068ee97a56"
|
||||
|
||||
[[package]]
|
||||
name = "xml-rs"
|
||||
version = "0.8.26"
|
||||
name = "yansi"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a62ce76d9b56901b19a74f19431b0d8b3bc7ca4ad685a746dfd78ca8f4fc6bda"
|
||||
checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049"
|
||||
|
||||
[[package]]
|
||||
name = "zenyx"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"crossbeam",
|
||||
"humantime",
|
||||
"terminator",
|
||||
"thiserror 2.0.12",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
"vulkano",
|
||||
"winit",
|
||||
"zlog",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -2231,3 +1933,12 @@ dependencies = [
|
|||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zlog"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"pretty_assertions",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
|
12
Cargo.toml
12
Cargo.toml
|
@ -12,9 +12,10 @@ repository = "https://codeberg.org/Caznix/Zenyx"
|
|||
|
||||
[workspace]
|
||||
resolver = "2"
|
||||
# members = ["engine","subcrates/zen_core"]
|
||||
|
||||
members = ["subcrates/zlog"]
|
||||
|
||||
[workspace.dependencies]
|
||||
zlog = { path = "subcrates/zlog" }
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
@ -36,13 +37,10 @@ incremental = false
|
|||
panic = "abort"
|
||||
|
||||
[dependencies]
|
||||
crossbeam = "0.8.4"
|
||||
humantime = "2.2.0"
|
||||
|
||||
terminator = "0.3.2"
|
||||
thiserror = "2.0.12"
|
||||
tracing = "0.1.41"
|
||||
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
|
||||
vulkano = "0.35.1"
|
||||
winit = "0.30.9"
|
||||
|
||||
|
||||
zlog.workspace = true
|
||||
|
|
321
src/main.rs
321
src/main.rs
|
@ -1,317 +1,14 @@
|
|||
use crossbeam::channel::{Sender, unbounded};
|
||||
use std::{
|
||||
fmt,
|
||||
fs::OpenOptions,
|
||||
io::{BufWriter, Write},
|
||||
path::{Path, PathBuf},
|
||||
str::FromStr,
|
||||
sync::{Arc, RwLock},
|
||||
time::SystemTime,
|
||||
};
|
||||
use tracing::{Event, Level, Subscriber, info};
|
||||
use tracing_subscriber::{
|
||||
EnvFilter,
|
||||
layer::{Context, Layer, SubscriberExt},
|
||||
registry::LookupSpan,
|
||||
util::SubscriberInitExt,
|
||||
};
|
||||
use tracing::info;
|
||||
use zlog::config::LoggerConfig;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct App;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LogEntry {
|
||||
timestamp: SystemTime,
|
||||
level: Level,
|
||||
message: String,
|
||||
}
|
||||
|
||||
struct BufferLayer {
|
||||
log_entries: Arc<RwLock<Vec<LogEntry>>>,
|
||||
sender: Sender<LogEntry>,
|
||||
}
|
||||
|
||||
impl<S> Layer<S> for BufferLayer
|
||||
where
|
||||
S: Subscriber + for<'a> LookupSpan<'a>,
|
||||
{
|
||||
fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) {
|
||||
let metadata = event.metadata();
|
||||
let level = *metadata.level();
|
||||
let timestamp = SystemTime::now();
|
||||
let mut message = String::new();
|
||||
let mut visitor = LogVisitor::new(&mut message);
|
||||
event.record(&mut visitor);
|
||||
|
||||
let log_entry = LogEntry {
|
||||
timestamp,
|
||||
level,
|
||||
message,
|
||||
};
|
||||
|
||||
if let Ok(mut guard) = self.log_entries.write() {
|
||||
guard.push(log_entry.clone());
|
||||
}
|
||||
|
||||
let _ = self.sender.send(log_entry);
|
||||
}
|
||||
}
|
||||
|
||||
struct LogVisitor<'msg> {
|
||||
message: &'msg mut String,
|
||||
}
|
||||
|
||||
impl<'msg> LogVisitor<'msg> {
|
||||
fn new(message: &'msg mut String) -> Self {
|
||||
Self { message }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'msg> tracing::field::Visit for LogVisitor<'msg> {
|
||||
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn fmt::Debug) {
|
||||
use std::fmt::Write as _;
|
||||
if field.name() == "message" {
|
||||
write!(self.message, "{:?}", value).unwrap();
|
||||
} else {
|
||||
write!(self.message, "{}={:?} ", field.name(), value).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
|
||||
pub enum LogLevel {
|
||||
Error,
|
||||
#[default]
|
||||
Info,
|
||||
Debug,
|
||||
Trace,
|
||||
}
|
||||
|
||||
impl fmt::Display for LogLevel {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for LogLevel {
|
||||
type Err = ();
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s {
|
||||
"trace" => Ok(Self::Trace),
|
||||
"debug" => Ok(Self::Debug),
|
||||
"info" => Ok(Self::Info),
|
||||
"error" => Ok(Self::Error),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone,PartialEq,Eq,PartialOrd)]
|
||||
pub struct LoggerConfig {
|
||||
pub log_level: LogLevel,
|
||||
pub log_to_file: bool,
|
||||
pub log_file_path: PathBuf,
|
||||
pub log_to_stdout: bool,
|
||||
pub stdout_color: bool,
|
||||
pub stdout_include_time: bool,
|
||||
pub file_include_time: bool,
|
||||
}
|
||||
|
||||
impl LoggerConfig {
|
||||
pub fn level(mut self, level: LogLevel) -> Self {
|
||||
self.log_level = level;
|
||||
self
|
||||
}
|
||||
pub fn log_to_file(mut self, log_to_file: bool) -> Self {
|
||||
self.log_to_file = log_to_file;
|
||||
self
|
||||
}
|
||||
pub fn colored_stdout(mut self, show_color: bool) -> Self {
|
||||
self.stdout_color = show_color;
|
||||
self
|
||||
}
|
||||
pub fn log_to_stdout(mut self, stdout: bool) -> Self {
|
||||
self.log_to_stdout = stdout;
|
||||
self
|
||||
}
|
||||
pub fn log_path<P: AsRef<Path>>(mut self, path: P) -> Self {
|
||||
self.log_file_path = path.as_ref().to_path_buf();
|
||||
self
|
||||
}
|
||||
pub fn stdout_include_time(mut self, include: bool) -> Self {
|
||||
self.stdout_include_time = include;
|
||||
self
|
||||
}
|
||||
pub fn file_include_time(mut self, include: bool) -> Self {
|
||||
self.file_include_time = include;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for LoggerConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
log_level: LogLevel::Debug,
|
||||
log_to_file: true,
|
||||
log_file_path: "zenyx.log".into(),
|
||||
log_to_stdout: true,
|
||||
stdout_color: true,
|
||||
stdout_include_time: false,
|
||||
file_include_time: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Clone,Copy, PartialEq,Eq,PartialOrd)]
|
||||
pub enum LogQuery {
|
||||
All,
|
||||
From(SystemTime),
|
||||
}
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Logger {
|
||||
config: LoggerConfig,
|
||||
log_entries: Arc<RwLock<Vec<LogEntry>>>,
|
||||
_sender: Sender<LogEntry>,
|
||||
}
|
||||
|
||||
impl Logger {
|
||||
pub fn new(config: LoggerConfig) -> Self {
|
||||
let (sender, receiver) = unbounded();
|
||||
let log_entries = Arc::new(RwLock::new(Vec::new()));
|
||||
|
||||
if config.log_to_stdout {
|
||||
let stdout_receiver = receiver.clone();
|
||||
let stdout_color = config.stdout_color;
|
||||
let stdout_include_time = config.stdout_include_time;
|
||||
std::thread::spawn(move || {
|
||||
for entry in stdout_receiver {
|
||||
let line = format_entry(&entry, stdout_color, stdout_include_time);
|
||||
println!("{}", line);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if config.log_to_file {
|
||||
let file_receiver = receiver.clone();
|
||||
let log_file_path = config.log_file_path.clone();
|
||||
let file_include_time = config.file_include_time;
|
||||
std::thread::spawn(move || {
|
||||
let file = OpenOptions::new()
|
||||
.append(true)
|
||||
.create(true)
|
||||
.open(&log_file_path)
|
||||
.unwrap_or_else(|_| {
|
||||
OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(&log_file_path)
|
||||
.expect("Failed to create log file")
|
||||
});
|
||||
let mut writer = BufWriter::new(file);
|
||||
for entry in file_receiver {
|
||||
let line = format_entry(&entry, false, file_include_time);
|
||||
writer
|
||||
.write_all(format!("{}\n", line).as_bytes())
|
||||
.expect("Failed to write to log file");
|
||||
writer.flush().expect("Failed to flush log file");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let buffer_layer = BufferLayer {
|
||||
log_entries: log_entries.clone(),
|
||||
sender: sender.clone(),
|
||||
};
|
||||
|
||||
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
|
||||
format!("{}={}", env!("CARGO_CRATE_NAME"), config.log_level).into()
|
||||
});
|
||||
|
||||
let subscriber = tracing_subscriber::registry()
|
||||
.with(env_filter)
|
||||
.with(buffer_layer);
|
||||
subscriber.init();
|
||||
|
||||
Self {
|
||||
config,
|
||||
log_entries,
|
||||
_sender: sender,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_logs(&self, log_query: LogQuery) -> Vec<LogEntry> {
|
||||
let guard = self.log_entries.read().unwrap();
|
||||
match log_query {
|
||||
LogQuery::All => guard.clone(),
|
||||
LogQuery::From(time) => guard
|
||||
.iter()
|
||||
.filter(|e| e.timestamp >= time)
|
||||
.cloned()
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn format_entry(entry: &LogEntry, use_color: bool, include_time: bool) -> String {
|
||||
let timestamp = if include_time {
|
||||
format!(
|
||||
"[{}] ",
|
||||
humantime::format_rfc3339_seconds(entry.timestamp)
|
||||
.to_string()
|
||||
.trim_end_matches('Z')
|
||||
.replace('T', " ")
|
||||
)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
let level = if use_color {
|
||||
match entry.level {
|
||||
Level::ERROR => format!("\x1b[31m{}\x1b[0m", entry.level),
|
||||
Level::WARN => format!("\x1b[33m{}\x1b[0m", entry.level),
|
||||
Level::INFO => format!("\x1b[32m{}\x1b[0m", entry.level),
|
||||
Level::DEBUG => format!("\x1b[36m{}\x1b[0m", entry.level),
|
||||
Level::TRACE => format!("\x1b[34m{}\x1b[0m", entry.level),
|
||||
}
|
||||
} else {
|
||||
entry.level.to_string()
|
||||
};
|
||||
|
||||
format!("{}{} {}", timestamp, level, entry.message)
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let logger_config = LoggerConfig::default()
|
||||
fn main() {
|
||||
let config = LoggerConfig::default()
|
||||
.colored_stdout(true)
|
||||
.level(LogLevel::Debug)
|
||||
.log_to_file(true)
|
||||
.stdout_include_time(false)
|
||||
.log_to_stdout(true)
|
||||
.file_include_time(true)
|
||||
.log_path(format!("{}.log", env!("CARGO_PKG_NAME")));
|
||||
.log_to_file(true)
|
||||
.log_path("zenyx.log");
|
||||
let _logger = zlog::Logger::new(config);
|
||||
|
||||
let logger = Logger::new(logger_config);
|
||||
|
||||
let app = App::default();
|
||||
tracing::info!("Application initialized");
|
||||
tracing::error!("Another log message with value: {}", 42);
|
||||
|
||||
let logs = logger.get_logs(LogQuery::All);
|
||||
println!("\n--- All Logs ---");
|
||||
for entry in logs {
|
||||
println!("{}", format_entry(&entry, false, true));
|
||||
}
|
||||
|
||||
let now = SystemTime::now();
|
||||
tracing::warn!("This is a warning after the initial logs");
|
||||
let new_logs = logger.get_logs(LogQuery::From(now));
|
||||
println!("\n--- Logs Since Now ---");
|
||||
for entry in new_logs {
|
||||
println!("{}", format_entry(&entry, false, true));
|
||||
}
|
||||
info!("Application finished");
|
||||
|
||||
// logger.get_logs();
|
||||
|
||||
Ok(())
|
||||
info!("This workss")
|
||||
}
|
||||
|
|
11
subcrates/zlog/Cargo.toml
Normal file
11
subcrates/zlog/Cargo.toml
Normal file
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "zlog"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
tracing = "0.1.41"
|
||||
tracing-subscriber = "0.3.19"
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = "1.4.1"
|
58
subcrates/zlog/src/config.rs
Normal file
58
subcrates/zlog/src/config.rs
Normal file
|
@ -0,0 +1,58 @@
|
|||
use crate::LogLevel;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct LoggerConfig {
|
||||
pub(crate) log_level: LogLevel,
|
||||
pub(crate) log_to_file: bool,
|
||||
pub(crate) log_file_path: PathBuf,
|
||||
pub(crate) log_to_stdout: bool,
|
||||
pub(crate) stdout_color: bool,
|
||||
pub(crate) stdout_include_time: bool,
|
||||
pub(crate) file_include_time: bool,
|
||||
}
|
||||
|
||||
impl LoggerConfig {
|
||||
pub fn level(mut self, level: LogLevel) -> Self {
|
||||
self.log_level = level;
|
||||
self
|
||||
}
|
||||
pub fn log_to_file(mut self, f: bool) -> Self {
|
||||
self.log_to_file = f;
|
||||
self
|
||||
}
|
||||
pub fn colored_stdout(mut self, c: bool) -> Self {
|
||||
self.stdout_color = c;
|
||||
self
|
||||
}
|
||||
pub fn log_to_stdout(mut self, s: bool) -> Self {
|
||||
self.log_to_stdout = s;
|
||||
self
|
||||
}
|
||||
pub fn log_path<P: AsRef<Path>>(mut self, p: P) -> Self {
|
||||
self.log_file_path = p.as_ref().to_path_buf();
|
||||
self
|
||||
}
|
||||
pub fn stdout_include_time(mut self, i: bool) -> Self {
|
||||
self.stdout_include_time = i;
|
||||
self
|
||||
}
|
||||
pub fn file_include_time(mut self, i: bool) -> Self {
|
||||
self.file_include_time = i;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for LoggerConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
log_level: LogLevel::Debug,
|
||||
log_to_file: true,
|
||||
log_file_path: "app.log".into(),
|
||||
log_to_stdout: true,
|
||||
stdout_color: true,
|
||||
stdout_include_time: false,
|
||||
file_include_time: false,
|
||||
}
|
||||
}
|
||||
}
|
284
subcrates/zlog/src/lib.rs
Normal file
284
subcrates/zlog/src/lib.rs
Normal file
|
@ -0,0 +1,284 @@
|
|||
pub mod config;
|
||||
pub mod query;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use config::LoggerConfig;
|
||||
use query::LogQuery;
|
||||
use std::{
|
||||
fmt,
|
||||
fs::OpenOptions,
|
||||
io::{BufWriter, Write},
|
||||
str::FromStr,
|
||||
sync::{
|
||||
Arc, RwLock,
|
||||
mpsc::{self, Sender},
|
||||
},
|
||||
thread,
|
||||
time::SystemTime,
|
||||
};
|
||||
use tracing::{Event, Level, Subscriber};
|
||||
use tracing_subscriber::{
|
||||
layer::{Context, Layer, SubscriberExt},
|
||||
registry::LookupSpan,
|
||||
util::SubscriberInitExt,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum LogEvent {
|
||||
Log(LogEntry),
|
||||
Shutdown,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct LogEntry {
|
||||
timestamp: SystemTime,
|
||||
level: Level,
|
||||
message: String,
|
||||
}
|
||||
|
||||
impl PartialOrd for LogEntry {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for LogEntry {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
self.timestamp
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.cmp(
|
||||
&other
|
||||
.timestamp
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
struct BufferLayer {
|
||||
log_entries: Arc<RwLock<Vec<LogEntry>>>,
|
||||
senders: Vec<Sender<LogEvent>>,
|
||||
}
|
||||
|
||||
impl BufferLayer {
|
||||
pub fn new(log_entries: Arc<RwLock<Vec<LogEntry>>>, senders: Vec<Sender<LogEvent>>) -> Self {
|
||||
Self {
|
||||
log_entries,
|
||||
senders,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for BufferLayer {
|
||||
fn drop(&mut self) {
|
||||
for tx in &self.senders {
|
||||
tx.send(LogEvent::Shutdown).unwrap();
|
||||
}
|
||||
self.senders.clear();
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Layer<S> for BufferLayer
|
||||
where
|
||||
S: Subscriber + for<'a> LookupSpan<'a>,
|
||||
{
|
||||
fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) {
|
||||
let metadata = event.metadata();
|
||||
let level = *metadata.level();
|
||||
let timestamp = SystemTime::now();
|
||||
let mut message = String::new();
|
||||
let mut visitor = LogVisitor::new(&mut message);
|
||||
event.record(&mut visitor);
|
||||
|
||||
let log_entry = LogEvent::Log(LogEntry {
|
||||
timestamp,
|
||||
level,
|
||||
message,
|
||||
});
|
||||
|
||||
if let LogEvent::Log(ref entry) = log_entry {
|
||||
if let Ok(mut buf) = self.log_entries.write() {
|
||||
buf.push(entry.clone());
|
||||
}
|
||||
}
|
||||
|
||||
for tx in &self.senders {
|
||||
let _ = tx.send(log_entry.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct LogVisitor<'msg> {
|
||||
message: &'msg mut String,
|
||||
}
|
||||
|
||||
impl<'msg> LogVisitor<'msg> {
|
||||
fn new(message: &'msg mut String) -> Self {
|
||||
Self { message }
|
||||
}
|
||||
}
|
||||
|
||||
impl tracing::field::Visit for LogVisitor<'_> {
|
||||
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn fmt::Debug) {
|
||||
use std::fmt::Write;
|
||||
if field.name() == "message" {
|
||||
write!(self.message, "{:?}", value).unwrap();
|
||||
} else {
|
||||
write!(self.message, "{}={:?} ", field.name(), value).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
|
||||
pub enum LogLevel {
|
||||
Error,
|
||||
#[default]
|
||||
Info,
|
||||
Warn,
|
||||
Debug,
|
||||
Trace,
|
||||
}
|
||||
|
||||
impl From<LogLevel> for tracing::Level {
|
||||
fn from(level: LogLevel) -> Self {
|
||||
match level {
|
||||
LogLevel::Error => Level::ERROR,
|
||||
LogLevel::Info => Level::INFO,
|
||||
LogLevel::Debug => Level::DEBUG,
|
||||
LogLevel::Trace => Level::TRACE,
|
||||
LogLevel::Warn => Level::WARN,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for LogLevel {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for LogLevel {
|
||||
type Err = ();
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s {
|
||||
"trace" => Ok(Self::Trace),
|
||||
"debug" => Ok(Self::Debug),
|
||||
"info" => Ok(Self::Info),
|
||||
"error" => Ok(Self::Error),
|
||||
"warn" => Ok(Self::Warn),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Logger {
|
||||
subscriber_guard: Option<tracing::subscriber::DefaultGuard>,
|
||||
log_entries: Arc<RwLock<Vec<LogEntry>>>,
|
||||
_handles: Vec<thread::JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl Logger {
|
||||
pub fn new(config: LoggerConfig) -> Self {
|
||||
let log_entries = Arc::new(RwLock::new(Vec::new()));
|
||||
let mut senders = Vec::new();
|
||||
let mut handles = Vec::new();
|
||||
|
||||
if config.log_to_stdout {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
senders.push(tx);
|
||||
let config_clone = config.clone();
|
||||
let handle = thread::spawn(move || {
|
||||
for msg in rx {
|
||||
match msg {
|
||||
LogEvent::Log(entry) => {
|
||||
println!(
|
||||
"{}",
|
||||
format_entry(
|
||||
&entry,
|
||||
config_clone.stdout_color,
|
||||
config_clone.stdout_include_time
|
||||
)
|
||||
);
|
||||
}
|
||||
LogEvent::Shutdown => break,
|
||||
}
|
||||
}
|
||||
});
|
||||
handles.push(handle);
|
||||
}
|
||||
|
||||
if config.log_to_file {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
senders.push(tx);
|
||||
let path = config.log_file_path.clone();
|
||||
let include_time = config.file_include_time;
|
||||
let handle = thread::spawn(move || {
|
||||
let file = OpenOptions::new()
|
||||
.append(true)
|
||||
.create(true)
|
||||
.open(&path)
|
||||
.expect("Failed to open log file");
|
||||
let mut writer = BufWriter::new(file);
|
||||
for msg in rx {
|
||||
match msg {
|
||||
LogEvent::Log(entry) => {
|
||||
let line = format_entry(&entry, false, include_time);
|
||||
writeln!(writer, "{}", line).expect("Failed to write to log file");
|
||||
writer.flush().expect("Failed to flush log file");
|
||||
}
|
||||
LogEvent::Shutdown => break,
|
||||
}
|
||||
}
|
||||
});
|
||||
handles.push(handle);
|
||||
}
|
||||
|
||||
let buffer_layer = BufferLayer::new(log_entries.clone(), senders);
|
||||
let subscriber = tracing_subscriber::registry().with(buffer_layer);
|
||||
let guard = subscriber.set_default();
|
||||
|
||||
Logger {
|
||||
subscriber_guard: Some(guard),
|
||||
log_entries,
|
||||
_handles: handles,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_logs(&self, query: LogQuery) -> Vec<LogEntry> {
|
||||
let guard = self.log_entries.read().unwrap();
|
||||
match query {
|
||||
LogQuery::All => guard.clone(),
|
||||
LogQuery::From(t) => guard.iter().filter(|e| e.timestamp >= t).cloned().collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Logger {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.subscriber_guard.take();
|
||||
let handles = std::mem::take(&mut self._handles);
|
||||
for handle in handles {
|
||||
handle.join().unwrap_or_else(|e| {
|
||||
eprintln!("Logger thread panicked: {:?}", e);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn format_entry(entry: &LogEntry, use_color: bool, _: bool) -> String {
|
||||
let lvl = if use_color {
|
||||
match entry.level {
|
||||
Level::ERROR => "\x1b[31mERROR\x1b[0m",
|
||||
Level::WARN => "\x1b[33mWARN\x1b[0m",
|
||||
Level::INFO => "\x1b[32mINFO\x1b[0m",
|
||||
Level::DEBUG => "\x1b[36mDEBUG\x1b[0m",
|
||||
Level::TRACE => "\x1b[34mTRACE\x1b[0m",
|
||||
}
|
||||
} else {
|
||||
entry.level.as_str()
|
||||
};
|
||||
|
||||
format!("{} {}", lvl, entry.message)
|
||||
}
|
14
subcrates/zlog/src/query.rs
Normal file
14
subcrates/zlog/src/query.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
use std::time::SystemTime;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Default)]
|
||||
pub enum LogQuery {
|
||||
#[default]
|
||||
All,
|
||||
From(SystemTime),
|
||||
}
|
||||
|
||||
impl LogQuery {
|
||||
pub fn since(time: SystemTime) -> Self {
|
||||
LogQuery::From(time)
|
||||
}
|
||||
}
|
90
subcrates/zlog/src/tests.rs
Normal file
90
subcrates/zlog/src/tests.rs
Normal file
|
@ -0,0 +1,90 @@
|
|||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tracing::Level;
|
||||
|
||||
#[test]
|
||||
fn test_logger_sequential_consistency() {
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use tracing::{debug, error, info, trace, warn};
|
||||
|
||||
let config = LoggerConfig::default()
|
||||
.log_to_stdout(true)
|
||||
.log_to_file(true);
|
||||
let logger = Logger::new(config);
|
||||
|
||||
static COUNTER: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
for i in 0..4096 * 128 {
|
||||
let count = COUNTER.fetch_add(1, Ordering::SeqCst);
|
||||
match i % 5 {
|
||||
0 => error!("Error message {}", count),
|
||||
1 => warn!("Warning message {}", count),
|
||||
2 => info!("Info message {}", count),
|
||||
3 => debug!("Debug message {}", count),
|
||||
_ => trace!("Trace message {}", count),
|
||||
}
|
||||
}
|
||||
|
||||
let logs = logger.get_logs(LogQuery::All);
|
||||
assert_eq!(
|
||||
logs.len(),
|
||||
4096 * 128,
|
||||
"Should have exactly 5000 log entries"
|
||||
);
|
||||
|
||||
for (i, log) in logs.iter().enumerate() {
|
||||
let expected_count = i;
|
||||
let expected_level = match i % 5 {
|
||||
0 => Level::ERROR,
|
||||
1 => Level::WARN,
|
||||
2 => Level::INFO,
|
||||
3 => Level::DEBUG,
|
||||
_ => Level::TRACE,
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
log.level, expected_level,
|
||||
"Log {} has incorrect level: {:?}",
|
||||
i, log.level
|
||||
);
|
||||
|
||||
let expected_msg = match expected_level {
|
||||
Level::ERROR => format!("Error message {}", expected_count),
|
||||
Level::WARN => format!("Warning message {}", expected_count),
|
||||
Level::INFO => format!("Info message {}", expected_count),
|
||||
Level::DEBUG => format!("Debug message {}", expected_count),
|
||||
Level::TRACE => format!("Trace message {}", expected_count),
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
log.message, expected_msg,
|
||||
"Log {} has incorrect message. Expected: '{}', Got: '{}'",
|
||||
i, expected_msg, log.message
|
||||
);
|
||||
|
||||
if i > 0 {
|
||||
assert!(
|
||||
log.timestamp >= logs[i - 1].timestamp,
|
||||
"Log {} has timestamp out of order. Current: {:?}, Previous: {:?}",
|
||||
i,
|
||||
log.timestamp,
|
||||
logs[i - 1].timestamp
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let mut counts: Vec<usize> = logs
|
||||
.iter()
|
||||
.map(|log| {
|
||||
log.message
|
||||
.split_whitespace()
|
||||
.last()
|
||||
.unwrap()
|
||||
.parse::<usize>()
|
||||
.unwrap()
|
||||
})
|
||||
.collect();
|
||||
counts.sort();
|
||||
counts.dedup();
|
||||
assert_eq!(counts.len(), 4096 * 128, "Found duplicate log entries");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue