mirror of
https://github.com/Nanaloveyuki/BitLogger.git
synced 2026-07-25 17:32:20 +00:00
✨ Add Int64 file rotation path and JSON roundtrip
This commit is contained in:
+37
-6
@@ -245,6 +245,30 @@ fn get_int(
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn get_optional_int64_string(
|
||||
obj : Map[String, @json_parser.JsonValue],
|
||||
key : String,
|
||||
) -> Int64? raise ConfigError {
|
||||
match obj.get(key) {
|
||||
None => None
|
||||
Some(value) =>
|
||||
match value.as_string() {
|
||||
Some(text) =>
|
||||
Some(
|
||||
@string.parse_int64(text) catch {
|
||||
_ =>
|
||||
raise ConfigError::InvalidConfig(
|
||||
"Expected int64 string at key " + key,
|
||||
)
|
||||
},
|
||||
)
|
||||
None =>
|
||||
raise ConfigError::InvalidConfig("Expected string at key " + key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn parse_level(name : String) -> @core.Level raise ConfigError {
|
||||
match name.to_upper() {
|
||||
@@ -403,10 +427,11 @@ fn parse_file_rotation_config(
|
||||
value : @json_parser.JsonValue,
|
||||
) -> FileRotation raise ConfigError {
|
||||
let obj = expect_object(value, "sink.rotation")
|
||||
file_rotation(
|
||||
get_int(obj, "max_bytes", default=1),
|
||||
max_backups=get_int(obj, "max_backups", default=1),
|
||||
)
|
||||
let max_backups = get_int(obj, "max_backups", default=1)
|
||||
match get_optional_int64_string(obj, "max_bytes_i64") {
|
||||
Some(max_bytes_i64) => file_rotation_i64(max_bytes_i64, max_backups~)
|
||||
None => file_rotation(get_int(obj, "max_bytes", default=1), max_backups~)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -570,12 +595,18 @@ pub fn stringify_text_formatter_config(
|
||||
pub fn file_rotation_config_to_json(
|
||||
config : FileRotation,
|
||||
) -> @json_parser.JsonValue {
|
||||
@json_parser.JsonValue::Object({
|
||||
let obj : Map[String, @json_parser.JsonValue] = {
|
||||
"max_bytes": @json_parser.JsonValue::Number(config.max_bytes.to_double()),
|
||||
"max_backups": @json_parser.JsonValue::Number(
|
||||
config.max_backups.to_double(),
|
||||
),
|
||||
})
|
||||
}
|
||||
match config.native_wide_max_bytes {
|
||||
Some(value) =>
|
||||
obj["max_bytes_i64"] = @json_parser.JsonValue::String(value.to_string())
|
||||
None => ()
|
||||
}
|
||||
@json_parser.JsonValue::Object(obj)
|
||||
}
|
||||
|
||||
///|
|
||||
|
||||
@@ -66,7 +66,7 @@ extern "C" fn file_seek_ffi(
|
||||
) -> Int = "bitlogger_file_seek"
|
||||
|
||||
///|
|
||||
extern "C" fn file_tell_ffi(handle : NativeFileHandle) -> Int = "bitlogger_file_tell"
|
||||
extern "C" fn file_tell_i64_ffi(handle : NativeFileHandle) -> Int64 = "bitlogger_file_tell_i64"
|
||||
|
||||
///|
|
||||
#borrow(from_path, to_path)
|
||||
@@ -114,11 +114,11 @@ pub fn close_file_handle_internal(handle : FileHandle) -> Bool {
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn file_size_internal(handle : FileHandle) -> Int {
|
||||
pub fn file_size_i64_internal(handle : FileHandle) -> Int64 {
|
||||
ignore(file_seek_ffi(handle.raw, 0, 2))
|
||||
let size = file_tell_ffi(handle.raw)
|
||||
if size < 0 {
|
||||
0
|
||||
let size = file_tell_i64_ffi(handle.raw)
|
||||
if size < 0L {
|
||||
0L
|
||||
} else {
|
||||
size
|
||||
}
|
||||
|
||||
@@ -35,9 +35,9 @@ pub fn close_file_handle_internal(handle : FileHandle) -> Bool {
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn file_size_internal(handle : FileHandle) -> Int {
|
||||
pub fn file_size_i64_internal(handle : FileHandle) -> Int64 {
|
||||
ignore(handle)
|
||||
0
|
||||
0L
|
||||
}
|
||||
|
||||
///|
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
"moonbitlang/core/env",
|
||||
"moonbitlang/core/json",
|
||||
"moonbitlang/core/ref",
|
||||
"moonbitlang/core/string",
|
||||
}
|
||||
|
||||
options(
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
pub struct FileRotation {
|
||||
max_bytes : Int
|
||||
max_backups : Int
|
||||
native_wide_max_bytes : Int64?
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -71,6 +72,29 @@ pub fn file_rotation(max_bytes : Int, max_backups? : Int = 1) -> FileRotation {
|
||||
} else {
|
||||
max_backups
|
||||
},
|
||||
native_wide_max_bytes: None,
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn file_rotation_i64(
|
||||
max_bytes : Int64,
|
||||
max_backups? : Int = 1,
|
||||
) -> FileRotation {
|
||||
let normalized = if max_bytes <= 0L { 1L } else { max_bytes }
|
||||
let clamped_max_bytes = if normalized > 2147483647L {
|
||||
2147483647
|
||||
} else {
|
||||
normalized.to_int()
|
||||
}
|
||||
{
|
||||
max_bytes: clamped_max_bytes,
|
||||
max_backups: if max_backups <= 0 {
|
||||
1
|
||||
} else {
|
||||
max_backups
|
||||
},
|
||||
native_wide_max_bytes: Some(normalized),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+7
-3
@@ -37,9 +37,13 @@ int32_t bitlogger_file_seek(void *handle, int32_t offset, int32_t origin) {
|
||||
return fseek((FILE *)handle, offset, origin);
|
||||
}
|
||||
|
||||
int32_t bitlogger_file_tell(void *handle) {
|
||||
long position = ftell((FILE *)handle);
|
||||
return position < 0 ? -1 : (int32_t)position;
|
||||
int64_t bitlogger_file_tell_i64(void *handle) {
|
||||
#if defined(_WIN32)
|
||||
__int64 position = _ftelli64((FILE *)handle);
|
||||
#else
|
||||
long long position = ftello((FILE *)handle);
|
||||
#endif
|
||||
return position < 0 ? -1 : (int64_t)position;
|
||||
}
|
||||
|
||||
int32_t bitlogger_file_rename(const char *from_path, const char *to_path) {
|
||||
|
||||
Reference in New Issue
Block a user