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
use crate::logger::ErrorChannel;
use crate::{DeferredNow, FormatFunction};
use log::Record;
use std::cell::RefCell;
use std::io::Write;
use std::path::Path;
use std::sync::RwLock;
#[cfg(test)]
use std::io::Cursor;
#[cfg(test)]
use std::sync::{Arc, Mutex};
#[cfg(feature = "async")]
pub(crate) const ASYNC_FLUSH: &[u8] = b"F";
#[cfg(feature = "async")]
pub(crate) const ASYNC_SHUTDOWN: &[u8] = b"S";
#[derive(Copy, Clone, Debug)]
pub(crate) enum ERRCODE {
Write,
Flush,
Format,
LogFile,
#[cfg(feature = "external_rotation")]
LogFileWatcher,
#[cfg(feature = "specfile")]
LogSpecFile,
Poison,
#[cfg(target_family = "unix")]
Symlink,
WriterSpec,
}
impl ERRCODE {
fn as_index(self) -> &'static str {
match self {
Self::Write => "write",
Self::Flush => "flush",
Self::Format => "format",
Self::LogFile => "logfile",
#[cfg(feature = "external_rotation")]
Self::LogFileWatcher => "logfilewatcher",
#[cfg(feature = "specfile")]
Self::LogSpecFile => "logspecfile",
Self::Poison => "poison",
#[cfg(target_family = "unix")]
Self::Symlink => "symlink",
Self::WriterSpec => "writerspec",
}
}
}
pub(crate) fn eprint_err(errcode: ERRCODE, msg: &str, err: &dyn std::error::Error) {
let s = format!(
"[flexi_logger][ERRCODE::{code:?}] {msg}, caused by {err:?}\n \
See https://docs.rs/flexi_logger/latest/flexi_logger/error_info/index.html#{code_lc}",
msg = msg,
err = err,
code = errcode,
code_lc = errcode.as_index(),
);
try_to_write(&s);
}
pub(crate) fn eprint_msg(errcode: ERRCODE, msg: &str) {
let s = format!(
"[flexi_logger][ERRCODE::{code:?}] {msg}\n \
See https://docs.rs/flexi_logger/latest/flexi_logger/error_info/index.html#{code_lc}",
msg = msg,
code = errcode,
code_lc = errcode.as_index(),
);
try_to_write(&s);
}
lazy_static::lazy_static! {
pub(crate) static ref ERROR_CHANNEL: RwLock<ErrorChannel> = RwLock::new(ErrorChannel::default());
}
pub(crate) fn set_error_channel(channel: ErrorChannel) {
match ERROR_CHANNEL.write() {
Ok(mut guard) => {
*guard = channel;
}
Err(e) => {
eprint_err(ERRCODE::Poison, "Error channel cannot be set", &e);
}
}
}
fn try_to_write(s: &str) {
match &*(ERROR_CHANNEL.read().unwrap()) {
ErrorChannel::StdErr => {
eprintln!("{}", s);
}
ErrorChannel::StdOut => {
println!("{}", s);
}
ErrorChannel::File(path) => try_to_write_to_file(s, path).unwrap_or_else(|e| {
eprintln!("{}", s);
eprintln!("Can't open error output file, caused by: {}", e);
}),
ErrorChannel::DevNull => {}
}
}
fn try_to_write_to_file(s: &str, path: &Path) -> Result<(), std::io::Error> {
let mut file = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(path)?;
writeln!(file, "{}", s)?;
file.flush()
}
pub(crate) fn io_err(s: &'static str) -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::Other, s)
}
pub(crate) fn buffer_with<F>(f: F)
where
F: FnOnce(&RefCell<Vec<u8>>),
{
thread_local! {
static BUFFER: RefCell<Vec<u8>> = RefCell::new(Vec::with_capacity(200));
}
BUFFER.with(f);
}
pub(crate) fn write_buffered(
format_function: FormatFunction,
now: &mut DeferredNow,
record: &Record,
w: &mut dyn Write,
#[cfg(test)] o_validation_buffer: Option<&Arc<Mutex<Cursor<Vec<u8>>>>>,
) -> Result<(), std::io::Error> {
let mut result: Result<(), std::io::Error> = Ok(());
buffer_with(|tl_buf| match tl_buf.try_borrow_mut() {
Ok(mut buffer) => {
(format_function)(&mut *buffer, now, record)
.unwrap_or_else(|e| eprint_err(ERRCODE::Format, "formatting failed", &e));
buffer
.write_all(b"\n")
.unwrap_or_else(|e| eprint_err(ERRCODE::Write, "writing failed", &e));
result = w.write_all(&buffer).map_err(|e| {
eprint_err(ERRCODE::Write, "writing failed", &e);
e
});
#[cfg(test)]
if let Some(valbuf) = o_validation_buffer {
valbuf.lock().unwrap().write_all(&buffer).ok();
}
buffer.clear();
}
Err(_e) => {
let mut tmp_buf = Vec::<u8>::with_capacity(200);
(format_function)(&mut tmp_buf, now, record)
.unwrap_or_else(|e| eprint_err(ERRCODE::Format, "formatting failed", &e));
tmp_buf
.write_all(b"\n")
.unwrap_or_else(|e| eprint_err(ERRCODE::Write, "writing failed", &e));
result = w.write_all(&tmp_buf).map_err(|e| {
eprint_err(ERRCODE::Write, "writing failed", &e);
e
});
#[cfg(test)]
if let Some(valbuf) = o_validation_buffer {
valbuf.lock().unwrap().write_all(&tmp_buf).ok();
}
}
});
result
}