aboutsummaryrefslogtreecommitdiff
path: root/src/logging.rs
diff options
context:
space:
mode:
authorLukas Lueg <lukas.lueg@gmail.com>2017-01-13 08:15:06 +0100
committerJoe Wilm <jwilm@users.noreply.github.com>2017-01-23 09:14:01 -0800
commit64b42cd2f3f1da1d6a99dcdf90e6f7728ddca968 (patch)
tree4856f4884144e1cbba8a0fc8185b446193515430 /src/logging.rs
parentb23ed6ed4c53a6d010238643c443da6db3931a87 (diff)
downloadalacritty-64b42cd2f3f1da1d6a99dcdf90e6f7728ddca968.tar.gz
alacritty-64b42cd2f3f1da1d6a99dcdf90e6f7728ddca968.zip
Use the log-crate instead of printing to stdout
Diffstat (limited to 'src/logging.rs')
-rw-r--r--src/logging.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/logging.rs b/src/logging.rs
new file mode 100644
index 00000000..e4ebf13f
--- /dev/null
+++ b/src/logging.rs
@@ -0,0 +1,64 @@
+// 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;
+
+pub struct Logger<T:Send+io::Write> {
+ level: log::LogLevelFilter,
+ output: sync::Mutex<T>
+}
+
+impl<T> Logger<T> where T:Send+io::Write {
+ pub fn new(output: T, level: log::LogLevelFilter) -> Logger<io::LineWriter<T>> {
+ Logger { level: level,
+ output: sync::Mutex::new(io::LineWriter::new(output)) }
+ }
+}
+
+
+impl<T> log::Log for Logger<T> where T:Send+io::Write {
+ fn enabled(&self, metadata: &log::LogMetadata) -> bool {
+ metadata.level() <= self.level
+ }
+
+ fn log(&self, record: &log::LogRecord) {
+ if !self.enabled(record.metadata()) {
+ return
+ }
+ let writer = &mut self.output.lock().unwrap();
+ if self.level < log::LogLevelFilter::Debug {
+ writer.write(format!("{}\n", record.args()).as_ref()).unwrap();
+ } else {
+ writer.write(format!("{}: {}\n",
+ record.target(),
+ record.args())
+ .as_ref()).unwrap();
+ }
+ }
+}
+
+pub fn initialize(options: &cli::Options) {
+ log::set_logger(|max_log_level| {
+ max_log_level.set(options.log_level);
+ Box::new(Logger::new(io::stdout(), options.log_level))
+ }).unwrap_or_else(|e| die!("{}", e));
+}