From fb9f143f2353dc8c64a18be84c12b53cdad847e7 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Sat, 1 Feb 2020 09:10:54 -0500 Subject: Add strava importer threadpool --- src/main.rs | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 6e11fc4..7ced591 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +#![feature(str_strip)] extern crate fern; #[macro_use] extern crate log; @@ -9,13 +10,42 @@ use diesel::connection::Connection; use diesel::pg::PgConnection; fn setup_logger() -> Result<(), fern::InitError> { - use fern::colors::ColoredLevelConfig; + use fern::colors::{Color, ColoredLevelConfig}; let colors = ColoredLevelConfig::new(); + fern::Dispatch::new() .format(move |out, message, record| { - out.finish(format_args!("[{}] {}", - colors.color(record.level()), - message)) + let thread = std::thread::current(); + + let thread_id = &format!("{:?}", thread.id())[..]; + let prefix = "ThreadId("; + let thread_id = if thread_id.find(prefix).is_some() { + &thread_id[prefix.len() .. thread_id.len() - 1] + } else { + thread_id + }; + + let thread_colors = [Color::Red, Color::Green, Color::Magenta, + Color::Cyan, Color::White, Color::BrightRed, + Color::BrightGreen, Color::BrightMagenta, + Color::BrightWhite]; + use std::collections::hash_map::DefaultHasher; + use std::hash::{Hash, Hasher}; + let mut hasher = DefaultHasher::new(); + thread_id.hash(&mut hasher); + let thread_color = thread_colors[hasher.finish() as usize % thread_colors.len()]; + + // if thread_id.find("ThreadId(") { + // } + // TODO: Make a random color based on the thread name. + out.finish(format_args!( + "[{}] \x1B[{}m{}@{}\x1B[0m {}", + colors.color(record.level()), + thread_color.to_fg_str(), + thread.name().unwrap_or(""), + thread_id, + message + )) }) .level(log::LevelFilter::Info) .chain(std::io::stdout()) -- cgit v1.2.3