From 34d6d95b6f65b7dcce46c54507d7f0a826a708fe Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 8 Jun 2019 15:17:41 -0700 Subject: [PATCH] adopt log and env_logger for logging --- Cargo.toml | 2 ++ src/font/coretext.rs | 3 ++- src/font/fontconfigandfreetype.rs | 5 +++-- src/font/fontloader_and_freetype.rs | 5 +++-- src/font/ftfont.rs | 1 + src/font/mod.rs | 5 +++-- src/frontend/glium/glutinloop.rs | 3 ++- src/frontend/glium/window.rs | 9 +++++---- src/frontend/guicommon/host.rs | 3 ++- src/frontend/guicommon/window.rs | 5 +++-- src/frontend/muxserver/mod.rs | 3 ++- src/frontend/xwindows/mod.rs | 3 ++- src/frontend/xwindows/x11loop.rs | 3 ++- src/frontend/xwindows/xwin.rs | 5 +++-- src/log.rs | 3 --- src/main.rs | 12 ++++++------ src/mux/domain.rs | 3 ++- src/mux/mod.rs | 11 ++++++----- src/opengl/render.rs | 1 + src/server/client.rs | 3 ++- src/server/codec.rs | 5 +++-- src/server/listener.rs | 7 ++++--- 22 files changed, 59 insertions(+), 41 deletions(-) delete mode 100644 src/log.rs diff --git a/Cargo.toml b/Cargo.toml index bf4cdeb18..fb5cc63dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ bitflags = "1.0" clipboard = "0.5" dirs = "1.0" euclid = "0.19" +env_logger = "0.6" failure = "0.1" failure_derive = "0.1" foreign-types = "0.3" @@ -27,6 +28,7 @@ harfbuzz = { path = "deps/harfbuzz" } lazy_static = "1.3" leb128 = "0.2" libc = "0.2" +log = "0.4" open = "1.2" palette = "0.4" portable-pty = { path = "pty", features = ["serde_support"]} diff --git a/src/font/coretext.rs b/src/font/coretext.rs index 57b8a5c2b..f91b60946 100644 --- a/src/font/coretext.rs +++ b/src/font/coretext.rs @@ -15,6 +15,7 @@ use core_text::font_descriptor::{ kCTFontDefaultOrientation, SymbolicTraitAccessors, TraitAccessors, }; use failure::{format_err, Error}; +use log::debug; use std::ptr; #[allow(non_upper_case_globals)] @@ -246,7 +247,7 @@ impl Font for CoreTextFontImpl { // freetype renderer. let bearing_x = rect.origin.x - AA_PADDING; let bearing_y = height as f64 + AA_PADDING + rect.origin.y; - eprintln!( + debug!( "rasterize_glyph {:?} -> {}x{} bearing_x={} bearing_y={}", rect, width, height, bearing_x, bearing_y ); diff --git a/src/font/fontconfigandfreetype.rs b/src/font/fontconfigandfreetype.rs index 25371ad33..c20d70516 100644 --- a/src/font/fontconfigandfreetype.rs +++ b/src/font/fontconfigandfreetype.rs @@ -6,6 +6,7 @@ use crate::font::ftfont::FreeTypeFontImpl; use crate::font::{fcwrap, ftwrap}; use crate::font::{shape_with_harfbuzz, FallbackIdx, Font, FontSystem, GlyphInfo, NamedFont}; use failure::{bail, ensure, err_msg, Error}; +use log::error; pub type FontSystemImpl = FontConfigAndFreeType; @@ -28,7 +29,7 @@ impl FontSystem for FontConfigAndFreeType { let mut pattern = if !fonts.is_empty() { let mut pattern = FontPattern::new()?; if fonts.len() > 1 { - eprintln!( + error!( "FIXME: fontconfig loader currently only processes the first in your set of fonts for {:?}", style @@ -88,7 +89,7 @@ impl NamedFontImpl { // care to abort the rest of what we're doing match lib.set_lcd_filter(ftwrap::FT_LcdFilter::FT_LCD_FILTER_DEFAULT) { Ok(_) => (), - Err(err) => eprintln!("Ignoring: FT_LcdFilter failed: {:?}", err), + Err(err) => error!("Ignoring: FT_LcdFilter failed: {:?}", err), }; // Enable some filtering options and pull in the standard diff --git a/src/font/fontloader_and_freetype.rs b/src/font/fontloader_and_freetype.rs index 105b06ae5..6c25e62b2 100644 --- a/src/font/fontloader_and_freetype.rs +++ b/src/font/fontloader_and_freetype.rs @@ -6,6 +6,7 @@ use crate::font::{ ftwrap, shape_with_harfbuzz, FallbackIdx, Font, FontSystem, GlyphInfo, NamedFont, }; use failure::{format_err, Error}; +use log::{debug, error}; struct NamedFontImpl { _lib: ftwrap::Library, @@ -41,13 +42,13 @@ impl FontSystem for FontLoaderAndFreeType { // care to abort the rest of what we're doing match lib.set_lcd_filter(ftwrap::FT_LcdFilter::FT_LCD_FILTER_DEFAULT) { Ok(_) => (), - Err(err) => eprintln!("Ignoring: FT_LcdFilter failed: {:?}", err), + Err(err) => error!("Ignoring: FT_LcdFilter failed: {:?}", err), }; let mut fonts = Vec::new(); let mut fontdata = Vec::new(); for (data, idx) in fontloader::load_system_fonts(config, style)? { - eprintln!("want idx {} in bytes of len {}", idx, data.len()); + debug!("want idx {} in bytes of len {}", idx, data.len()); let face = lib.new_face_from_slice(&data, idx.into())?; fontdata.push(data); diff --git a/src/font/ftfont.rs b/src/font/ftfont.rs index f455d335e..99c0e8470 100644 --- a/src/font/ftfont.rs +++ b/src/font/ftfont.rs @@ -2,6 +2,7 @@ use super::hbwrap as harfbuzz; use crate::font::{ftwrap, Font, FontMetrics, RasterizedGlyph}; use failure::{bail, Error}; +use log::debug; use std::cell::RefCell; use std::mem; use std::slice; diff --git a/src/font/mod.rs b/src/font/mod.rs index 878811ef6..bed530fd9 100644 --- a/src/font/mod.rs +++ b/src/font/mod.rs @@ -1,4 +1,5 @@ use failure::{bail, format_err, Error}; +use log::{debug, error}; use serde_derive::*; mod ftfont; mod hbwrap; @@ -319,7 +320,7 @@ pub fn shape_with_harfbuzz( let mut shape = match shape_with_harfbuzz(font, font_idx + 1, substr) { Ok(shape) => Ok(shape), Err(e) => { - eprintln!("{:?} for {:?}", e, substr); + error!("{:?} for {:?}", e, substr); if font_idx == 0 && s == "?" { bail!("unable to find any usable glyphs for `?` in font_idx 0"); } @@ -361,7 +362,7 @@ pub fn shape_with_harfbuzz( let mut shape = match shape_with_harfbuzz(font, font_idx + 1, substr) { Ok(shape) => Ok(shape), Err(e) => { - eprintln!("{:?} for {:?}", e, substr); + error!("{:?} for {:?}", e, substr); if font_idx == 0 && s == "?" { bail!("unable to find any usable glyphs for `?` in font_idx 0"); } diff --git a/src/frontend/glium/glutinloop.rs b/src/frontend/glium/glutinloop.rs index 1b33e6aad..f30fb3522 100644 --- a/src/frontend/glium/glutinloop.rs +++ b/src/frontend/glium/glutinloop.rs @@ -9,6 +9,7 @@ use failure::{bail, Error}; use glium; use glium::glutin::EventsLoopProxy; use glium::glutin::WindowId; +use log::{debug, error}; use promise::{Executor, Future, SpawnFunc}; use std::cell::RefCell; use std::collections::HashMap; @@ -368,7 +369,7 @@ impl GuiEventLoop { } Ok(Break) => Break, Err(err) => { - eprintln!("Error in event loop: {:?}", err); + error!("Error in event loop: {:?}", err); Break } } diff --git a/src/frontend/glium/window.rs b/src/frontend/glium/window.rs index daef78de1..3b7d2e1cb 100644 --- a/src/frontend/glium/window.rs +++ b/src/frontend/glium/window.rs @@ -13,6 +13,7 @@ use failure::{format_err, Error}; use glium; use glium::glutin::dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize}; use glium::glutin::{self, ElementState, MouseCursor}; +use log::{debug, error}; use std::rc::Rc; use std::sync::Arc; use term; @@ -175,7 +176,7 @@ impl TerminalWindow for GliumTerminalWindow { .ok_or_else(|| format_err!("failed to get inner window size"))?; let dpi_scale = self.host.display.gl_window().get_hidpi_factor(); let (width, height): (u32, u32) = size.to_physical(dpi_scale).into(); - eprintln!( + debug!( "resize {}x{}@{} -> {}x{}@{}", self.width, self.height, old_dpi_scale, width, height, dpi_scale ); @@ -207,7 +208,7 @@ impl GliumTerminalWindow { let height = cell_height * physical_rows; let logical_size = LogicalSize::new(width as f64, height as f64); - eprintln!("make window with {}x{}", width, height); + debug!("make window with {}x{}", width, height); let display = { let pref_context = glutin::ContextBuilder::new() @@ -645,7 +646,7 @@ impl GliumTerminalWindow { ElementState::Released => {} } } else { - eprintln!("event {:?} with no mapping", event); + error!("event {:?} with no mapping", event); } self.paint_if_needed()?; Ok(()) @@ -685,7 +686,7 @@ impl GliumTerminalWindow { } => { // Coupled with logic in key_event which gates whether // we allow processing unicode chars here - // eprintln!("ReceivedCharacter {} {:?}", c as u32, c); + // debug!("ReceivedCharacter {} {:?}", c as u32, c); if self.allow_received_character { self.allow_received_character = false; let mux = Mux::get().unwrap(); diff --git a/src/frontend/guicommon/host.rs b/src/frontend/guicommon/host.rs index 6ecc2c98a..af9cbb486 100644 --- a/src/frontend/guicommon/host.rs +++ b/src/frontend/guicommon/host.rs @@ -6,6 +6,7 @@ use crate::mux::Mux; use clipboard::{ClipboardContext, ClipboardProvider}; use failure::Fallible; use failure::{format_err, Error}; +use log::error; use portable_pty::PtySize; use promise::Future; use std::collections::HashMap; @@ -358,7 +359,7 @@ impl<'a, H: HostHelper> term::TerminalHost for TabHost<'a, H> { fn click_link(&mut self, link: &Arc) { match open::that(link.uri()) { Ok(_) => {} - Err(err) => eprintln!("failed to open {}: {:?}", link.uri(), err), + Err(err) => error!("failed to open {}: {:?}", link.uri(), err), } } diff --git a/src/frontend/guicommon/window.rs b/src/frontend/guicommon/window.rs index 1b1dd99db..15e896289 100644 --- a/src/frontend/guicommon/window.rs +++ b/src/frontend/guicommon/window.rs @@ -7,6 +7,7 @@ use crate::opengl::render::Renderer; use crate::opengl::textureatlas::OutOfTextureSpace; use failure::{ensure, format_err, Error}; use glium; +use log::{debug, error}; use portable_pty::PtySize; use std::rc::Rc; use std::sync::Arc; @@ -145,7 +146,7 @@ pub trait TerminalWindow { match res { Err(err) => { if let Some(&OutOfTextureSpace { size }) = err.downcast_ref::() { - eprintln!("out of texture space, allocating {}", size); + error!("out of texture space, allocating {}", size); self.recreate_texture_atlas(size)?; tab.renderer().make_all_lines_dirty(); // Recursively initiate a new paint @@ -225,7 +226,7 @@ pub trait TerminalWindow { let fonts = self.fonts(); let dpi_scale = dpi_scale.unwrap_or_else(|| fonts.get_dpi_scale()); let font_scale = font_scale.unwrap_or_else(|| fonts.get_font_scale()); - eprintln!( + debug!( "TerminalWindow::scaling_changed dpi_scale={} font_scale={}", dpi_scale, font_scale ); diff --git a/src/frontend/muxserver/mod.rs b/src/frontend/muxserver/mod.rs index 77b77c93b..77d968c24 100644 --- a/src/frontend/muxserver/mod.rs +++ b/src/frontend/muxserver/mod.rs @@ -6,6 +6,7 @@ use crate::mux::tab::Tab; use crate::mux::Mux; use crate::server::listener::spawn_listener; use failure::{bail, Error}; +use log::info; use promise::Executor; use promise::SpawnFunc; use std::rc::Rc; @@ -68,7 +69,7 @@ impl FrontEnd for MuxServerFrontEnd { } if Mux::get().unwrap().is_empty() { - eprintln!("No more tabs; all done!"); + info!("No more tabs; all done!"); return Ok(()); } } diff --git a/src/frontend/xwindows/mod.rs b/src/frontend/xwindows/mod.rs index 03b5f8161..f903ebb5a 100644 --- a/src/frontend/xwindows/mod.rs +++ b/src/frontend/xwindows/mod.rs @@ -1,3 +1,4 @@ +use log::debug; use term::{KeyCode, KeyModifiers}; mod keyboard; use egli; @@ -124,7 +125,7 @@ impl Connection { let egl_display = egli::Display::from_display_id(display as *mut _).map_err(egli_err)?; let egl_version = egl_display.initialize_and_get_version().map_err(egli_err)?; - println!("Using EGL {}", egl_version); + debug!("Using EGL {}", egl_version); let configs = egl_display .config_filter() diff --git a/src/frontend/xwindows/x11loop.rs b/src/frontend/xwindows/x11loop.rs index 2cebe536b..ecc10c253 100644 --- a/src/frontend/xwindows/x11loop.rs +++ b/src/frontend/xwindows/x11loop.rs @@ -7,6 +7,7 @@ use crate::frontend::FrontEnd; use crate::mux::tab::Tab; use crate::mux::Mux; use failure::{bail, Error}; +use log::debug; use mio::{Events, Poll, PollOpt, Ready, Token}; use mio_extras::channel::{channel, Receiver as GuiReceiver, Sender as GuiSender}; use portable_pty::PtySize; @@ -234,7 +235,7 @@ impl GuiEventLoop { } fn schedule_window_close(&self, window_id: WindowId) -> Result<(), Error> { - eprintln!("schedule_window_close {:?}", window_id); + debug!("schedule_window_close {:?}", window_id); let mut windows = self.windows.borrow_mut(); windows.by_id.remove(&window_id); diff --git a/src/frontend/xwindows/xwin.rs b/src/frontend/xwindows/xwin.rs index 16346c8b5..0b0d5c979 100644 --- a/src/frontend/xwindows/xwin.rs +++ b/src/frontend/xwindows/xwin.rs @@ -10,6 +10,7 @@ use crate::mux::window::WindowId; use crate::mux::{Mux, SessionTerminated}; use crate::opengl::render::Renderer; use failure::Error; +use log::{debug, error}; use std::rc::Rc; use std::sync::Arc; use term::{self, KeyCode, KeyModifiers, MouseButton, MouseEvent, MouseEventKind}; @@ -239,7 +240,7 @@ impl X11TerminalWindow { 4 => MouseButton::WheelUp, 5 => MouseButton::WheelDown, _ => { - eprintln!("button {} is not implemented", button_press.detail()); + error!("button {} is not implemented", button_press.detail()); return Ok(()); } }, @@ -250,7 +251,7 @@ impl X11TerminalWindow { } xcb::CLIENT_MESSAGE => { let msg: &xcb::ClientMessageEvent = unsafe { xcb::cast_event(event) }; - println!("CLIENT_MESSAGE {:?}", msg.data().data32()); + debug!("CLIENT_MESSAGE {:?}", msg.data().data32()); if msg.data().data32()[0] == self.conn.atom_delete() { return Err(SessionTerminated::WindowClosed.into()); } diff --git a/src/log.rs b/src/log.rs deleted file mode 100644 index f557e3e89..000000000 --- a/src/log.rs +++ /dev/null @@ -1,3 +0,0 @@ -macro_rules! debug { - ($($arg:tt)*) => (if cfg!(debug_assertions) { println!($($arg)*) }) -} diff --git a/src/main.rs b/src/main.rs index c3d6ada73..5b2a2bd54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,8 @@ // Don't create a new standard console window when launched from the windows GUI. #![windows_subsystem = "windows"] -#[macro_use] -pub mod log; use failure::Error; +use log::{error, info}; use std::ffi::OsString; use structopt::StructOpt; @@ -139,6 +138,7 @@ fn run_terminal_gui(config: Arc, opts: &StartCommand) -> Result< } fn main() -> Result<(), Error> { + env_logger::init(); // This is a bit gross. // In order to not to automatically open a standard windows console when // we run, we use the windows_subsystem attribute at the top of this @@ -169,21 +169,21 @@ fn main() -> Result<(), Error> { .unwrap_or_else(|| SubCommand::Start(StartCommand::default())) { SubCommand::Start(start) => { - println!("Using configuration: {:#?}\nopts: {:#?}", config, opts); + error!("Using configuration: {:#?}\nopts: {:#?}", config, opts); run_terminal_gui(config, &start) } SubCommand::Cli(_) => { use crate::server::client::Client; use crate::server::codec::*; let mut client = Client::new(&config)?; - eprintln!("ping: {:?}", client.ping()?); + info!("ping: {:?}", client.ping()?); let tabs = client.list_tabs()?; for (tab_id, title) in tabs.tabs.iter() { - eprintln!("tab {}: {}", tab_id, title); + info!("tab {}: {}", tab_id, title); let _data = client.get_coarse_tab_renderable_data(GetCoarseTabRenderableData { tab_id: *tab_id, })?; - // eprintln!("coarse: {:?}", data); + // info!("coarse: {:?}", data); } Ok(()) } diff --git a/src/mux/domain.rs b/src/mux/domain.rs index 244cf8ae1..b2ca46a4a 100644 --- a/src/mux/domain.rs +++ b/src/mux/domain.rs @@ -10,6 +10,7 @@ use crate::frontend::guicommon::localtab::LocalTab; use crate::mux::tab::Tab; use crate::mux::Mux; use failure::Error; +use log::info; use portable_pty::cmdbuilder::CommandBuilder; use portable_pty::{PtySize, PtySystem}; use std::rc::Rc; @@ -41,7 +42,7 @@ impl Domain for LocalDomain { }; let (master, slave) = self.pty_system.openpty(size)?; let child = slave.spawn_command(cmd)?; - eprintln!("spawned: {:?}", child); + info!("spawned: {:?}", child); let terminal = term::Terminal::new( size.rows as usize, diff --git a/src/mux/mod.rs b/src/mux/mod.rs index 88c4dd1db..424fdbada 100644 --- a/src/mux/mod.rs +++ b/src/mux/mod.rs @@ -2,6 +2,7 @@ use crate::config::Config; use crate::frontend::gui_executor; use failure::Error; use failure_derive::*; +use log::{debug, error, warn}; use portable_pty::ExitStatus; use promise::{Executor, Future}; use std::cell::{Ref, RefCell, RefMut}; @@ -36,11 +37,11 @@ fn read_from_tab_pty(tab_id: TabId, mut reader: Box) { loop { match reader.read(&mut buf) { Ok(size) if size == 0 => { - eprintln!("read_pty EOF: tab_id {}", tab_id); + error!("read_pty EOF: tab_id {}", tab_id); break; } Err(err) => { - eprintln!("read_pty failed: tab {} {:?}", tab_id, err); + error!("read_pty failed: tab {} {:?}", tab_id, err); break; } Ok(size) => { @@ -83,12 +84,12 @@ impl<'a> TerminalHost for Host<'a> { fn click_link(&mut self, link: &Arc) { match open::that(link.uri()) { Ok(_) => {} - Err(err) => eprintln!("failed to open {}: {:?}", link.uri(), err), + Err(err) => error!("failed to open {}: {:?}", link.uri(), err), } } fn get_clipboard(&mut self) -> Result { - eprintln!("peer requested clipboard; ignoring"); + warn!("peer requested clipboard; ignoring"); Ok("".into()) } @@ -152,7 +153,7 @@ impl Mux { } pub fn remove_tab(&self, tab_id: TabId) { - eprintln!("removing tab {}", tab_id); + debug!("removing tab {}", tab_id); self.tabs.borrow_mut().remove(&tab_id); } diff --git a/src/opengl/render.rs b/src/opengl/render.rs index 321aae779..255f2fcb9 100644 --- a/src/opengl/render.rs +++ b/src/opengl/render.rs @@ -10,6 +10,7 @@ use glium::backend::Facade; use glium::texture::SrgbTexture2d; use glium::{self, IndexBuffer, Surface, VertexBuffer}; use glium::{implement_vertex, uniform}; +use log::debug; use std::cell::RefCell; use std::collections::HashMap; use std::mem; diff --git a/src/server/client.rs b/src/server/client.rs index 44dff3e0d..fd74549d6 100644 --- a/src/server/client.rs +++ b/src/server/client.rs @@ -2,6 +2,7 @@ use crate::config::Config; use crate::server::codec::*; use crate::server::UnixStream; use failure::{bail, ensure, err_msg, Error}; +use log::info; use std::path::Path; use std::sync::Arc; @@ -43,7 +44,7 @@ impl Client { .as_ref() .ok_or_else(|| err_msg("no mux_server_unix_domain_socket_path"))?, ); - eprintln!("connect to {}", sock_path.display()); + info!("connect to {}", sock_path.display()); let stream = UnixStream::connect(sock_path)?; Ok(Self { stream, serial: 0 }) } diff --git a/src/server/codec.rs b/src/server/codec.rs index b55c9acb3..ed1d6bd4d 100644 --- a/src/server/codec.rs +++ b/src/server/codec.rs @@ -13,6 +13,7 @@ use crate::mux::tab::TabId; use failure::{bail, Error}; use leb128; +use log::debug; use serde_derive::*; use std::collections::HashMap; use std::sync::Arc; @@ -88,7 +89,7 @@ fn decode_raw(mut r: R) -> Result { } else { (len, false) }; - eprintln!("decode_raw {} compressed={}", len, is_compressed); + debug!("decode_raw {} compressed={}", len, is_compressed); let serial = read_u64(r.by_ref())?; let ident = read_u64(r.by_ref())?; let data_len = len as usize - (encoded_length(ident) + encoded_length(serial)); @@ -127,7 +128,7 @@ fn serialize(t: &T) -> Result<(Vec, bool), Error> { drop(encode); compress.finish()?; - eprintln!( + debug!( "serialized+compress len {} vs {}", compressed.len(), uncompressed.len() diff --git a/src/server/listener.rs b/src/server/listener.rs index fb45d1aec..a1f4366cf 100644 --- a/src/server/listener.rs +++ b/src/server/listener.rs @@ -5,6 +5,7 @@ use crate::server::{UnixListener, UnixStream}; use failure::{err_msg, format_err, Error}; #[cfg(unix)] use libc::{mode_t, umask}; +use log::{debug, error}; use promise::{Executor, Future}; use std::collections::HashMap; use std::fs::{remove_file, DirBuilder}; @@ -33,7 +34,7 @@ impl Listener { thread::spawn(move || session.run()); } Err(err) => { - eprintln!("accept failed: {}", err); + error!("accept failed: {}", err); return; } } @@ -54,7 +55,7 @@ impl ClientSession { fn process(&mut self) -> Result<(), Error> { loop { let decoded = Pdu::decode(&mut self.stream)?; - eprintln!("got pdu {:?} from client", decoded); + debug!("got pdu {:?} from client", decoded); match decoded.pdu { Pdu::Ping(Ping {}) => { Pdu::Pong(Pong {}).encode(&mut self.stream, decoded.serial)?; @@ -153,7 +154,7 @@ impl Drop for UmaskSaver { fn safely_create_sock_path(sock_path: &str) -> Result { let sock_path = Path::new(sock_path); - eprintln!("setting up {}", sock_path.display()); + debug!("setting up {}", sock_path.display()); let _saver = UmaskSaver::new();