summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs38
1 files changed, 34 insertions, 4 deletions
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())