mirror of
https://github.com/soywod/himalaya.git
synced 2024-07-20 15:11:11 +00:00
fix stderr redirection (#130)
This commit is contained in:
parent
06f628c33b
commit
cb296a5d98
|
@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
- Improve config compatibility on Windows [#111](https://github.com/soywod/himalaya/pull/111)
|
- Improve config compatibility on Windows [#111](https://github.com/soywod/himalaya/pull/111)
|
||||||
- Vim table containing emoji [#122]
|
- Vim table containing emoji [#122]
|
||||||
- IDLE mode after network interruption [#123]
|
- IDLE mode after network interruption [#123]
|
||||||
|
- Output redirected to `stderr` [#130]
|
||||||
|
|
||||||
## [0.2.6] - 2021-04-17
|
## [0.2.6] - 2021-04-17
|
||||||
|
|
||||||
|
@ -207,3 +208,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
[#123]: https://github.com/soywod/himalaya/issues/123
|
[#123]: https://github.com/soywod/himalaya/issues/123
|
||||||
[#125]: https://github.com/soywod/himalaya/issues/125
|
[#125]: https://github.com/soywod/himalaya/issues/125
|
||||||
[#126]: https://github.com/soywod/himalaya/issues/126
|
[#126]: https://github.com/soywod/himalaya/issues/126
|
||||||
|
[#130]: https://github.com/soywod/himalaya/issues/130
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use clap;
|
use clap;
|
||||||
use error_chain::error_chain;
|
use error_chain::error_chain;
|
||||||
use log::{debug, error, trace};
|
use log::{debug, error, trace};
|
||||||
use std::{env, path::PathBuf};
|
use std::{env, path::PathBuf, process::exit};
|
||||||
|
|
||||||
mod comp;
|
mod comp;
|
||||||
mod config;
|
mod config;
|
||||||
|
@ -61,9 +61,7 @@ fn run() -> Result<()> {
|
||||||
|
|
||||||
let output_fmt: OutputFmt = matches.value_of("output").unwrap().into();
|
let output_fmt: OutputFmt = matches.value_of("output").unwrap().into();
|
||||||
let log_level: LogLevel = matches.value_of("log-level").unwrap().into();
|
let log_level: LogLevel = matches.value_of("log-level").unwrap().into();
|
||||||
init_logger(&output_fmt, &log_level)?;
|
init_logger(output_fmt, log_level)?;
|
||||||
debug!("output format: {}", output_fmt);
|
|
||||||
debug!("log level: {}", log_level);
|
|
||||||
|
|
||||||
// Check completion matches before the config init
|
// Check completion matches before the config init
|
||||||
if comp_matches(build_app(), &matches)? {
|
if comp_matches(build_app(), &matches)? {
|
||||||
|
@ -104,5 +102,8 @@ fn main() {
|
||||||
errs.for_each(|err| error!(" ↳ {}", err));
|
errs.for_each(|err| error!(" ↳ {}", err));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
exit(1);
|
||||||
|
} else {
|
||||||
|
exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
use chrono::Local;
|
use chrono::Local;
|
||||||
use env_logger;
|
use env_logger;
|
||||||
use error_chain::error_chain;
|
use error_chain::error_chain;
|
||||||
use log::{self, Level, LevelFilter, Metadata, Record};
|
use log::{self, debug, Level, LevelFilter};
|
||||||
use std::{fmt, io::Write};
|
use std::{fmt, io, io::Write, ops::Deref};
|
||||||
|
|
||||||
use super::fmt::{set_output_fmt, OutputFmt};
|
use super::fmt::{set_output_fmt, OutputFmt};
|
||||||
|
|
||||||
error_chain! {}
|
error_chain! {}
|
||||||
|
|
||||||
// Log level struct
|
// Log level wrapper
|
||||||
|
|
||||||
pub struct LogLevel(pub LevelFilter);
|
pub struct LogLevel(pub LevelFilter);
|
||||||
|
|
||||||
|
@ -26,83 +26,58 @@ impl From<&str> for LogLevel {
|
||||||
|
|
||||||
impl fmt::Display for LogLevel {
|
impl fmt::Display for LogLevel {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
write!(f, "{}", self.0.to_string())
|
write!(f, "{}", self.deref())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plain logger
|
impl Deref for LogLevel {
|
||||||
|
type Target = LevelFilter;
|
||||||
|
|
||||||
pub struct PlainLogger;
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
impl log::Log for PlainLogger {
|
|
||||||
fn enabled(&self, metadata: &Metadata) -> bool {
|
|
||||||
metadata.level() <= Level::Trace
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn log(&self, record: &Record) {
|
|
||||||
if self.enabled(record.metadata()) {
|
|
||||||
if let Level::Error = record.level() {
|
|
||||||
eprintln!("{}", record.args());
|
|
||||||
} else {
|
|
||||||
println!("{}", record.args());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn flush(&self) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// JSON logger
|
|
||||||
|
|
||||||
pub struct JsonLogger;
|
|
||||||
|
|
||||||
impl log::Log for JsonLogger {
|
|
||||||
fn enabled(&self, metadata: &Metadata) -> bool {
|
|
||||||
[Level::Error, Level::Info].contains(&metadata.level())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn log(&self, record: &Record) {
|
|
||||||
if self.enabled(record.metadata()) {
|
|
||||||
if let Level::Error = record.level() {
|
|
||||||
eprintln!("{}", record.args());
|
|
||||||
} else {
|
|
||||||
print!("{}", record.args());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn flush(&self) {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init
|
// Init
|
||||||
|
|
||||||
pub fn init(fmt: &OutputFmt, level: &LogLevel) -> Result<()> {
|
pub fn init(fmt: OutputFmt, filter: LogLevel) -> Result<()> {
|
||||||
|
let level_filter = filter.deref();
|
||||||
|
let level = level_filter.to_level();
|
||||||
|
|
||||||
match fmt {
|
match fmt {
|
||||||
&OutputFmt::Plain => {
|
OutputFmt::Plain => {
|
||||||
set_output_fmt(&OutputFmt::Plain);
|
set_output_fmt(&OutputFmt::Plain);
|
||||||
}
|
}
|
||||||
&OutputFmt::Json => {
|
OutputFmt::Json => {
|
||||||
set_output_fmt(&OutputFmt::Json);
|
set_output_fmt(&OutputFmt::Json);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
env_logger::Builder::new()
|
env_logger::Builder::new()
|
||||||
.format(|buf, record| {
|
.target(env_logger::Target::Stdout)
|
||||||
if let log::Level::Info = record.metadata().level() {
|
.format(move |buf, record| match level {
|
||||||
write!(buf, "{}", record.args())
|
None => Ok(()),
|
||||||
} else {
|
Some(Level::Info) => match record.metadata().level() {
|
||||||
|
Level::Info => write!(buf, "{}", record.args()),
|
||||||
|
Level::Error => writeln!(&mut io::stderr(), "{}", record.args()),
|
||||||
|
_ => writeln!(buf, "{}", record.args()),
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
writeln!(
|
writeln!(
|
||||||
buf,
|
buf,
|
||||||
"[{} {:5} {}] {}",
|
"[{} {:5} {}] {}",
|
||||||
Local::now().format("%Y-%m-%dT%H:%M:%S"),
|
Local::now().format("%Y-%m-%dT%H:%M:%S"),
|
||||||
record.metadata().level(),
|
record.metadata().level(),
|
||||||
record.module_path().unwrap_or_default(),
|
record.module_path().unwrap_or_default(),
|
||||||
record.args()
|
record.args(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.filter_level(level.0)
|
.filter_level(*level_filter)
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
|
debug!("output format: {}", fmt);
|
||||||
|
debug!("log level: {}", filter);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue