/
threads.rs
185 lines (174 loc) · 6.73 KB
/
threads.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use {
crate::{primary_writer::PrimaryWriter, writers::LogWriter, FlexiLoggerError},
std::{
collections::HashMap,
sync::{
mpsc::{channel, Receiver, Sender},
Arc,
},
thread::Builder as ThreadBuilder,
},
};
#[cfg(any(feature = "specfile", feature = "async"))]
use crate::util::{eprint_err, ERRCODE};
#[cfg(feature = "async")]
use {
crate::{
primary_writer::std_stream::StdStream,
util::{ASYNC_FLUSH, ASYNC_SHUTDOWN},
},
crossbeam_channel::Receiver as CrossbeamReceiver,
crossbeam_queue::ArrayQueue,
std::{sync::Mutex, thread::JoinHandle},
};
// no clue why we get a warning if this allow is omitted; if we omit the use, we get an error
#[allow(unused_imports)]
#[cfg(feature = "async")]
use std::io::Write;
#[cfg(feature = "specfile")]
use {
crate::{
logger::log_spec_string_from_file, logger_handle::LogSpecSubscriber, LogSpecification,
},
notify::{watcher, DebouncedEvent, RecursiveMode, Watcher},
std::path::Path,
};
#[cfg(feature = "async")]
const ASYNC_STD_WRITER: &str = "flexi_logger-async_std_writer";
const FLUSHER: &str = "flexi_logger-flusher";
#[cfg(feature = "specfile")]
const SPECFILE_WATCHER: &str = "flexi_logger-specfile-watcher";
// Used in Logger
pub(crate) fn start_flusher_thread(
primary_writer: Arc<PrimaryWriter>,
other_writers: Arc<HashMap<String, Box<dyn LogWriter>>>,
flush_interval: std::time::Duration,
) -> Result<(), FlexiLoggerError> {
let builder = ThreadBuilder::new().name(FLUSHER.to_string());
#[cfg(not(feature = "dont_minimize_extra_stacks"))]
let builder = builder.stack_size(128);
builder.spawn(move || {
let (_sender, receiver): (Sender<()>, Receiver<()>) = channel();
loop {
receiver.recv_timeout(flush_interval).ok();
primary_writer.flush().ok();
for w in other_writers.values() {
w.flush().ok();
}
}
})?;
Ok(())
}
// Used in Logger
// Reread the specfile when it was updated
#[cfg(feature = "specfile")]
pub(crate) fn start_specfile_watcher_thread<S: LogSpecSubscriber>(
specfile: &Path,
mut subscriber: S,
) -> Result<(), FlexiLoggerError> {
// Now that the file exists, we can canonicalize the path
let specfile = specfile
.canonicalize()
.map_err(FlexiLoggerError::SpecfileIo)?;
// Watch the parent folder of the specfile, using debounced events
let (tx, rx) = std::sync::mpsc::channel();
let debouncing_delay = std::time::Duration::from_millis(1000);
let mut watcher = watcher(tx, debouncing_delay)?;
watcher.watch(
specfile.parent().unwrap(/*cannot fail*/),
RecursiveMode::NonRecursive,
)?;
// in a separate thread, reread the specfile when it was updated
let builder = ThreadBuilder::new().name(SPECFILE_WATCHER.to_string());
#[cfg(not(feature = "dont_minimize_extra_stacks"))]
let builder = builder.stack_size(128 * 1024);
builder.spawn(move || {
let _anchor_for_watcher = watcher; // keep it alive!
loop {
match rx.recv() {
Ok(debounced_event) => match debounced_event {
DebouncedEvent::Create(ref path) | DebouncedEvent::Write(ref path) => {
if path.canonicalize().map(|x| x == specfile).unwrap_or(false) {
log_spec_string_from_file(&specfile)
.map_err(FlexiLoggerError::SpecfileIo)
.and_then(|s| LogSpecification::from_toml(&s))
.and_then(|spec| subscriber.set_new_spec(spec))
.map_err(|e| {
eprint_err(
ERRCODE::LogSpecFile,
"continuing with previous log specification, because \
rereading the log specification file failed",
&e,
);
})
.ok();
}
}
_event => {}
},
Err(e) => {
eprint_err(
ERRCODE::LogSpecFile,
"error while watching the specfile",
&e,
);
}
}
}
})?;
Ok(())
}
#[cfg(feature = "async")]
pub(crate) fn start_async_stdwriter(
mut std_stream: StdStream,
receiver: CrossbeamReceiver<std::vec::Vec<u8>>,
t_pool: Arc<ArrayQueue<Vec<u8>>>,
msg_capa: usize,
#[cfg(test)] t_validation_buffer: Arc<Mutex<std::io::Cursor<Vec<u8>>>>,
) -> Mutex<Option<JoinHandle<()>>> {
Mutex::new(Some(
ThreadBuilder::new()
.name(
ASYNC_STD_WRITER.to_string()
)
.spawn(move || {
loop {
match receiver.recv() {
Err(_) => break,
Ok(mut message) => {
match message.as_ref() {
ASYNC_FLUSH => {
std_stream
.deref_mut()
.flush()
.unwrap_or_else(
|e| eprint_err(ERRCODE::Flush, "flushing failed", &e)
);
}
ASYNC_SHUTDOWN => {
break;
}
_ => {
std_stream
.deref_mut()
.write_all(&message)
.unwrap_or_else(
|e| eprint_err(ERRCODE::Write,"writing failed", &e)
);
#[cfg(test)]
if let Ok(mut guard) = t_validation_buffer.lock() {
(*guard).write_all(&message).ok();
}
}
}
if message.capacity() <= msg_capa {
message.clear();
t_pool.push(message).ok();
}
}
}
}
})
.unwrap(/* yes, let's panic if the thread can't be spawned */),
))
}