2017-01-13 07:15:06 +00:00
|
|
|
// Copyright 2016 Joe Wilm, The Alacritty Project Contributors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
//! Logging for alacritty.
|
|
|
|
//!
|
|
|
|
//! The main executable is supposed to call `initialize()` exactly once during
|
|
|
|
//! startup. All logging messages are written to stdout, given that their
|
|
|
|
//! log-level is sufficient for the level configured in `cli::Options`.
|
|
|
|
use log;
|
|
|
|
use std::sync;
|
|
|
|
use std::io;
|
|
|
|
use cli;
|
|
|
|
|
2017-01-23 16:41:34 +00:00
|
|
|
pub struct Logger<T> {
|
2018-01-26 20:20:42 +00:00
|
|
|
level: log::LevelFilter,
|
2017-01-13 07:15:06 +00:00
|
|
|
output: sync::Mutex<T>
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:41:34 +00:00
|
|
|
impl<T: Send + io::Write> Logger<T> {
|
2018-01-06 01:42:55 +00:00
|
|
|
// False positive, see: https://github.com/rust-lang-nursery/rust-clippy/issues/734
|
|
|
|
#[cfg_attr(feature = "clippy", allow(new_ret_no_self))]
|
2018-01-26 20:20:42 +00:00
|
|
|
pub fn new(output: T, level: log::LevelFilter) -> Logger<io::LineWriter<T>> {
|
2018-03-23 01:06:12 +00:00
|
|
|
log::set_max_level(level);
|
2017-01-23 16:41:34 +00:00
|
|
|
Logger {
|
2018-03-04 22:40:15 +00:00
|
|
|
level,
|
2017-01-23 16:41:34 +00:00
|
|
|
output: sync::Mutex::new(io::LineWriter::new(output))
|
|
|
|
}
|
2017-01-13 07:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:41:34 +00:00
|
|
|
impl<T: Send + io::Write> log::Log for Logger<T> {
|
2018-01-26 20:20:42 +00:00
|
|
|
fn enabled(&self, metadata: &log::Metadata) -> bool {
|
2017-01-13 07:15:06 +00:00
|
|
|
metadata.level() <= self.level
|
|
|
|
}
|
|
|
|
|
2018-01-26 20:20:42 +00:00
|
|
|
fn log(&self, record: &log::Record) {
|
2017-09-28 00:29:44 +00:00
|
|
|
if self.enabled(record.metadata()) && record.target().starts_with("alacritty") {
|
|
|
|
if let Ok(ref mut writer) = self.output.lock() {
|
|
|
|
writer.write_all(format!("{}\n", record.args()).as_ref()).expect("Error while logging!");
|
2017-01-23 16:39:16 +00:00
|
|
|
}
|
2017-01-13 07:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-26 20:20:42 +00:00
|
|
|
|
|
|
|
fn flush(&self) {}
|
2017-01-13 07:15:06 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 16:45:40 +00:00
|
|
|
pub fn initialize(options: &cli::Options) -> Result<(), log::SetLoggerError> {
|
2017-10-08 22:42:38 +00:00
|
|
|
// Use env_logger if RUST_LOG environment variable is defined. Otherwise,
|
|
|
|
// use the alacritty-only logger.
|
|
|
|
if ::std::env::var("RUST_LOG").is_ok() {
|
2018-01-26 20:20:42 +00:00
|
|
|
::env_logger::try_init()
|
2017-10-08 22:42:38 +00:00
|
|
|
} else {
|
2018-01-26 20:20:42 +00:00
|
|
|
log::set_boxed_logger(Box::new(Logger::new(io::stdout(), options.log_level)))
|
2017-10-08 22:42:38 +00:00
|
|
|
}
|
2017-01-13 07:15:06 +00:00
|
|
|
}
|