Changes to Cargo.toml.
Changes to src/lumberjack.rs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
-
+
-
+
|
use std::{
fmt, fs,
env, fmt, fs,
io::Write,
path::{Path, PathBuf},
str::FromStr
};
use time::macros::format_description;
use tracing_subscriber::{fmt::time::UtcTime, FmtSubscriber};
use tracing_subscriber::{fmt::time::UtcTime, EnvFilter};
#[cfg(feature = "clap")]
use clap::ValueEnum;
use crate::err::Error;
|
︙ | | |
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
|
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
|
-
+
-
-
+
+
-
-
+
+
-
-
-
+
+
-
-
-
-
+
|
}
/// Logging and tracing initialization.
pub struct LumberJack {
init: bool,
log_out: LogOut,
log_level: LogLevel,
trace_level: LogLevel,
trace_filter: Option<String>,
//log_file: Option<PathBuf>,
trace_file: Option<PathBuf>
}
impl Default for LumberJack {
/// Create a default log/trace initialization.
///
/// This will set the `log` log level to the value of the `LOG_LEVEL`
/// environment variable, or default to `warm` (if either not set or
/// invalid).
///
/// The `tracing` trace level will use the environment variable `TRACE_LEVEL`
/// in a similar manner, but defaults to `off`.
/// The `tracing` trace level will use the environment variable
/// `TRACE_FILTER` in a similar manner, but defaults to `none`.
///
/// If the environment variable `TRACE_FILE` is set the value will be the
/// used as the file name to write the trace logs to.
fn default() -> Self {
let log_level = if let Ok(level) = std::env::var("LOG_LEVEL") {
if let Ok(level) = level.parse::<LogLevel>() {
level
} else {
LogLevel::Warn
}
} else {
LogLevel::Warn
};
let trace_file = if let Ok(v) = std::env::var("TRACE_FILE") {
Some(PathBuf::from(v))
} else {
None
};
let trace_level = if let Ok(level) = std::env::var("TRACE_LEVEL") {
if let Ok(level) = level.parse::<LogLevel>() {
let trace_filter = if let Ok(var) = env::var("TRACE_FILTER") {
Some(var.to_string())
level
} else {
LogLevel::Off
} else {
None
}
} else {
LogLevel::Off
};
Self {
init: true,
log_out: Default::default(),
log_level,
trace_level,
trace_filter,
//log_file: None,
trace_file
}
}
}
impl LumberJack {
|
︙ | | |
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
|
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
|
-
+
+
-
+
-
-
-
-
-
-
+
+
+
+
-
-
+
+
|
pub fn from_winsvc(svcname: &str) -> Result<Self, Error> {
let params = crate::rt::winsvc::get_service_param(svcname)?;
let loglevel = params
.get_value::<String, &str>("LogLevel")
.unwrap_or(String::from("warn"))
.parse::<LogLevel>()
.unwrap_or(LogLevel::Warn);
let tracelevel = params.get_value::<String, &str>("TraceLevel");
let tracefilter = params.get_value::<String, &str>("TraceFilter");
let tracefile = params.get_value::<String, &str>("TraceFile");
let mut this = Self::new().log_level(loglevel);
this.log_out = LogOut::WinEvtLog {
svcname: svcname.to_string()
};
let this =
let this = if let (Ok(tracelevel), Ok(tracefile)) = (tracelevel, tracefile)
if let (Ok(tracefilter), Ok(tracefile)) = (tracefilter, tracefile) {
{
let tracelevel = tracelevel.parse::<LogLevel>().unwrap_or(LogLevel::Off);
this.trace_level(tracelevel).trace_file(tracefile)
} else {
this
};
this.trace_filter(tracefilter).trace_file(tracefile)
} else {
this
};
Ok(this)
}
/// Set the `log` logging level.
pub fn log_level(mut self, level: LogLevel) -> Self {
self.log_level = level;
self
}
/// Set the `tracing` log level.
pub fn trace_level(mut self, level: LogLevel) -> Self {
self.trace_level = level;
pub fn trace_filter(mut self, filter: impl ToString) -> Self {
self.trace_filter = Some(filter.to_string());
self
}
/// Set a file to which `tracing` log entries are written (rather than to
/// write to console).
pub fn trace_file<P>(mut self, fname: P) -> Self
where
|
︙ | | |
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
-
+
-
+
|
LogOut::WinEvtLog { svcname } => {
eventlog::init(&svcname, log::Level::Trace)?;
log::set_max_level(self.log_level.into());
}
}
if let Some(fname) = self.trace_file {
init_file_tracing(fname, self.trace_level);
init_file_tracing(fname, self.trace_filter.as_deref());
} else {
init_console_tracing(self.trace_level);
init_console_tracing(self.trace_filter.as_deref());
}
Ok(())
} else {
Ok(())
}
}
|
︙ | | |
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
|
.filter(None, lf)
.init();
Ok(())
}
pub fn init_console_tracing(level: LogLevel) {
pub fn init_console_tracing(filter: Option<&str>) {
let lvl: Option<tracing::Level> = level.into();
/*
let subscriber = FmtSubscriber::builder().with_max_level(lvl).finish();
tracing::subscriber::set_global_default(subscriber)
.expect("setting default subscriber failed");
*/
// When running on console, then disable tracing by default
let filter = if let Some(spec) = filter {
EnvFilter::new(spec)
} else {
EnvFilter::new("none")
};
tracing_subscriber::fmt()
.with_env_filter(filter)
//.with_timer(timer)
.init();
}
/// Optionally set up tracing to a file.
///
/// This function will attempt to set up tracing to a file. There are three
/// conditions that must be true in order for tracing to a file to be enabled:
/// - A filename must be specified (either via the `fname` argument or the
/// `TRACE_FILE` environment variable).
/// - A trace level must be specified (either via the `level` argument or the
/// `TRACE_LEVEL` environment variable).
/// - A trace filter must be specified (either via the `filter` argument or the
/// `TRACE_FILTER` environment variable).
/// - The file name must be openable for writing.
///
/// Because tracing is an optional feature and intended for development only,
/// this function will enable tracing if possible, and silently ignore and
/// errors.
pub fn init_file_tracing<P>(fname: P, level: LogLevel)
pub fn init_file_tracing<P>(fname: P, filter: Option<&str>)
where
P: AsRef<Path>
{
// If a level was set, convert it to an Option<tracing::Level>.
let lvl: Option<tracing::Level> = level.into();
//
// If both a trace file name and a trace level
//
let timer = UtcTime::new(format_description!(
"[year]-[month]-[day] [hour]:[minute]:[second]"
));
let f = if let Ok(f) =
fs::OpenOptions::new().create(true).append(true).open(fname)
{
f
} else {
return;
};
//
// If TRACING_FILE is set, then default to filter out at warn level.
// Disable all tracing if TRACE_FILTER is not set
//
let filter = if let Some(spec) = filter {
EnvFilter::new(spec)
} else {
EnvFilter::new("warn")
/*
EnvFilter::builder()
.with_default_directive(LevelFilter::OFF.into())
.parse("")
.unwrap()
*/
};
tracing_subscriber::fmt()
.with_env_filter(filter)
.with_writer(f)
.with_ansi(false)
.with_max_level(lvl)
.with_timer(timer)
.init();
//tracing_subscriber::registry().with(filter).init();
}
// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :
|
Changes to www/changelog.md.