Skip to content

Commit 88023b6

Browse files
committed
feat:v1.1.0
1 parent bfba3c8 commit 88023b6

File tree

8 files changed

+65
-35
lines changed

8 files changed

+65
-35
lines changed

Cargo.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hyperlane-log"
3-
version = "1.0.0"
3+
version = "1.1.0"
44
edition = "2021"
55
authors = ["ltpp-universe <root@ltpp.vip>"]
66
license = "MIT"
@@ -17,7 +17,7 @@ exclude = [
1717

1818
[dependencies]
1919
file-operation = "0.3.0"
20-
http-type = "2.27.1"
20+
http-type = "2.28.0"
2121
hyperlane-time = "0.0.3"
2222
lombok-macros = "1.7.0"
2323
once_cell = "1.20.2"

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ cargo add hyperlane-log
2727

2828
```rust
2929
use hyperlane_log::*;
30-
let log: Log = Log::new("./logs", 1_024_000);
30+
let log: Log = Log::new("./logs", 1_024_000, 360);
3131
let log_thread: JoinHandle<()> = log_run(&log);
3232
log.error("error data!", |error| {
3333
let write_data: String = format!("User error func => {:?}\n", error);

src/cfg.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ fn test() {
33
use crate::*;
44
use std::thread;
55
use std::time::Duration;
6-
let log: Log = Log::new("./logs", 1_024_000);
6+
let log: Log = Log::new("./logs", 1_024_000, 360);
77
let _log_thread: JoinHandle<()> = log_run(&log);
88
log.error("error data => ", |error| {
99
let write_data: String = format!("User error func => {:?}\n", error);
@@ -25,7 +25,7 @@ fn test_more_log_first() {
2525
use crate::*;
2626
use std::thread;
2727
use std::time::Duration;
28-
let log: Log = Log::new("./logs", 0);
28+
let log: Log = Log::new("./logs", 0, 360);
2929
let _log_thread: JoinHandle<()> = log_run(&log);
3030
log.error("error data => ", |error| {
3131
let write_data: String = format!("User error func => {:?}\n", error);
@@ -48,7 +48,7 @@ fn test_more_log_second() {
4848
use std::thread;
4949
use std::time::Duration;
5050
thread::sleep(Duration::new(4, 0));
51-
let log: Log = Log::new("./logs", 1_024);
51+
let log: Log = Log::new("./logs", 1_024, 360);
5252
let _log_thread: JoinHandle<()> = log_run(&log);
5353
let times: i32 = 1000;
5454
for _ in 0..times {

src/log/constant.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pub static DEBUG_DIR: &str = "debug";
55
pub static LOG_EXTION: &str = "log";
66
pub static DEFAULT_LOG_FILE_START_IDX: usize = 1;
77
pub static DEFAULT_LOG_FILE_SIZE: usize = 1_024_000_000;
8+
pub static DEFAULT_LOG_INTERVAL_MILLIS: usize = 1;

src/log/impl.rs

+31-17
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ use super::{
44
r#type::{Log, LogListArcLock},
55
utils::*,
66
};
7-
use crate::BoxLogFunc;
7+
use crate::{ArcLogFunc, ListLog};
88
use file_operation::*;
99
use http_type::*;
1010
use hyperlane_time::*;
1111
use once_cell::sync::Lazy;
12-
use std::sync::{Arc, RwLock};
12+
use std::{
13+
sync::{Arc, RwLock},
14+
vec,
15+
};
1316

1417
static LOG_ERROR_QUEUE: Lazy<LogListArcLock> = Lazy::new(|| Arc::new(RwLock::new(Vec::new())));
1518
static LOG_INFO_QUEUE: Lazy<LogListArcLock> = Lazy::new(|| Arc::new(RwLock::new(Vec::new())));
@@ -21,24 +24,26 @@ impl Default for Log {
2124
Self {
2225
path: DEFAULT_LOG_DIR.to_owned(),
2326
file_size: DEFAULT_LOG_FILE_SIZE,
27+
interval_millis: DEFAULT_LOG_INTERVAL_MILLIS,
2428
}
2529
}
2630
}
2731

2832
impl Log {
2933
#[inline]
30-
pub fn new<T>(path: T, file_size: usize) -> Self
34+
pub fn new<T>(path: T, file_size: usize, interval_millis: usize) -> Self
3135
where
3236
T: Into<String>,
3337
{
3438
Self {
3539
path: path.into(),
3640
file_size,
41+
interval_millis,
3742
}
3843
}
3944

4045
#[inline]
41-
fn write(list: &mut Vec<(String, BoxLogFunc)>, path: &str) {
46+
fn write(list: &mut Vec<(String, ArcLogFunc)>, path: &str) {
4247
for (log_string, func) in list.iter() {
4348
let out: String = func(log_string);
4449
let _ = write_to_file(path, &out.as_bytes());
@@ -54,7 +59,7 @@ impl Log {
5459
let data_string: String = data.into();
5560
{
5661
if let Ok(mut queue) = log_queue.write() {
57-
queue.push((data_string, Box::new(func)));
62+
queue.push((data_string, Arc::new(func)));
5863
}
5964
}
6065
}
@@ -103,26 +108,35 @@ impl Log {
103108

104109
#[inline]
105110
pub(super) fn write_error(&self) {
106-
if let Ok(mut error) = LOG_ERROR_QUEUE.write() {
107-
Self::write(&mut *error, &self.get_log_path(ERROR_DIR));
108-
error.clear();
109-
}
111+
let mut list: ListLog = if let Ok(mut error) = LOG_ERROR_QUEUE.write() {
112+
let tmp_error: ListLog = error.drain(..).collect::<Vec<_>>();
113+
tmp_error
114+
} else {
115+
vec![]
116+
};
117+
Self::write(&mut list, &self.get_log_path(ERROR_DIR));
110118
}
111119

112120
#[inline]
113121
pub(super) fn write_info(&self) {
114-
if let Ok(mut info) = LOG_INFO_QUEUE.write() {
115-
Self::write(&mut *info, &self.get_log_path(INFO_DIR));
116-
info.clear();
117-
}
122+
let mut list: ListLog = if let Ok(mut info) = LOG_INFO_QUEUE.write() {
123+
let tmp_info: ListLog = info.drain(..).collect::<Vec<_>>();
124+
tmp_info
125+
} else {
126+
vec![]
127+
};
128+
Self::write(&mut list, &self.get_log_path(INFO_DIR));
118129
}
119130

120131
#[inline]
121132
pub(super) fn write_debug(&self) {
122-
if let Ok(mut debug) = LOG_DEBUG_QUEUE.write() {
123-
Self::write(&mut *debug, &self.get_log_path(DEBUG_DIR));
124-
debug.clear();
125-
}
133+
let mut list: ListLog = if let Ok(mut debug) = LOG_DEBUG_QUEUE.write() {
134+
let tmp_debug: ListLog = debug.drain(..).collect::<Vec<_>>();
135+
tmp_debug
136+
} else {
137+
vec![]
138+
};
139+
Self::write(&mut list, &self.get_log_path(DEBUG_DIR));
126140
}
127141

128142
#[inline]

src/log/thread.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::Log;
22
use recoverable_spawn::*;
3-
use std::sync::Arc;
3+
use std::{sync::Arc, thread::sleep, time::Duration};
44

55
#[inline]
66
pub fn log_run(log: &Log) -> JoinHandle<()> {
@@ -9,14 +9,26 @@ pub fn log_run(log: &Log) -> JoinHandle<()> {
99
let arc_error_log_clone: Arc<Log> = Arc::clone(&arc_log);
1010
let arc_info_log_clone: Arc<Log> = Arc::clone(&arc_log);
1111
let arc_debug_log_clone: Arc<Log> = Arc::clone(&arc_log);
12-
let error_log_thread: JoinHandle<()> = recoverable_spawn(move || loop {
13-
arc_error_log_clone.write_error();
12+
let error_log_thread: JoinHandle<()> = recoverable_spawn(move || {
13+
let interval_millis: u64 = *arc_error_log_clone.get_interval_millis() as u64;
14+
loop {
15+
arc_error_log_clone.write_error();
16+
sleep(Duration::from_millis(interval_millis));
17+
}
1418
});
15-
let info_log_thread: JoinHandle<()> = recoverable_spawn(move || loop {
16-
arc_info_log_clone.write_info();
19+
let info_log_thread: JoinHandle<()> = recoverable_spawn(move || {
20+
let interval_millis: u64 = *arc_info_log_clone.get_interval_millis() as u64;
21+
loop {
22+
arc_info_log_clone.write_info();
23+
sleep(Duration::from_millis(interval_millis));
24+
}
1725
});
18-
let debug_log_thread: JoinHandle<()> = recoverable_spawn(move || loop {
19-
arc_debug_log_clone.write_debug();
26+
let debug_log_thread: JoinHandle<()> = recoverable_spawn(move || {
27+
let interval_millis: u64 = *arc_debug_log_clone.get_interval_millis() as u64;
28+
loop {
29+
arc_debug_log_clone.write_debug();
30+
sleep(Duration::from_millis(interval_millis));
31+
}
2032
});
2133
let _ = error_log_thread.join();
2234
let _ = info_log_thread.join();

src/log/type.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
use super::r#trait::LogFuncTrait;
22
use http_type::*;
33
use lombok_macros::Lombok;
4+
use std::sync::Arc;
45

5-
pub type LogListArcLock = ArcRwLock<Vec<(String, BoxLogFunc)>>;
6+
pub type ListLog = Vec<(String, ArcLogFunc)>;
7+
pub type LogListArcLock = ArcRwLock<ListLog>;
68
pub type LogArcLock = ArcRwLock<Log>;
79
pub type LogFunc = dyn LogFuncTrait;
8-
pub type BoxLogFunc = Box<LogFunc>;
10+
pub type ArcLogFunc = Arc<LogFunc>;
911

1012
#[derive(Debug, Clone, Lombok)]
1113
pub struct Log {
1214
pub(super) path: String,
1315
pub(super) file_size: usize,
16+
pub(super) interval_millis: usize,
1417
}

0 commit comments

Comments
 (0)