feat: logging via env_logger and log crates

This commit is contained in:
Vladimir Rubin 2025-04-24 15:18:13 +03:00
parent be93543bb3
commit 62e054c7fe
Signed by: vavakado
GPG key ID: CAB744727F36B524
4 changed files with 277 additions and 41 deletions

179
Cargo.lock generated
View file

@ -17,6 +17,15 @@ version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "android-tzdata" name = "android-tzdata"
version = "0.1.1" version = "0.1.1"
@ -32,6 +41,56 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "anstream"
version = "0.6.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9"
[[package]]
name = "anstyle-parse"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c"
dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e"
dependencies = [
"anstyle",
"once_cell",
"windows-sys 0.59.0",
]
[[package]] [[package]]
name = "async-broadcast" name = "async-broadcast"
version = "0.7.2" version = "0.7.2"
@ -277,6 +336,12 @@ dependencies = [
"windows-link", "windows-link",
] ]
[[package]]
name = "colorchoice"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]] [[package]]
name = "concurrent-queue" name = "concurrent-queue"
version = "2.5.0" version = "2.5.0"
@ -344,6 +409,29 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "env_filter"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "186e05a59d4c50738528153b83b0b0194d3a29507dfec16eccd4b342903397d0"
dependencies = [
"log",
"regex",
]
[[package]]
name = "env_logger"
version = "0.11.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13c863f0904021b108aa8b2f55046443e6b1ebde8fd4a15c399893aae4fa069f"
dependencies = [
"anstream",
"anstyle",
"env_filter",
"jiff",
"log",
]
[[package]] [[package]]
name = "equivalent" name = "equivalent"
version = "1.0.2" version = "1.0.2"
@ -357,7 +445,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "976dd42dc7e85965fe702eb8164f21f450704bdde31faefd6471dba214cb594e" checksum = "976dd42dc7e85965fe702eb8164f21f450704bdde31faefd6471dba214cb594e"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys 0.52.0", "windows-sys 0.59.0",
] ]
[[package]] [[package]]
@ -453,6 +541,8 @@ name = "hinoirisetr"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"env_logger",
"log",
"notify-rust", "notify-rust",
"tokio", "tokio",
] ]
@ -491,6 +581,36 @@ dependencies = [
"hashbrown", "hashbrown",
] ]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "jiff"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a064218214dc6a10fbae5ec5fa888d80c45d611aba169222fc272072bf7aef6"
dependencies = [
"jiff-static",
"log",
"portable-atomic",
"portable-atomic-util",
"serde",
]
[[package]]
name = "jiff-static"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "199b7932d97e325aff3a7030e141eafe7f2c6268e1d1b24859b753a627f45254"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.77" version = "0.3.77"
@ -716,6 +836,21 @@ dependencies = [
"windows-sys 0.59.0", "windows-sys 0.59.0",
] ]
[[package]]
name = "portable-atomic"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e"
[[package]]
name = "portable-atomic-util"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8a2f0d8d040d7848a709caf78912debcc3f33ee4b3cac47d73d1e1069e83507"
dependencies = [
"portable-atomic",
]
[[package]] [[package]]
name = "powerfmt" name = "powerfmt"
version = "0.2.0" version = "0.2.0"
@ -764,6 +899,35 @@ version = "5.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5"
[[package]]
name = "regex"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.24" version = "0.1.24"
@ -780,7 +944,7 @@ dependencies = [
"errno", "errno",
"libc", "libc",
"linux-raw-sys 0.4.15", "linux-raw-sys 0.4.15",
"windows-sys 0.52.0", "windows-sys 0.59.0",
] ]
[[package]] [[package]]
@ -793,7 +957,7 @@ dependencies = [
"errno", "errno",
"libc", "libc",
"linux-raw-sys 0.9.4", "linux-raw-sys 0.9.4",
"windows-sys 0.52.0", "windows-sys 0.59.0",
] ]
[[package]] [[package]]
@ -906,7 +1070,7 @@ dependencies = [
"getrandom", "getrandom",
"once_cell", "once_cell",
"rustix 1.0.5", "rustix 1.0.5",
"windows-sys 0.52.0", "windows-sys 0.59.0",
] ]
[[package]] [[package]]
@ -959,6 +1123,7 @@ dependencies = [
"libc", "libc",
"mio", "mio",
"pin-project-lite", "pin-project-lite",
"signal-hook-registry",
"socket2", "socket2",
"tokio-macros", "tokio-macros",
"windows-sys 0.52.0", "windows-sys 0.52.0",
@ -1040,6 +1205,12 @@ version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"

View file

@ -1,15 +1,21 @@
[package] [package]
name = "hinoirisetr" name = "hinoirisetr"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2024"
[dependencies] [dependencies]
chrono = "0.4.40" chrono = "0.4.40"
env_logger = "0.11.8"
log = "0.4.27"
notify-rust = "4.11.7" notify-rust = "4.11.7"
tokio = { version = "1", features = ["rt-multi-thread", "macros", "sync", "time","io-util","net" ] } tokio = { version = "1", features = ["rt-multi-thread", "macros", "sync", "time","io-util","net","signal" ] }
[profile.release] [profile.release]
strip = true strip = true
lto = true lto = true
codegen-units = 1 codegen-units = 1
opt-level = "s" opt-level = "s"
panic = "abort"
[profile.release.package."*"]
opt-level = "z"

View file

@ -1,9 +1,12 @@
//! hinoirisetr library //! hinoirisetr library
//! Contains core logic for computing temperature and gamma and applying settings. //! Contains core logic for computing temperature and gamma and applying settings.
use chrono::{DateTime, Local, Timelike};
use std::process::Command; use std::process::Command;
use chrono::{DateTime, Local, Timelike};
use log::{debug, trace};
// Constants for colors and timings // Constants for colors and timings
pub const TEMP_DAY: i32 = 6500; pub const TEMP_DAY: i32 = 6500;
pub const TEMP_NIGHT: i32 = 2700; pub const TEMP_NIGHT: i32 = 2700;
@ -13,49 +16,60 @@ pub const GAMMA_NIGHT: i32 = 95;
pub const SUNSET_START: u32 = 19; pub const SUNSET_START: u32 = 19;
pub const SUNSET_END: u32 = 22; pub const SUNSET_END: u32 = 22;
pub const SUNRISE_START: u32 = 4; pub const SUNRISE_START: u32 = 14;
pub const SUNRISE_END: u32 = 7; pub const SUNRISE_END: u32 = 17;
/// Linearly interpolate between start and end by factor [0.0, 1.0] /// Linearly interpolate between start and end by factor [0.0, 1.0]
pub fn interpolate(start: i32, end: i32, factor: f64) -> i32 { pub fn interpolate(start: i32, end: i32, factor: f64) -> i32 {
trace!("interpolate({start}, {end}, {factor})");
(start as f64 + (end - start) as f64 * factor).round() as i32 (start as f64 + (end - start) as f64 * factor).round() as i32
} }
/// Compute current temperature and gamma based on provided time /// Compute current temperature and gamma based on provided time
pub fn compute_settings(now: DateTime<Local>) -> (i32, i32) { pub fn compute_settings(now: DateTime<Local>) -> (i32, i32) {
trace!("compute_settings({now:?})");
let time_in_hours = now.hour() as f64 + now.minute() as f64 / 60.0; let time_in_hours = now.hour() as f64 + now.minute() as f64 / 60.0;
trace!("time_in_hours: {time_in_hours}");
if (time_in_hours >= SUNSET_START as f64) && (time_in_hours <= SUNSET_END as f64) { if (time_in_hours >= SUNSET_START as f64) && (time_in_hours <= SUNSET_END as f64) {
let factor = ((time_in_hours - SUNSET_START as f64) trace!("time_in_hours is within sunset");
/ (SUNSET_END - SUNSET_START) as f64) let factor = ((time_in_hours - SUNSET_START as f64) / (SUNSET_END - SUNSET_START) as f64)
.clamp(0.0, 1.0); .clamp(0.0, 1.0);
( (
interpolate(TEMP_DAY, TEMP_NIGHT, factor), interpolate(TEMP_DAY, TEMP_NIGHT, factor),
interpolate(GAMMA_DAY, GAMMA_NIGHT, factor), interpolate(GAMMA_DAY, GAMMA_NIGHT, factor),
) )
} else if (time_in_hours >= SUNRISE_START as f64) && (time_in_hours <= SUNRISE_END as f64) { } else if (time_in_hours >= SUNRISE_START as f64) && (time_in_hours <= SUNRISE_END as f64) {
trace!("time_in_hours is within sunrise");
let factor = 1.0 let factor = 1.0
- ((time_in_hours - SUNRISE_START as f64) - ((time_in_hours - SUNRISE_START as f64) / (SUNRISE_END - SUNRISE_START) as f64)
/ (SUNRISE_END - SUNRISE_START) as f64)
.clamp(0.0, 1.0); .clamp(0.0, 1.0);
( (
interpolate(TEMP_DAY, TEMP_NIGHT, factor), interpolate(TEMP_DAY, TEMP_NIGHT, factor),
interpolate(GAMMA_DAY, GAMMA_NIGHT, factor), interpolate(GAMMA_DAY, GAMMA_NIGHT, factor),
) )
} else if time_in_hours > SUNSET_END as f64 || time_in_hours < SUNRISE_START as f64 { } else if time_in_hours > SUNSET_END as f64 || time_in_hours < SUNRISE_START as f64 {
trace!("time_in_hours is within night");
(TEMP_NIGHT, GAMMA_NIGHT) (TEMP_NIGHT, GAMMA_NIGHT)
} else { } else {
trace!("time_in_hours is within day");
(TEMP_DAY, GAMMA_DAY) (TEMP_DAY, GAMMA_DAY)
} }
} }
/// Apply given temperature (Kelvin) and gamma (%) via hyprctl commands /// Apply given temperature (Kelvin) and gamma (%) via hyprctl commands
pub fn apply_settings(temp: i32, gamma: i32) { pub fn apply_settings(temp: i32, gamma: i32) {
trace!("apply_settings({temp}, {gamma})");
debug!("applying temperature: {temp}");
debug!("applying gamma: {gamma}");
let _ = Command::new("hyprctl") let _ = Command::new("hyprctl")
.args(["hyprsunset", "temperature", &temp.to_string()]) .args(["hyprsunset", "temperature", &temp.to_string()])
.output(); .output();
trace!("hyprctl hyprsunset temperature {temp}");
let _ = Command::new("hyprctl") let _ = Command::new("hyprctl")
.args(["hyprsunset", "gamma", &gamma.to_string()]) .args(["hyprsunset", "gamma", &gamma.to_string()])
.output(); .output();
trace!("hyprctl hyprsunset gamma {gamma}");
} }

View file

@ -1,50 +1,58 @@
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use chrono::Local; use chrono::Local;
use hinoirisetr::{apply_settings, compute_settings}; use hinoirisetr::{apply_settings, compute_settings};
use log::{debug, error, info, trace, warn};
use notify_rust::Notification; use notify_rust::Notification;
use std::sync::{ use tokio::io::{AsyncBufReadExt, BufReader};
atomic::{AtomicBool, Ordering}, use tokio::net::UnixListener;
Arc, use tokio::signal::unix::{SignalKind, signal};
}; use tokio::sync::Notify;
use std::time::Duration; use tokio::time::sleep;
use tokio::{
io::{AsyncBufReadExt, BufReader},
net::UnixListener,
sync::Notify,
time::sleep,
};
const SOCKET_PATH: &str = "/tmp/hinoirisetr.sock"; const SOCKET_PATH: &str = "/tmp/hinoirisetr.sock";
const NOTIFICATION_TIMEOUT: u32 = 5000; const NOTIFICATION_TIMEOUT: u32 = 5000;
async fn socket_server(disabled: Arc<AtomicBool>, notify: Arc<Notify>) { async fn socket_server(disabled: Arc<AtomicBool>, notify: Arc<Notify>) {
let _ = std::fs::remove_file(SOCKET_PATH);
let listener = UnixListener::bind(SOCKET_PATH).expect("Failed to bind socket"); let listener = UnixListener::bind(SOCKET_PATH).expect("Failed to bind socket");
trace!("socket server bound");
loop { loop {
let (stream, _) = listener.accept().await.unwrap(); let (stream, _) = listener.accept().await.unwrap();
let mut lines = BufReader::new(stream).lines(); let mut lines = BufReader::new(stream).lines();
trace!("socket server accepted connection");
if let Ok(Some(line)) = lines.next_line().await { if let Ok(Some(line)) = lines.next_line().await {
match line.trim() { match line.trim() {
"disable" => { "disable" => {
trace!("disable dispatched");
disabled.store(true, Ordering::SeqCst); disabled.store(true, Ordering::SeqCst);
notify.notify_one(); notify.notify_one();
debug!("dimming is disabled");
} }
"enable" => { "enable" => {
trace!("enable dispatched");
disabled.store(false, Ordering::SeqCst); disabled.store(false, Ordering::SeqCst);
notify.notify_one(); notify.notify_one();
debug!("dimming is enabled");
} }
"toggle" => { "toggle" => {
trace!("toggle dispatched");
let now = !disabled.load(Ordering::SeqCst); let now = !disabled.load(Ordering::SeqCst);
disabled.store(now, Ordering::SeqCst); disabled.store(now, Ordering::SeqCst);
notify.notify_one(); notify.notify_one();
debug!("dimming is {}", if now { "enabled" } else { "disabled" });
} }
"status" => { "status" => {
trace!("status dispatched");
// compute current temp/gamma // compute current temp/gamma
let now = Local::now(); let now = Local::now();
let (cur_temp, cur_gamma) = compute_settings(now); let (cur_temp, cur_gamma) = compute_settings(now);
println!( info!(
"dimming is {} — temp: {}K, gamma: {}%", "dimming is {} — temp: {}K, gamma: {}%",
if disabled.load(Ordering::SeqCst) { if disabled.load(Ordering::SeqCst) {
"disabled" "disabled"
@ -56,13 +64,14 @@ async fn socket_server(disabled: Arc<AtomicBool>, notify: Arc<Notify>) {
); );
} }
"status_notify" => { "status_notify" => {
trace!("status_notify dispatched");
let now = Local::now(); let now = Local::now();
let (cur_temp, cur_gamma) = compute_settings(now); let (cur_temp, cur_gamma) = compute_settings(now);
let body = if disabled.load(Ordering::SeqCst) { let body = if disabled.load(Ordering::SeqCst) {
"disabled".to_string() "disabled".to_string()
} else { } else {
format!("temp: {}K, gamma: {}%", cur_temp, cur_gamma) format!("temp: {cur_temp}K, gamma: {cur_gamma}%")
}; };
match Notification::new() match Notification::new()
@ -74,11 +83,11 @@ async fn socket_server(disabled: Arc<AtomicBool>, notify: Arc<Notify>) {
{ {
Ok(_) => {} Ok(_) => {}
Err(err) => { Err(err) => {
eprintln!("Erorr occured while sending a notification: {}", err) error!("Erorr occured while sending a notification: {err}")
} }
} }
} }
_ => eprintln!("unknown command: {}", line.trim()), _ => error!("unknown command: {}", line.trim()),
} }
} }
} }
@ -86,10 +95,24 @@ async fn socket_server(disabled: Arc<AtomicBool>, notify: Arc<Notify>) {
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
env_logger::init();
log::info!("starting the daemon");
let disabled = Arc::new(AtomicBool::new(false)); let disabled = Arc::new(AtomicBool::new(false));
let notify = Arc::new(Notify::new()); let notify = Arc::new(Notify::new());
println!("dimming is enabled"); if std::path::Path::new(SOCKET_PATH).exists() {
match std::os::unix::net::UnixStream::connect(SOCKET_PATH) {
Ok(_) => {
error!("Another instance is running.");
std::process::exit(1);
}
Err(_) => {
warn!("Stale socket found, removing.");
let _ = std::fs::remove_file(SOCKET_PATH);
}
}
}
// Spawn control socket server // Spawn control socket server
{ {
@ -100,19 +123,41 @@ async fn main() {
}); });
} }
loop { // Signal handling
if disabled.load(Ordering::SeqCst) { let mut sigint = signal(SignalKind::interrupt()).unwrap();
apply_settings(hinoirisetr::TEMP_DAY, hinoirisetr::GAMMA_DAY); let mut sigterm = signal(SignalKind::terminate()).unwrap();
} else {
let now = Local::now();
let (temp, gamma) = compute_settings(now);
apply_settings(temp, gamma); // Main loop with shutdown support
} tokio::select! {
_ = async {
loop {
if disabled.load(Ordering::SeqCst) {
apply_settings(hinoirisetr::TEMP_DAY, hinoirisetr::GAMMA_DAY);
} else {
let now = Local::now();
let (temp, gamma) = compute_settings(now);
apply_settings(temp, gamma);
}
tokio::select! { tokio::select! {
_ = sleep(Duration::from_secs(300)) => {}, _ = sleep(Duration::from_secs(300)) => {trace!("main loop tick");},
_ = notify.notified() => {}, _ = notify.notified() => {trace!("main loop woke up via notify");},
}
}
} => {},
_ = sigint.recv() => {
info!("Received SIGINT, shutting down...");
},
_ = sigterm.recv() => {
info!("Received SIGTERM, shutting down...");
},
}
// Cleanup the socket file on shutdown
if std::path::Path::new(SOCKET_PATH).exists() {
match std::fs::remove_file(SOCKET_PATH) {
Ok(_) => info!("Socket file {SOCKET_PATH} removed."),
Err(e) => warn!("Failed to remove socket file {SOCKET_PATH}: {e}"),
} }
} }
} }