~neon/activity-graph

244b37d839bc92e47a1faff52fcb2c0d79fc53fb — Jens Pitkanen 3 years ago 02a5945
Add -q
4 files changed, 48 insertions(+), 29 deletions(-)

M Cargo.lock
M src/log.rs
M src/main.rs
M src/server.rs
M Cargo.lock => Cargo.lock +1 -1
@@ 2,7 2,7 @@
# It is not intended for manual editing.
[[package]]
name = "activity-graph"
version = "0.1.0"
version = "0.2.0"
dependencies = [
 "chrono",
 "hyper",

M src/log.rs => src/log.rs +17 -4
@@ 4,15 4,25 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Mutex;
use std::time::{Duration, Instant};

use crate::Verbosity;

lazy_static::lazy_static! {
    static ref LAST_UPDATE_PRINT_TIME: Mutex<Option<Instant>> = Mutex::new(None);
}

static LAST_PRINT_WAS_UPDATE: AtomicBool = AtomicBool::new(false);
static VERBOSE: AtomicBool = AtomicBool::new(false);
static QUIET: AtomicBool = AtomicBool::new(false);

pub fn set_verbosity(verbosity: &Verbosity) {
    VERBOSE.store(verbosity.verbose, Ordering::Relaxed);
    QUIET.store(verbosity.quiet, Ordering::Relaxed);
}

pub fn set_verbosity(verbose: bool) {
    VERBOSE.store(verbose, Ordering::Relaxed);
pub fn println(s: &str) {
    if !QUIET.load(Ordering::Relaxed) {
        eprintln!("[{}] {}", timestamp(), s);
    }
}

pub fn verbose_println(s: &str, updating_line: bool) {


@@ 45,8 55,11 @@ pub fn verbose_println(s: &str, updating_line: bool) {
                    *last_update = None;
                }
            }
            let timestamp = Local::now().to_rfc3339_opts(SecondsFormat::Secs, true);
            eprintln!("[{}] {}", timestamp, s);
            eprintln!("[{}] {}", timestamp(), s);
        }
    }
}

fn timestamp() -> String {
    Local::now().to_rfc3339_opts(SecondsFormat::Secs, true)
}

M src/main.rs => src/main.rs +26 -19
@@ 87,12 87,21 @@ pub struct ExternalResources {
}

#[derive(StructOpt)]
pub struct Verbosity {
    /// Prints verbose information
    #[structopt(short, long)]
    verbose: bool,
    /// Disables all prints
    #[structopt(short, long)]
    quiet: bool,
}

#[derive(StructOpt)]
enum CommandArgs {
    /// Output the generated html into a file
    Generate {
        /// Prints verbose information
        #[structopt(short, long)]
        verbose: bool,
        #[structopt(flatten)]
        verbosity: Verbosity,
        #[structopt(flatten)]
        gen: GenerationData,
        #[structopt(flatten)]


@@ 108,9 117,8 @@ enum CommandArgs {

    /// Prints a visualization into stdout
    Stdout {
        /// Prints verbose information
        #[structopt(short, long)]
        verbose: bool,
        #[structopt(flatten)]
        verbosity: Verbosity,
        #[structopt(flatten)]
        gen: GenerationData,
    },


@@ 118,9 126,8 @@ enum CommandArgs {
    #[cfg(feature = "server")]
    /// Run a server that serves the generated activity graph html
    Server {
        /// Prints verbose information
        #[structopt(short, long)]
        verbose: bool,
        #[structopt(flatten)]
        verbosity: Verbosity,
        #[structopt(flatten)]
        gen: GenerationData,
        #[structopt(flatten)]


@@ 142,30 149,30 @@ fn main() {
    if let Some(command) = &args.command {
        match command {
            CommandArgs::Generate {
                verbose,
                verbosity,
                gen,
                ext,
                html,
                css,
            } => {
                log::set_verbosity(*verbose);
                log::set_verbosity(verbosity);

                let write_to_file = |path: &Path, s: String, name: &str| {
                    let mut writer = File::create(path).map(BufWriter::new);
                    match &mut writer {
                        Ok(writer) => {
                            if let Err(err) = writer.write(&s.as_bytes()) {
                                eprintln!(
                                log::println(&format!(
                                    "error: encountered while writing out the {}: {}",
                                    name, err
                                );
                                ));
                            }
                        }
                        Err(err) => {
                            eprintln!(
                            log::println(&format!(
                                "error: encountered while creating the {} file: {}",
                                name, err
                            );
                            ));
                        }
                    }
                };


@@ 181,20 188,20 @@ fn main() {
                }
            }

            CommandArgs::Stdout { verbose, gen } => {
                log::set_verbosity(*verbose);
            CommandArgs::Stdout { verbosity, gen } => {
                log::set_verbosity(verbosity);
                println!("{}", render::ascii(&generate_years(gen)));
            }

            #[cfg(feature = "server")]
            CommandArgs::Server {
                verbose,
                verbosity,
                gen,
                ext,
                host,
                cache_lifetime,
            } => {
                log::set_verbosity(*verbose);
                log::set_verbosity(verbosity);
                server::run(&gen, &ext, *host, *cache_lifetime);
            }
        }

M src/server.rs => src/server.rs +4 -5
@@ 52,14 52,14 @@ pub fn run(gen: &GenerationData, ext: &ExternalResources, host: SocketAddr, cach
                let make_service =
                    make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle)) });
                let server = Server::bind(&host).serve(make_service);
                log::verbose_println(&format!("server started on {}", host), false);
                log::println(&format!("server started on {}", host));
                if let Err(err) = server.await {
                    eprintln!("error: hyper server encountered an error: {}", err);
                    log::println(&format!("error: hyper server encountered an error: {}", err));
                }
            });
        }
        Err(err) => {
            eprintln!("error: could not start tokio runtime: {}", err);
            log::println(&format!("error: could not start tokio runtime: {}", err));
        }
    }
}


@@ 120,9 120,8 @@ async fn refresh_caches() {
                    *last_cache = Instant::now();
                }
            }
            log::verbose_println(
            log::println(
                &format!("updated cache, took {:?}", Instant::now() - start),
                false,
            );
            REFRESHING_CACHE.store(false, Ordering::Relaxed);
            CACHE_INITIALIZED.store(true, Ordering::Relaxed);