Permalink
Browse files

Merge branch 'loggin''

  • Loading branch information...
ogham committed Aug 19, 2017
2 parents e933fa6 + 80e3d6f commit e0727a150b5839ccbc796f929dd2618a584a3e2b
Showing with 162 additions and 19 deletions.
  1. +80 −0 Cargo.lock
  2. +3 −1 Cargo.toml
  3. +29 −1 src/bin/main.rs
  4. +2 −2 src/exa.rs
  5. +2 −0 src/fs/dir.rs
  6. +2 −0 src/fs/feature/git.rs
  7. +21 −11 src/fs/file.rs
  8. +12 −3 src/output/details.rs
  9. +2 −0 xtests/debug
  10. +9 −1 xtests/run.sh

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -25,19 +25,21 @@ path = "src/exa.rs"
[dependencies]
ansi_term = "0.8.0"
datetime = "0.4.3"
env_logger = "0.3"
getopts = "0.2.14"
glob = "0.2"
lazy_static = "0.2"
libc = "0.2.9"
locale = "0.2.1"
log = "0.3"
natord = "1.0.7"
num_cpus = "1.3.0"
number_prefix = "0.2.3"
scoped_threadpool = "0.1.*"
term_grid = "0.1.6"
term_size = "0.3.0"
unicode-width = "0.1.4"
users = "0.5.2"
term_size = "0.3.0"
[features]
default = [ "git" ]
@@ -2,12 +2,14 @@ extern crate exa;
use exa::Exa;
use std::ffi::OsString;
use std::env::args_os;
use std::env::{args_os, var_os};
use std::io::{stdout, stderr, Write, ErrorKind};
use std::process::exit;
fn main() {
configure_logger();
let args: Vec<OsString> = args_os().skip(1).collect();
match Exa::new(args.iter(), &mut stdout()) {
Ok(mut exa) => {
@@ -38,6 +40,32 @@ fn main() {
}
/// Sets up a global logger if one is asked for.
/// The ‘EXA_DEBUG’ environment variable controls whether log messages are
/// displayed or not. Currently there are just two settings (on and off)
pub fn configure_logger() {
extern crate env_logger;
extern crate log;
let present = match var_os("EXA_DEBUG") {
Some(debug) => debug.len() > 0,
None => false,
};
let mut logs = env_logger::LogBuilder::new();
if present {
logs.filter(None, log::LogLevelFilter::Debug);
}
else {
logs.filter(None, log::LogLevelFilter::Off);
}
if let Err(e) = logs.init() {
writeln!(stderr(), "Failed to initialise logger: {}", e).unwrap();
}
}
extern crate libc;
#[allow(trivial_numeric_casts)]
mod exits {
@@ -18,8 +18,8 @@ extern crate term_size;
#[cfg(feature="git")] extern crate git2;
#[macro_use]
extern crate lazy_static;
#[macro_use] extern crate lazy_static;
#[macro_use] extern crate log;
use std::env::var_os;
@@ -37,6 +37,8 @@ impl Dir {
/// entries, so if the user wants to see them, we’ll have to add them
/// ourselves after the files have been read.
pub fn read_dir(path: PathBuf, git: bool) -> IOResult<Dir> {
info!("Reading directory {:?}", &path);
let contents: Vec<PathBuf> = try!(fs::read_dir(&path)?
.map(|result| result.map(|entry| entry.path()))
.collect());
@@ -15,6 +15,8 @@ impl Git {
/// Discover a Git repository on or above this directory, scanning it for
/// the files' statuses if one is found.
pub fn scan(path: &Path) -> Result<Git, git2::Error> {
info!("Scanning for Git repository under {:?}", path);
let repo = git2::Repository::discover(path)?;
let workdir = match repo.workdir() {
Some(w) => w,
@@ -62,9 +62,11 @@ impl<'dir> File<'dir> {
FN: Into<Option<String>>
{
let parent_dir = parent_dir.into();
let metadata = fs::symlink_metadata(&path)?;
let name = filename.into().unwrap_or_else(|| File::filename(&path));
let ext = File::ext(&path);
debug!("Statting file {:?}", &path);
let metadata = fs::symlink_metadata(&path)?;
Ok(File { path, parent_dir, metadata, ext, name })
}
@@ -73,9 +75,13 @@ impl<'dir> File<'dir> {
/// such as `/` or `..`, which have no `file_name` component. So instead, just
/// use the last component as the name.
pub fn filename(path: &Path) -> String {
match path.components().next_back() {
Some(back) => back.as_os_str().to_string_lossy().to_string(),
None => path.display().to_string(), // use the path as fallback
if let Some(back) = path.components().next_back() {
back.as_os_str().to_string_lossy().to_string()
}
else {
// use the path as fallback
error!("Path {:?} has no last component", path);
path.display().to_string()
}
}
@@ -187,6 +193,7 @@ impl<'dir> File<'dir> {
// this file -- which could be absolute or relative -- to the path
// we actually look up and turn into a `File` -- which needs to be
// absolute to be accessible from any directory.
debug!("Reading link {:?}", &self.path);
let path = match fs::read_link(&self.path) {
Ok(p) => p,
Err(e) => return FileTarget::Err(e),
@@ -196,13 +203,16 @@ impl<'dir> File<'dir> {
// Use plain `metadata` instead of `symlink_metadata` - we *want* to
// follow links.
if let Ok(metadata) = fs::metadata(&absolute_path) {
let ext = File::ext(&path);
let name = File::filename(&path);
FileTarget::Ok(File { parent_dir: None, path, ext, metadata, name })
}
else {
FileTarget::Broken(path)
match fs::metadata(&absolute_path) {
Ok(metadata) => {
let ext = File::ext(&path);
let name = File::filename(&path);
FileTarget::Ok(File { parent_dir: None, path, ext, metadata, name })
}
Err(e) => {
error!("Error following link {:?}: {:#?}", &path, e);
FileTarget::Broken(path)
}
}
}
@@ -214,9 +214,18 @@ impl<'a> Render<'a> {
if xattr::ENABLED {
match file.path.attributes() {
Ok(xs) => xattrs.extend(xs),
Err(e) => if self.opts.xattr { errors.push((e, None)) },
};
Ok(xs) => {
xattrs.extend(xs);
}
Err(e) => {
if self.opts.xattr {
errors.push((e, None));
}
else {
error!("Error looking up xattr for {:?}: {:#?}", file.path, e);
}
}
}
}
let table_row = table.as_ref().map(|t| t.row_for_file(&file, !xattrs.is_empty()));
@@ -0,0 +1,2 @@
DEBUG:exa::fs::file: Statting file "/testcases/attributes/dirs/no-xattrs_empty"
INFO:exa::fs::dir: Reading directory "/testcases/attributes/dirs/no-xattrs_empty"
@@ -17,7 +17,10 @@ results="/vagrant/xtests"
# We want to use strict mode here. It’s important that no combination of
# testing flags happens to work by accident!
export EXA_STRICT=1
export EXA_STRICT="1"
# We also don’t want to see reams and reams of debug output.
export EXA_DEBUG=""
# Check that no files were created more than a year ago.
@@ -188,6 +191,11 @@ $exa --time 2>&1 | diff -q - $results/error_value || exit 1
$exa --long=time 2>&1 | diff -q - $results/error_overvalued || exit 1
# Debug mode
# (uses an empty directory so it prints nothing to stdout)
EXA_DEBUG="1" $exa $testcases/attributes/dirs/no-xattrs_empty -lh 2>&1 | diff -q - $results/debug || exit 1
# And finally...
$exa --help | diff -q - $results/help || exit 1
$exa --help --long | diff -q - $results/help_long || exit 1

0 comments on commit e0727a1

Please sign in to comment.