Change logger to fern

It is easier to setup per module.
This commit is contained in:
Demmie 2023-04-11 22:22:14 -04:00
parent 55ba0b9bb3
commit 91224d63e8
No known key found for this signature in database
GPG Key ID: B06DAA3D432C6E9A
6 changed files with 81 additions and 32 deletions

64
Cargo.lock generated
View File

@ -170,6 +170,17 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "debc29dde2e69f9e47506b525f639ed42300fc014a3e007832592448fa8e4599"
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi 0.1.19",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.1.0"
@ -202,13 +213,13 @@ dependencies = [
]
[[package]]
name = "aw-watcher"
name = "awatcher"
version = "0.1.0"
dependencies = [
"aw-client-rust",
"chrono",
"clap",
"env_logger",
"fern",
"gethostname",
"log",
"serde_json",
@ -337,6 +348,17 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "colored"
version = "1.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4ffc801dacf156c5854b9df4f425a626539c3a6ef7893cc0c5084a23f0b6c59"
dependencies = [
"atty",
"lazy_static",
"winapi",
]
[[package]]
name = "concolor-override"
version = "1.0.0"
@ -541,19 +563,6 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "env_logger"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0"
dependencies = [
"humantime",
"is-terminal",
"log",
"regex",
"termcolor",
]
[[package]]
name = "errno"
version = "0.3.0"
@ -590,6 +599,16 @@ dependencies = [
"instant",
]
[[package]]
name = "fern"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9f0c14694cbd524c8720dd69b0e3179344f04ebb5f90f2e4a440c6ea3b2f1ee"
dependencies = [
"colored",
"log",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -740,6 +759,15 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "hermit-abi"
version = "0.2.6"
@ -795,12 +823,6 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "hyper"
version = "0.14.25"

View File

@ -1,5 +1,5 @@
[package]
name = "aw-watcher"
name = "awatcher"
version = "0.1.0"
edition = "2021"
@ -14,4 +14,4 @@ serde_json = "1.0.95"
zbus = "3.11.1"
clap = "4.2.1"
log = { version = "0.4.17", features = ["std"] }
env_logger = "0.10.0"
fern = { version = "0.6.2", features = ["colored"] }

View File

@ -11,15 +11,44 @@ mod wl_kwin_idle;
mod wl_kwin_window;
use config::Config;
use fern::colors::{Color, ColoredLevelConfig};
use report_client::ReportClient;
use std::{error::Error, sync::Arc, thread};
use std::env;
use std::{error::Error, str::FromStr, sync::Arc, thread};
use wl_kwin_idle::run as run_kwin_idle;
use wl_kwin_window::run as run_kwin_active_window;
type BoxedError = Box<dyn Error>;
fn setup_logger() -> Result<(), fern::InitError> {
let log_setting = env::var("AWATCHER_LOG").unwrap_or("info".to_string());
fern::Dispatch::new()
.format(|out, message, record| {
let colors = ColoredLevelConfig::new()
.info(Color::Green)
.debug(Color::Blue)
.trace(Color::Cyan);
out.finish(format_args!(
"[{} {} {}] {}",
chrono::Utc::now().format("%Y-%m-%d %H:%M:%S%.6f"),
colors.color(record.level()),
record.target(),
message
));
})
.level(log::LevelFilter::Warn)
.level_for(
"awatcher",
FromStr::from_str(&log_setting).unwrap_or(log::LevelFilter::Info),
)
.chain(std::io::stdout())
.apply()?;
Ok(())
}
fn main() {
env_logger::init();
setup_logger().unwrap();
let client = ReportClient::new(Config::from_cli());
let client = Arc::new(client);
@ -35,9 +64,7 @@ fn main() {
let idle_handler = thread::spawn(move || run_kwin_idle(&client1));
let client2 = Arc::clone(&client);
let active_window_handler = thread::spawn(move || {
run_kwin_active_window(&client2);
});
let active_window_handler = thread::spawn(move || run_kwin_active_window(&client2));
idle_handler.join().expect("Idle thread failed");
active_window_handler

View File

@ -18,7 +18,7 @@ impl ReportClient {
Self {
config,
client: AwClient::new(&host, &port, "aw-watcher"),
client: AwClient::new(&host, &port, "awatcher"),
}
}

View File

@ -166,8 +166,6 @@ pub fn run(client: &Arc<ReportClient>) {
client.create_bucket(&bucket_name, "afkstatus").unwrap();
info!("Starting idle watcher");
let mut connection = WlEventConnection::connect().unwrap();
let mut idle_state = IdleState::new(
@ -179,6 +177,7 @@ pub fn run(client: &Arc<ReportClient>) {
);
connection.event_queue.roundtrip(&mut idle_state).unwrap();
info!("Starting idle watcher");
loop {
if let Err(e) = idle_state.run_loop(&mut connection) {
error!("Error on idle iteration {e}");

View File

@ -160,7 +160,7 @@ impl ActiveWindowInterface {
resource_class: String,
resource_name: String,
) {
debug!("Active window caption: '{caption}', class: '{resource_class}', name: '{resource_name}'");
debug!("Active window class: \"{resource_class}\", name: \"{resource_name}\", caption: \"{caption}\"");
let mut active_window = self.active_window.lock().unwrap();
active_window.caption = caption;
active_window.resource_class = resource_class;
@ -204,6 +204,7 @@ pub fn run(client: &Arc<ReportClient>) {
});
let _ = rx.recv().unwrap();
info!("Starting active window watcher");
loop {
if let Err(error) = send_heartbeat(client, &bucket_name, &active_window) {
error!("Error on sending active window heartbeat: {error}");