Browse Source

Use happylog

Michael Ekstrand 6 months ago
parent
commit
6a78022664
7 changed files with 370 additions and 612 deletions
  1. 362
    505
      Cargo.lock
  2. 3
    2
      Cargo.toml
  3. 1
    1
      src/commands/import_json.rs
  4. 2
    2
      src/commands/parse_marc.rs
  5. 1
    1
      src/commands/pcat.rs
  6. 0
    99
      src/logging.rs
  7. 1
    2
      src/main.rs

File diff suppressed because it is too large
+ 362
- 505
Cargo.lock

+ 3
- 2
Cargo.toml

@@ -5,10 +5,10 @@ authors = ["Michael Ekstrand <michaelekstrand@boisestate.edu>"]
 edition = "2018"
 
 [dependencies]
-structopt = "0.2"
+structopt = ">=0.2"
 quick-xml = "^0.17.2"
 flate2 = "1.0.6"
-indicatif = "0.11"
+indicatif = ">=0.11"
 console = "0.7"
 zip = "0.5.0"
 log = { version = "^0.4.8", features = ["std"] }
@@ -24,3 +24,4 @@ anyhow = "1.0.26"
 serde = { version="1.0", features=["derive"] }
 toml = "^0.5"
 crossbeam-channel = "~0.4.2"
+happylog = { git = "https://github.com/mdekstrand/happylog", features=["structopt"] }

+ 1
- 1
src/commands/import_json.rs

@@ -12,12 +12,12 @@ use sha1::Sha1;
 use anyhow::{Result};
 use serde::{Deserialize};
 use toml;
+use happylog::set_progress;
 
 use crate::io::{HashWrite, DelimPrinter};
 use crate::cleaning::*;
 use crate::db::{DbOpts, CopyRequest};
 use crate::tracking::StageOpts;
-use crate::logging::set_progress;
 use super::Command;
 
 /// Process OpenLib data into format suitable for PostgreSQL import.

+ 2
- 2
src/commands/parse_marc.rs

@@ -14,13 +14,13 @@ use quick_xml::events::Event;
 use flate2::bufread::MultiGzDecoder;
 use indicatif::{ProgressBar, ProgressStyle};
 use anyhow::{Result, anyhow};
+use happylog::set_progress;
 
 use crate::cleaning::write_pgencoded;
 use crate::tsv::split_first;
 use crate::tracking::StageOpts;
 use crate::io::{HashWrite};
 use crate::db::{DbOpts, CopyRequest};
-use crate::logging;
 use super::Command;
 
 /// Parse MARC files into records for a PostgreSQL table.
@@ -225,7 +225,7 @@ impl Command for ParseMarc {
       let fs = File::open(inf)?;
       let pb = ProgressBar::new(fs.metadata()?.len());
       pb.set_style(ProgressStyle::default_bar().template("{elapsed_precise} {bar} {percent}% {bytes}/{total_bytes} (eta: {eta})"));
-      let _pbs = logging::set_progress(&pb);
+      let _pbs = set_progress(&pb);
       let mut in_sf = stage.source_file(inf);
       let pbr = pb.wrap_read(fs);
       let pbr = BufReader::new(pbr);

+ 1
- 1
src/commands/pcat.rs

@@ -8,11 +8,11 @@ use log::*;
 use indicatif::{ProgressBar, ProgressStyle};
 use sha1::Sha1;
 use anyhow::Result;
+use happylog::set_progress;
 
 use crate::db::{DbOpts, CopyRequest};
 use crate::tracking::{Stage, StageOpts};
 use crate::io::{HashWrite};
-use crate::logging::set_progress;
 use super::Command;
 
 const PB_STYLE: &'static str = "{prefix}: {elapsed_precise} {bar} {percent}% {bytes}/{total_bytes} (eta: {eta})";

+ 0
- 99
src/logging.rs

@@ -1,99 +0,0 @@
-use structopt::StructOpt;
-use indicatif::ProgressBar;
-use log::*;
-
-use std::sync::atomic::{AtomicPtr, Ordering};
-use std::ptr;
-use std::marker::PhantomData;
-
-use anyhow::Result;
-
-static LOG_PB: AtomicPtr<ProgressBar> = AtomicPtr::new(ptr::null_mut());
-
-/// Progress bar logging context
-pub struct LogPBState<'a> {
-  phantom: PhantomData<&'a str>
-}
-
-struct LogEnv {
-  level: LevelFilter,
-  progress: &'static AtomicPtr<ProgressBar>
-}
-
-impl Log for LogEnv {
-  fn enabled(&self, metadata: &Metadata) -> bool {
-    metadata.level() <= self.level
-  }
-
-  fn log(&self, record: &Record) {
-    let pass = record.level() <= self.level;
-    if pass {
-      let pb_ptr = self.progress.load(Ordering::Relaxed);
-      if pb_ptr.is_null() {
-        eprintln!("[{:>5}] {}", record.level(), record.args());
-      } else {
-        let msg = format!("[{:>5}] {}", record.level(), record.args());
-        unsafe {
-          let pb = &*pb_ptr;
-          pb.println(msg)
-        }
-      }
-    }
-  }
-
-  fn flush(&self) {}
-}
-
-fn verbosify(f: LevelFilter) -> LevelFilter {
-  match f {
-    LevelFilter::Error => LevelFilter::Warn,
-    LevelFilter::Warn => LevelFilter::Info,
-    LevelFilter::Info => LevelFilter::Debug,
-    LevelFilter::Debug => LevelFilter::Trace,
-    x => x
-  }
-}
-
-#[derive(StructOpt, Debug)]
-pub struct LogOpts {
-  /// Verbose mode (-v, -vv, -vvv, etc.)
-  #[structopt(short="v", long="verbose", parse(from_occurrences))]
-  verbose: usize,
-  /// Silence output
-  #[structopt(short="q", long="quiet")]
-  quiet: bool
-}
-
-impl LogOpts {
-  /// Initialize logging
-  pub fn init(&self) -> Result<()> {
-    let mut level = LevelFilter::Info;
-    if self.quiet {
-      level = LevelFilter::Off;
-    }
-    for _i in 0..self.verbose {
-      level = verbosify(level);
-    }
-    let logger = LogEnv {
-      level: level,
-      progress: &LOG_PB
-    };
-    set_boxed_logger(Box::new(logger))?;
-    set_max_level(level);
-    Ok(())
-  }
-}
-
-pub fn set_progress<'a>(pb: &'a ProgressBar) -> LogPBState<'a> {
-  let pbb = Box::new(pb.clone());
-  LOG_PB.store(Box::leak(pbb), Ordering::Relaxed);
-  LogPBState {
-    phantom: PhantomData
-  }
-}
-
-impl <'a> Drop for LogPBState<'a> {
-  fn drop(&mut self) {
-    LOG_PB.store(ptr::null_mut(), Ordering::Relaxed);
-  }
-}

+ 1
- 2
src/main.rs

@@ -3,14 +3,13 @@ mod tsv;
 mod db;
 mod io;
 mod tracking;
-mod logging;
 mod commands;
 
 use anyhow::{anyhow, Result};
 use log::*;
 use structopt::StructOpt;
 
-use logging::LogOpts;
+use happylog::args::LogOpts;
 use commands::*;
 
 /// BookData import tools