🔊 Update 1.0.0

This commit is contained in:
Nanaloveyuki
2026-06-27 10:45:36 +08:00
parent 4cc43def73
commit 0cbe25a551
55 changed files with 5734 additions and 2360 deletions
File diff suppressed because it is too large Load Diff
+8 -1
View File
@@ -1,19 +1,26 @@
///|
pub type ApplicationAsyncLogger = AsyncLogger[@bitlogger.RuntimeSink]
pub type ApplicationTextAsyncLogger = AsyncLogger[@bitlogger.FormattedConsoleSink]
///|
pub type ApplicationTextAsyncLogger = AsyncLogger[
@bitlogger.FormattedConsoleSink,
]
///|
pub fn build_application_async_logger(
config : AsyncLoggerBuildConfig,
) -> ApplicationAsyncLogger {
build_async_logger(config)
}
///|
pub fn build_application_text_async_logger(
config : AsyncLoggerBuildConfig,
) -> ApplicationTextAsyncLogger {
build_async_text_logger(config)
}
///|
pub fn parse_and_build_application_async_logger(
input : String,
) -> ApplicationAsyncLogger raise {
+5
View File
@@ -1,20 +1,25 @@
///|
pub fn async_runtime_mode() -> AsyncRuntimeMode {
@utils.native_worker_async_runtime_mode()
}
///|
pub fn async_runtime_supports_background_worker() -> Bool {
ignore(all_async_runtime_modes())
true
}
///|
fn async_runtime_guard_closed_on_log() -> Bool {
false
}
///|
fn async_runtime_shutdown_clears_pending_after_wait_idle() -> Bool {
true
}
///|
fn async_runtime_shutdown_waits_for_worker() -> Bool {
true
}
+153 -69
View File
@@ -1,84 +1,125 @@
///|
pub(all) suberror AsyncLoggerClosed {
AsyncLoggerClosed
}
///|
pub type AsyncOverflowPolicy = @utils.AsyncOverflowPolicy
///|
pub type AsyncFlushPolicy = @utils.AsyncFlushPolicy
///|
pub type AsyncRuntimeMode = @utils.AsyncRuntimeMode
///|
fn all_async_runtime_modes() -> Array[AsyncRuntimeMode] {
[@utils.native_worker_async_runtime_mode(), @utils.compatibility_async_runtime_mode()]
[
@utils.native_worker_async_runtime_mode(),
@utils.compatibility_async_runtime_mode(),
]
}
///|
pub type AsyncRuntimeState = @utils.AsyncRuntimeState
///|
pub type AsyncLoggerState = @utils.AsyncLoggerState
///|
pub fn async_runtime_mode_label(mode : AsyncRuntimeMode) -> String {
@utils.async_runtime_mode_label(mode)
}
///|
pub fn async_runtime_state() -> AsyncRuntimeState {
AsyncRuntimeState::new(async_runtime_mode(), async_runtime_supports_background_worker())
AsyncRuntimeState::new(
async_runtime_mode(),
async_runtime_supports_background_worker(),
)
}
pub fn async_runtime_state_to_json(state : AsyncRuntimeState) -> @json_parser.JsonValue {
///|
pub fn async_runtime_state_to_json(
state : AsyncRuntimeState,
) -> @json_parser.JsonValue {
@utils.async_runtime_state_to_json(state)
}
///|
pub fn stringify_async_runtime_state(
state : AsyncRuntimeState,
pretty~ : Bool = false,
pretty? : Bool = false,
) -> String {
@utils.stringify_async_runtime_state(state, pretty=pretty)
@utils.stringify_async_runtime_state(state, pretty~)
}
pub fn async_logger_state_to_json(state : AsyncLoggerState) -> @json_parser.JsonValue {
///|
pub fn async_logger_state_to_json(
state : AsyncLoggerState,
) -> @json_parser.JsonValue {
@utils.async_logger_state_to_json(state)
}
///|
pub fn stringify_async_logger_state(
state : AsyncLoggerState,
pretty~ : Bool = false,
pretty? : Bool = false,
) -> String {
@utils.stringify_async_logger_state(state, pretty=pretty)
@utils.stringify_async_logger_state(state, pretty~)
}
///|
pub type AsyncLoggerConfig = @utils.AsyncLoggerConfig
pub fn parse_async_logger_config_text(input : String) -> AsyncLoggerConfig raise {
///|
pub fn parse_async_logger_config_text(
input : String,
) -> AsyncLoggerConfig raise {
@utils.parse_async_logger_config_text(input)
}
pub fn async_logger_config_to_json(config : AsyncLoggerConfig) -> @json_parser.JsonValue {
///|
pub fn async_logger_config_to_json(
config : AsyncLoggerConfig,
) -> @json_parser.JsonValue {
@utils.async_logger_config_to_json(config)
}
pub fn stringify_async_logger_config(config : AsyncLoggerConfig, pretty~ : Bool = false) -> String {
@utils.stringify_async_logger_config(config, pretty=pretty)
///|
pub fn stringify_async_logger_config(
config : AsyncLoggerConfig,
pretty? : Bool = false,
) -> String {
@utils.stringify_async_logger_config(config, pretty~)
}
///|
pub type AsyncLoggerBuildConfig = @utils.AsyncLoggerBuildConfig
pub fn parse_async_logger_build_config_text(input : String) -> AsyncLoggerBuildConfig raise {
///|
pub fn parse_async_logger_build_config_text(
input : String,
) -> AsyncLoggerBuildConfig raise {
@utils.parse_async_logger_build_config_text(input)
}
///|
pub fn async_logger_build_config_to_json(
config : AsyncLoggerBuildConfig,
) -> @json_parser.JsonValue {
@utils.async_logger_build_config_to_json(config)
}
///|
pub fn stringify_async_logger_build_config(
config : AsyncLoggerBuildConfig,
pretty~ : Bool = false,
pretty? : Bool = false,
) -> String {
@utils.stringify_async_logger_build_config(config, pretty=pretty)
@utils.stringify_async_logger_build_config(config, pretty~)
}
///|
pub struct AsyncLogger[S] {
min_level : @bitlogger.Level
target : String
@@ -101,12 +142,13 @@ pub struct AsyncLogger[S] {
last_error : Ref[String]
}
///|
pub fn[S] async_logger(
sink : S,
config~ : AsyncLoggerConfig = AsyncLoggerConfig::new(),
min_level~ : @bitlogger.Level = @bitlogger.Level::Info,
target~ : String = "",
flush~ : (S) -> Int raise = fn(_) { 0 },
config? : AsyncLoggerConfig = AsyncLoggerConfig::new(),
min_level? : @bitlogger.Level = @bitlogger.Level::Info,
target? : String = "",
flush? : (S) -> Int raise = fn(_) { 0 },
) -> AsyncLogger[S] {
{
min_level,
@@ -131,6 +173,7 @@ pub fn[S] async_logger(
}
}
///|
fn queue_kind_of(config : AsyncLoggerConfig) -> @aqueue.Kind {
let limit = if config.max_pending < 0 { 0 } else { config.max_pending }
match config.overflow {
@@ -140,14 +183,23 @@ fn queue_kind_of(config : AsyncLoggerConfig) -> @aqueue.Kind {
}
}
pub fn[S] AsyncLogger::with_timestamp(self : AsyncLogger[S], enabled~ : Bool = true) -> AsyncLogger[S] {
///|
pub fn[S] AsyncLogger::with_timestamp(
self : AsyncLogger[S],
enabled? : Bool = true,
) -> AsyncLogger[S] {
{ ..self, timestamp: enabled }
}
pub fn[S] AsyncLogger::with_target(self : AsyncLogger[S], target : String) -> AsyncLogger[S] {
{ ..self, target }
///|
pub fn[S] AsyncLogger::with_target(
self : AsyncLogger[S],
target : String,
) -> AsyncLogger[S] {
{ ..self, target, }
}
///|
pub fn[S] AsyncLogger::with_context_fields(
self : AsyncLogger[S],
fields : Array[@bitlogger.Field],
@@ -155,39 +207,33 @@ pub fn[S] AsyncLogger::with_context_fields(
{ ..self, context_fields: fields }
}
///|
pub fn[S] AsyncLogger::with_filter(
self : AsyncLogger[S],
predicate : (@bitlogger.Record) -> Bool,
) -> AsyncLogger[S] {
let current = self.filter
{
..self,
filter: fn(rec) {
current(rec) && predicate(rec)
},
}
{ ..self, filter: fn(rec) { current(rec) && predicate(rec) } }
}
///|
pub fn[S] AsyncLogger::with_patch(
self : AsyncLogger[S],
patch : @bitlogger.RecordPatch,
) -> AsyncLogger[S] {
let current = self.patch
{
..self,
patch: fn(rec) {
patch(current(rec))
},
}
{ ..self, patch: fn(rec) { patch(current(rec)) } }
}
///|
pub fn[S] AsyncLogger::with_min_level(
self : AsyncLogger[S],
min_level : @bitlogger.Level,
) -> AsyncLogger[S] {
{ ..self, min_level }
{ ..self, min_level, }
}
///|
fn combine_targets(parent : String, child : String) -> String {
if parent == "" {
child
@@ -198,14 +244,23 @@ fn combine_targets(parent : String, child : String) -> String {
}
}
pub fn[S] AsyncLogger::child(self : AsyncLogger[S], target : String) -> AsyncLogger[S] {
///|
pub fn[S] AsyncLogger::child(
self : AsyncLogger[S],
target : String,
) -> AsyncLogger[S] {
{ ..self, target: combine_targets(self.target, target) }
}
pub fn[S] AsyncLogger::is_enabled(self : AsyncLogger[S], level : @bitlogger.Level) -> Bool {
///|
pub fn[S] AsyncLogger::is_enabled(
self : AsyncLogger[S],
level : @bitlogger.Level,
) -> Bool {
level.enabled(self.min_level)
}
///|
fn merge_fields(
left : Array[@bitlogger.Field],
right : Array[@bitlogger.Field],
@@ -219,32 +274,27 @@ fn merge_fields(
}
}
///|
pub async fn[S] AsyncLogger::log(
self : AsyncLogger[S],
level : @bitlogger.Level,
message : String,
fields~ : Array[@bitlogger.Field] = [],
fields? : Array[@bitlogger.Field] = [],
target? : String = "",
) -> Unit {
guard !(async_runtime_guard_closed_on_log() && self.is_closed()) else {
()
}
guard self.is_enabled(level) else {
()
}
guard !(async_runtime_guard_closed_on_log() && self.is_closed()) else { () }
guard self.is_enabled(level) else { () }
let actual_target = if target == "" { self.target } else { target }
let timestamp_ms = if self.timestamp { @env.now() } else { 0UL }
let rec = @bitlogger.Record::new(
level,
message,
timestamp_ms=timestamp_ms,
timestamp_ms~,
target=actual_target,
fields=merge_fields(self.context_fields, fields),
)
let rec = (self.patch)(rec)
guard (self.filter)(rec) else {
()
}
guard (self.filter)(rec) else { () }
let accepted = self.queue.try_put(rec) catch {
err if err is AsyncLoggerClosed => false
err => raise err
@@ -254,7 +304,7 @@ pub async fn[S] AsyncLogger::log(
} else {
match self.overflow {
AsyncOverflowPolicy::Blocking => {
let accepted = (async fn() -> Bool raise {
let accepted = (async fn() -> Bool {
self.queue.put(rec)
true
})() catch {
@@ -265,81 +315,93 @@ pub async fn[S] AsyncLogger::log(
self.pending_count.val += 1
}
}
AsyncOverflowPolicy::DropOldest | AsyncOverflowPolicy::DropNewest => {
AsyncOverflowPolicy::DropOldest | AsyncOverflowPolicy::DropNewest =>
self.dropped_count.val += 1
}
}
}
}
///|
pub async fn[S] AsyncLogger::trace(
self : AsyncLogger[S],
message : String,
fields~ : Array[@bitlogger.Field] = [],
fields? : Array[@bitlogger.Field] = [],
) -> Unit {
self.log(@bitlogger.Level::Trace, message, fields=fields)
self.log(@bitlogger.Level::Trace, message, fields~)
}
///|
pub async fn[S] AsyncLogger::debug(
self : AsyncLogger[S],
message : String,
fields~ : Array[@bitlogger.Field] = [],
fields? : Array[@bitlogger.Field] = [],
) -> Unit {
self.log(@bitlogger.Level::Debug, message, fields=fields)
self.log(@bitlogger.Level::Debug, message, fields~)
}
///|
pub async fn[S] AsyncLogger::info(
self : AsyncLogger[S],
message : String,
fields~ : Array[@bitlogger.Field] = [],
fields? : Array[@bitlogger.Field] = [],
) -> Unit {
self.log(@bitlogger.Level::Info, message, fields=fields)
self.log(@bitlogger.Level::Info, message, fields~)
}
///|
pub async fn[S] AsyncLogger::warn(
self : AsyncLogger[S],
message : String,
fields~ : Array[@bitlogger.Field] = [],
fields? : Array[@bitlogger.Field] = [],
) -> Unit {
self.log(@bitlogger.Level::Warn, message, fields=fields)
self.log(@bitlogger.Level::Warn, message, fields~)
}
///|
pub async fn[S] AsyncLogger::error(
self : AsyncLogger[S],
message : String,
fields~ : Array[@bitlogger.Field] = [],
fields? : Array[@bitlogger.Field] = [],
) -> Unit {
self.log(@bitlogger.Level::Error, message, fields=fields)
self.log(@bitlogger.Level::Error, message, fields~)
}
///|
pub fn[S] AsyncLogger::pending_count(self : AsyncLogger[S]) -> Int {
self.pending_count.val
}
///|
pub fn[S] AsyncLogger::dropped_count(self : AsyncLogger[S]) -> Int {
self.dropped_count.val
}
///|
pub fn[S] AsyncLogger::is_closed(self : AsyncLogger[S]) -> Bool {
self.is_closed.val
}
///|
pub fn[S] AsyncLogger::is_running(self : AsyncLogger[S]) -> Bool {
self.is_running.val
}
///|
pub fn[S] AsyncLogger::has_failed(self : AsyncLogger[S]) -> Bool {
self.has_failed.val
}
///|
pub fn[S] AsyncLogger::last_error(self : AsyncLogger[S]) -> String {
self.last_error.val
}
///|
pub fn[S] AsyncLogger::flush_policy(self : AsyncLogger[S]) -> AsyncFlushPolicy {
self.flush_policy
}
///|
pub fn[S] AsyncLogger::state(self : AsyncLogger[S]) -> AsyncLoggerState {
AsyncLoggerState::new(
async_runtime_state(),
@@ -353,7 +415,11 @@ pub fn[S] AsyncLogger::state(self : AsyncLogger[S]) -> AsyncLoggerState {
)
}
pub fn[S] AsyncLogger::close(self : AsyncLogger[S], clear? : Bool = false) -> Unit {
///|
pub fn[S] AsyncLogger::close(
self : AsyncLogger[S],
clear? : Bool = false,
) -> Unit {
self.is_closed.val = true
if clear {
let abandoned = self.pending_count()
@@ -362,9 +428,10 @@ pub fn[S] AsyncLogger::close(self : AsyncLogger[S], clear? : Bool = false) -> Un
self.pending_count.val = 0
}
}
self.queue.close(error=AsyncLoggerClosed, clear=clear)
self.queue.close(error=AsyncLoggerClosed, clear~)
}
///|
pub async fn[S] AsyncLogger::wait_idle(self : AsyncLogger[S]) -> Unit {
while self.pending_count() > 0 {
if self.has_failed() {
@@ -374,12 +441,17 @@ pub async fn[S] AsyncLogger::wait_idle(self : AsyncLogger[S]) -> Unit {
}
}
pub async fn[S] AsyncLogger::shutdown(self : AsyncLogger[S], clear? : Bool = false) -> Unit {
///|
pub async fn[S] AsyncLogger::shutdown(
self : AsyncLogger[S],
clear? : Bool = false,
) -> Unit {
if clear {
self.close(clear=true)
} else {
self.wait_idle()
if async_runtime_shutdown_clears_pending_after_wait_idle() && self.pending_count() > 0 {
if async_runtime_shutdown_clears_pending_after_wait_idle() &&
self.pending_count() > 0 {
self.close(clear=true)
} else {
self.close()
@@ -392,6 +464,7 @@ pub async fn[S] AsyncLogger::shutdown(self : AsyncLogger[S], clear? : Bool = fal
}
}
///|
async fn[S : @bitlogger.Sink] run_worker(logger : AsyncLogger[S]) -> Unit {
while true {
let rec = logger.queue.get() catch {
@@ -419,7 +492,9 @@ async fn[S : @bitlogger.Sink] run_worker(logger : AsyncLogger[S]) -> Unit {
if logger.linger_ms <= 0 {
break
}
let waited = @async.with_timeout_opt(logger.linger_ms, () => logger.queue.get()) catch {
let waited = @async.with_timeout_opt(logger.linger_ms, () => {
logger.queue.get()
}) catch {
err if err is AsyncLoggerClosed => None
err => raise err
}
@@ -447,7 +522,10 @@ async fn[S : @bitlogger.Sink] run_worker(logger : AsyncLogger[S]) -> Unit {
}
}
pub async fn[S : @bitlogger.Sink] AsyncLogger::run(self : AsyncLogger[S]) -> Unit {
///|
pub async fn[S : @bitlogger.Sink] AsyncLogger::run(
self : AsyncLogger[S],
) -> Unit {
self.is_running.val = true
self.has_failed.val = false
self.last_error.val = ""
@@ -462,6 +540,7 @@ pub async fn[S : @bitlogger.Sink] AsyncLogger::run(self : AsyncLogger[S]) -> Uni
self.is_running.val = false
}
///|
pub fn build_async_logger(
config : AsyncLoggerBuildConfig,
) -> AsyncLogger[@bitlogger.RuntimeSink] {
@@ -475,9 +554,14 @@ pub fn build_async_logger(
).with_timestamp(enabled=logger.timestamp)
}
pub fn build_async_text_logger(config : AsyncLoggerBuildConfig) -> AsyncLogger[@bitlogger.FormattedConsoleSink] {
///|
pub fn build_async_text_logger(
config : AsyncLoggerBuildConfig,
) -> AsyncLogger[@bitlogger.FormattedConsoleSink] {
async_logger(
@bitlogger.text_console_sink(config.logger.sink.text_formatter.to_formatter()),
@bitlogger.text_console_sink(
config.logger.sink.text_formatter.to_formatter(),
),
config=config.async_config,
min_level=config.logger.min_level,
target=config.logger.target,
+5
View File
@@ -1,20 +1,25 @@
///|
pub fn async_runtime_mode() -> AsyncRuntimeMode {
@utils.compatibility_async_runtime_mode()
}
///|
pub fn async_runtime_supports_background_worker() -> Bool {
ignore(all_async_runtime_modes())
false
}
///|
fn async_runtime_guard_closed_on_log() -> Bool {
true
}
///|
fn async_runtime_shutdown_clears_pending_after_wait_idle() -> Bool {
false
}
///|
fn async_runtime_shutdown_waits_for_worker() -> Bool {
false
}
+43 -25
View File
@@ -1,61 +1,67 @@
///|
pub struct LibraryAsyncLogger[S] {
inner : AsyncLogger[S]
}
///|
fn[S] library_async_logger(logger : AsyncLogger[S]) -> LibraryAsyncLogger[S] {
{ inner: logger }
}
pub fn[S] AsyncLogger::to_library_async_logger(self : AsyncLogger[S]) -> LibraryAsyncLogger[S] {
///|
pub fn[S] AsyncLogger::to_library_async_logger(
self : AsyncLogger[S],
) -> LibraryAsyncLogger[S] {
library_async_logger(self)
}
///|
pub fn[S] LibraryAsyncLogger::new(
sink : S,
config~ : AsyncLoggerConfig = AsyncLoggerConfig::new(),
min_level~ : @bitlogger.Level = @bitlogger.Level::Info,
target~ : String = "",
flush~ : (S) -> Int raise = fn(_) { 0 },
config? : AsyncLoggerConfig = AsyncLoggerConfig::new(),
min_level? : @bitlogger.Level = @bitlogger.Level::Info,
target? : String = "",
flush? : (S) -> Int raise = fn(_) { 0 },
) -> LibraryAsyncLogger[S] {
library_async_logger(
async_logger(
sink,
config=config,
min_level=min_level,
target=target,
flush=flush,
),
)
library_async_logger(async_logger(sink, config~, min_level~, target~, flush~))
}
pub fn[S] LibraryAsyncLogger::to_async_logger(self : LibraryAsyncLogger[S]) -> AsyncLogger[S] {
///|
pub fn[S] LibraryAsyncLogger::to_async_logger(
self : LibraryAsyncLogger[S],
) -> AsyncLogger[S] {
self.inner
}
///|
fn[S] configured_library_async_logger(
logger : AsyncLogger[S],
) -> LibraryAsyncLogger[S] {
library_async_logger(logger)
}
///|
pub fn build_library_async_logger(
config : AsyncLoggerBuildConfig,
) -> LibraryAsyncLogger[@bitlogger.RuntimeSink] {
configured_library_async_logger(build_async_logger(config))
}
///|
pub fn build_library_async_text_logger(
config : AsyncLoggerBuildConfig,
) -> LibraryAsyncLogger[@bitlogger.FormattedConsoleSink] {
configured_library_async_logger(build_async_text_logger(config))
}
///|
pub fn parse_and_build_library_async_logger(
input : String,
) -> LibraryAsyncLogger[@bitlogger.RuntimeSink] raise {
build_library_async_logger(parse_async_logger_build_config_text(input))
}
///|
pub fn[S] LibraryAsyncLogger::with_target(
self : LibraryAsyncLogger[S],
target : String,
@@ -63,6 +69,7 @@ pub fn[S] LibraryAsyncLogger::with_target(
library_async_logger(self.inner.with_target(target))
}
///|
pub fn[S] LibraryAsyncLogger::child(
self : LibraryAsyncLogger[S],
target : String,
@@ -70,6 +77,7 @@ pub fn[S] LibraryAsyncLogger::child(
library_async_logger(self.inner.child(target))
}
///|
pub fn[S] LibraryAsyncLogger::with_context_fields(
self : LibraryAsyncLogger[S],
fields : Array[@bitlogger.Field],
@@ -77,6 +85,7 @@ pub fn[S] LibraryAsyncLogger::with_context_fields(
library_async_logger(self.inner.with_context_fields(fields))
}
///|
pub fn[S] LibraryAsyncLogger::bind(
self : LibraryAsyncLogger[S],
fields : Array[@bitlogger.Field],
@@ -84,6 +93,7 @@ pub fn[S] LibraryAsyncLogger::bind(
self.with_context_fields(fields)
}
///|
pub fn[S] LibraryAsyncLogger::is_enabled(
self : LibraryAsyncLogger[S],
level : @bitlogger.Level,
@@ -91,47 +101,55 @@ pub fn[S] LibraryAsyncLogger::is_enabled(
self.inner.is_enabled(level)
}
///|
pub async fn[S] LibraryAsyncLogger::log(
self : LibraryAsyncLogger[S],
level : @bitlogger.Level,
message : String,
fields~ : Array[@bitlogger.Field] = [],
fields? : Array[@bitlogger.Field] = [],
target? : String = "",
) -> Unit {
self.inner.log(level, message, fields=fields, target=target)
self.inner.log(level, message, fields~, target~)
}
///|
pub async fn[S] LibraryAsyncLogger::info(
self : LibraryAsyncLogger[S],
message : String,
fields~ : Array[@bitlogger.Field] = [],
fields? : Array[@bitlogger.Field] = [],
) -> Unit {
self.inner.info(message, fields=fields)
self.inner.info(message, fields~)
}
///|
pub async fn[S] LibraryAsyncLogger::warn(
self : LibraryAsyncLogger[S],
message : String,
fields~ : Array[@bitlogger.Field] = [],
fields? : Array[@bitlogger.Field] = [],
) -> Unit {
self.inner.warn(message, fields=fields)
self.inner.warn(message, fields~)
}
///|
pub async fn[S] LibraryAsyncLogger::error(
self : LibraryAsyncLogger[S],
message : String,
fields~ : Array[@bitlogger.Field] = [],
fields? : Array[@bitlogger.Field] = [],
) -> Unit {
self.inner.error(message, fields=fields)
self.inner.error(message, fields~)
}
pub async fn[S : @bitlogger.Sink] LibraryAsyncLogger::run(self : LibraryAsyncLogger[S]) -> Unit {
///|
pub async fn[S : @bitlogger.Sink] LibraryAsyncLogger::run(
self : LibraryAsyncLogger[S],
) -> Unit {
self.inner.run()
}
///|
pub async fn[S] LibraryAsyncLogger::shutdown(
self : LibraryAsyncLogger[S],
clear? : Bool = false,
) -> Unit {
self.inner.shutdown(clear=clear)
self.inner.shutdown(clear~)
}
+12 -6
View File
@@ -1,17 +1,23 @@
import {
"Nanaloveyuki/BitLogger/src" @bitlogger,
"Nanaloveyuki/BitLogger/src-async/utils" @utils,
"maria/json_parser" @json_parser,
"moonbitlang/async" @async,
"moonbitlang/async/aqueue" @aqueue,
"moonbitlang/core/env" @env,
"Nanaloveyuki/BitLogger/src-async/utils",
"maria/json_parser",
"moonbitlang/async",
"moonbitlang/async/aqueue",
"moonbitlang/core/env",
"moonbitlang/core/ref",
}
options(
targets: {
"async_logger_shared.mbt": [ "native", "llvm", "js", "wasm", "wasm-gc" ],
"application_async_logger.mbt": [ "native", "llvm", "js", "wasm", "wasm-gc" ],
"application_async_logger.mbt": [
"native",
"llvm",
"js",
"wasm",
"wasm-gc",
],
"library_async_logger.mbt": [ "native", "llvm", "js", "wasm", "wasm-gc" ],
"async_logger_native.mbt": [ "native", "llvm" ],
"async_logger_stub.mbt": [ "js", "wasm", "wasm-gc" ],
+137 -61
View File
@@ -1,20 +1,24 @@
///|
pub(all) enum AsyncOverflowPolicy {
Blocking
DropOldest
DropNewest
}
///|
pub(all) enum AsyncFlushPolicy {
Never
Batch
Shutdown
}
///|
pub enum AsyncRuntimeMode {
NativeWorker
Compatibility
}
///|
pub fn async_runtime_mode_label(mode : AsyncRuntimeMode) -> String {
match mode {
AsyncRuntimeMode::NativeWorker => "native_worker"
@@ -22,19 +26,23 @@ pub fn async_runtime_mode_label(mode : AsyncRuntimeMode) -> String {
}
}
///|
pub fn native_worker_async_runtime_mode() -> AsyncRuntimeMode {
AsyncRuntimeMode::NativeWorker
}
///|
pub fn compatibility_async_runtime_mode() -> AsyncRuntimeMode {
AsyncRuntimeMode::Compatibility
}
///|
pub struct AsyncRuntimeState {
mode : AsyncRuntimeMode
background_worker : Bool
}
///|
pub fn AsyncRuntimeState::new(
mode : AsyncRuntimeMode,
background_worker : Bool,
@@ -42,6 +50,7 @@ pub fn AsyncRuntimeState::new(
{ mode, background_worker }
}
///|
pub struct AsyncLoggerState {
runtime : AsyncRuntimeState
pending_count : Int
@@ -53,6 +62,7 @@ pub struct AsyncLoggerState {
flush_policy : AsyncFlushPolicy
}
///|
pub fn AsyncLoggerState::new(
runtime : AsyncRuntimeState,
pending_count : Int,
@@ -75,16 +85,20 @@ pub fn AsyncLoggerState::new(
}
}
pub fn async_runtime_state_to_json(state : AsyncRuntimeState) -> @json_parser.JsonValue {
///|
pub fn async_runtime_state_to_json(
state : AsyncRuntimeState,
) -> @json_parser.JsonValue {
@json_parser.JsonValue::Object({
"mode": @json_parser.JsonValue::String(async_runtime_mode_label(state.mode)),
"background_worker": @json_parser.JsonValue::Bool(state.background_worker),
})
}
///|
pub fn stringify_async_runtime_state(
state : AsyncRuntimeState,
pretty~ : Bool = false,
pretty? : Bool = false,
) -> String {
let value = async_runtime_state_to_json(state)
if pretty {
@@ -94,6 +108,7 @@ pub fn stringify_async_runtime_state(
}
}
///|
fn async_flush_policy_label(policy : AsyncFlushPolicy) -> String {
match policy {
AsyncFlushPolicy::Never => "Never"
@@ -102,26 +117,39 @@ fn async_flush_policy_label(policy : AsyncFlushPolicy) -> String {
}
}
fn async_logger_state_to_json_value(state : AsyncLoggerState) -> @json_parser.JsonValue {
///|
fn async_logger_state_to_json_value(
state : AsyncLoggerState,
) -> @json_parser.JsonValue {
@json_parser.JsonValue::Object({
"runtime": async_runtime_state_to_json(state.runtime),
"pending_count": @json_parser.JsonValue::Number(state.pending_count.to_double()),
"dropped_count": @json_parser.JsonValue::Number(state.dropped_count.to_double()),
"pending_count": @json_parser.JsonValue::Number(
state.pending_count.to_double(),
),
"dropped_count": @json_parser.JsonValue::Number(
state.dropped_count.to_double(),
),
"is_closed": @json_parser.JsonValue::Bool(state.is_closed),
"is_running": @json_parser.JsonValue::Bool(state.is_running),
"has_failed": @json_parser.JsonValue::Bool(state.has_failed),
"last_error": @json_parser.JsonValue::String(state.last_error),
"flush_policy": @json_parser.JsonValue::String(async_flush_policy_label(state.flush_policy)),
"flush_policy": @json_parser.JsonValue::String(
async_flush_policy_label(state.flush_policy),
),
})
}
pub fn async_logger_state_to_json(state : AsyncLoggerState) -> @json_parser.JsonValue {
///|
pub fn async_logger_state_to_json(
state : AsyncLoggerState,
) -> @json_parser.JsonValue {
async_logger_state_to_json_value(state)
}
///|
pub fn stringify_async_logger_state(
state : AsyncLoggerState,
pretty~ : Bool = false,
pretty? : Bool = false,
) -> String {
let value = async_logger_state_to_json_value(state)
if pretty {
@@ -131,6 +159,7 @@ pub fn stringify_async_logger_state(
}
}
///|
pub struct AsyncLoggerConfig {
max_pending : Int
overflow : AsyncOverflowPolicy
@@ -139,22 +168,32 @@ pub struct AsyncLoggerConfig {
flush : AsyncFlushPolicy
}
///|
pub fn AsyncLoggerConfig::new(
max_pending~ : Int = 0,
overflow~ : AsyncOverflowPolicy = AsyncOverflowPolicy::Blocking,
max_batch~ : Int = 1,
linger_ms~ : Int = 0,
flush~ : AsyncFlushPolicy = AsyncFlushPolicy::Never,
max_pending? : Int = 0,
overflow? : AsyncOverflowPolicy = AsyncOverflowPolicy::Blocking,
max_batch? : Int = 1,
linger_ms? : Int = 0,
flush? : AsyncFlushPolicy = AsyncFlushPolicy::Never,
) -> AsyncLoggerConfig {
{
max_pending,
overflow,
max_batch: if max_batch <= 1 { 1 } else { max_batch },
linger_ms: if linger_ms < 0 { 0 } else { linger_ms },
max_batch: if max_batch <= 1 {
1
} else {
max_batch
},
linger_ms: if linger_ms < 0 {
0
} else {
linger_ms
},
flush,
}
}
///|
fn parse_async_overflow(name : String) -> AsyncOverflowPolicy raise {
match name.to_upper() {
"BLOCKING" => AsyncOverflowPolicy::Blocking
@@ -165,6 +204,7 @@ fn parse_async_overflow(name : String) -> AsyncOverflowPolicy raise {
}
}
///|
fn parse_async_flush(name : String) -> AsyncFlushPolicy raise {
match name.to_upper() {
"NEVER" => AsyncFlushPolicy::Never
@@ -175,75 +215,100 @@ fn parse_async_flush(name : String) -> AsyncFlushPolicy raise {
}
}
pub fn parse_async_logger_config_text(input : String) -> AsyncLoggerConfig raise {
///|
pub fn parse_async_logger_config_text(
input : String,
) -> AsyncLoggerConfig raise {
let root = @json_parser.parse(input)
let obj = match root.as_object() {
Some(obj) => obj
None => raise Failure::Failure("Expected object for async logger config")
}
let max_pending = match obj.get("max_pending") {
Some(value) => match value.as_number() {
Some(number) => number.to_int()
None => raise Failure::Failure("Expected number at async_config.max_pending")
}
Some(value) =>
match value.as_number() {
Some(number) => number.to_int()
None =>
raise Failure::Failure("Expected number at async_config.max_pending")
}
None => 0
}
let overflow = match obj.get("overflow") {
Some(value) => match value.as_string() {
Some(text) => parse_async_overflow(text)
None => raise Failure::Failure("Expected string at async_config.overflow")
}
Some(value) =>
match value.as_string() {
Some(text) => parse_async_overflow(text)
None =>
raise Failure::Failure("Expected string at async_config.overflow")
}
None => AsyncOverflowPolicy::Blocking
}
let max_batch = match obj.get("max_batch") {
Some(value) => match value.as_number() {
Some(number) => number.to_int()
None => raise Failure::Failure("Expected number at async_config.max_batch")
}
Some(value) =>
match value.as_number() {
Some(number) => number.to_int()
None =>
raise Failure::Failure("Expected number at async_config.max_batch")
}
None => 1
}
let linger_ms = match obj.get("linger_ms") {
Some(value) => match value.as_number() {
Some(number) => number.to_int()
None => raise Failure::Failure("Expected number at async_config.linger_ms")
}
Some(value) =>
match value.as_number() {
Some(number) => number.to_int()
None =>
raise Failure::Failure("Expected number at async_config.linger_ms")
}
None => 0
}
let flush = match obj.get("flush") {
Some(value) => match value.as_string() {
Some(text) => parse_async_flush(text)
None => raise Failure::Failure("Expected string at async_config.flush")
}
Some(value) =>
match value.as_string() {
Some(text) => parse_async_flush(text)
None => raise Failure::Failure("Expected string at async_config.flush")
}
None => AsyncFlushPolicy::Never
}
AsyncLoggerConfig::new(
max_pending=max_pending,
overflow=overflow,
max_batch=max_batch,
linger_ms=linger_ms,
flush=flush,
max_pending~,
overflow~,
max_batch~,
linger_ms~,
flush~,
)
}
pub fn async_logger_config_to_json(config : AsyncLoggerConfig) -> @json_parser.JsonValue {
///|
pub fn async_logger_config_to_json(
config : AsyncLoggerConfig,
) -> @json_parser.JsonValue {
@json_parser.JsonValue::Object({
"max_pending": @json_parser.JsonValue::Number(config.max_pending.to_double()),
"max_pending": @json_parser.JsonValue::Number(
config.max_pending.to_double(),
),
"max_batch": @json_parser.JsonValue::Number(config.max_batch.to_double()),
"linger_ms": @json_parser.JsonValue::Number(config.linger_ms.to_double()),
"overflow": @json_parser.JsonValue::String(match config.overflow {
AsyncOverflowPolicy::Blocking => "Blocking"
AsyncOverflowPolicy::DropOldest => "DropOldest"
AsyncOverflowPolicy::DropNewest => "DropNewest"
}),
"flush": @json_parser.JsonValue::String(match config.flush {
AsyncFlushPolicy::Never => "Never"
AsyncFlushPolicy::Batch => "Batch"
AsyncFlushPolicy::Shutdown => "Shutdown"
}),
"overflow": @json_parser.JsonValue::String(
match config.overflow {
AsyncOverflowPolicy::Blocking => "Blocking"
AsyncOverflowPolicy::DropOldest => "DropOldest"
AsyncOverflowPolicy::DropNewest => "DropNewest"
},
),
"flush": @json_parser.JsonValue::String(
match config.flush {
AsyncFlushPolicy::Never => "Never"
AsyncFlushPolicy::Batch => "Batch"
AsyncFlushPolicy::Shutdown => "Shutdown"
},
),
})
}
pub fn stringify_async_logger_config(config : AsyncLoggerConfig, pretty~ : Bool = false) -> String {
///|
pub fn stringify_async_logger_config(
config : AsyncLoggerConfig,
pretty? : Bool = false,
) -> String {
let value = async_logger_config_to_json(config)
if pretty {
@json_parser.stringify_pretty(value, 2)
@@ -252,35 +317,45 @@ pub fn stringify_async_logger_config(config : AsyncLoggerConfig, pretty~ : Bool
}
}
///|
pub struct AsyncLoggerBuildConfig {
logger : @bitlogger.LoggerConfig
async_config : AsyncLoggerConfig
}
///|
pub fn AsyncLoggerBuildConfig::new(
logger~ : @bitlogger.LoggerConfig = @bitlogger.default_logger_config(),
async_config~ : AsyncLoggerConfig = AsyncLoggerConfig::new(),
logger? : @bitlogger.LoggerConfig = @bitlogger.default_logger_config(),
async_config? : AsyncLoggerConfig = AsyncLoggerConfig::new(),
) -> AsyncLoggerBuildConfig {
{ logger, async_config }
}
pub fn parse_async_logger_build_config_text(input : String) -> AsyncLoggerBuildConfig raise {
///|
pub fn parse_async_logger_build_config_text(
input : String,
) -> AsyncLoggerBuildConfig raise {
let root = @json_parser.parse(input)
let obj = match root.as_object() {
Some(obj) => obj
None => raise Failure::Failure("Expected object at async logger build config root")
None =>
raise Failure::Failure(
"Expected object at async logger build config root",
)
}
let logger = match obj.get("logger") {
Some(value) => @bitlogger.parse_logger_config_text(@json_parser.stringify(value))
Some(value) =>
@bitlogger.parse_logger_config_text(@json_parser.stringify(value))
None => @bitlogger.default_logger_config()
}
let async_config = match obj.get("async_config") {
Some(value) => parse_async_logger_config_text(@json_parser.stringify(value))
None => AsyncLoggerConfig::new()
}
AsyncLoggerBuildConfig::new(logger=logger, async_config=async_config)
AsyncLoggerBuildConfig::new(logger~, async_config~)
}
///|
pub fn async_logger_build_config_to_json(
config : AsyncLoggerBuildConfig,
) -> @json_parser.JsonValue {
@@ -290,9 +365,10 @@ pub fn async_logger_build_config_to_json(
})
}
///|
pub fn stringify_async_logger_build_config(
config : AsyncLoggerBuildConfig,
pretty~ : Bool = false,
pretty? : Bool = false,
) -> String {
let value = async_logger_build_config_to_json(config)
if pretty {
+1 -1
View File
@@ -1,4 +1,4 @@
import {
"Nanaloveyuki/BitLogger/src" @bitlogger,
"maria/json_parser" @json_parser,
"maria/json_parser",
}