Event logging

This commit is contained in:
Alexey 2025-09-09 15:02:29 +03:00
commit 239484c3bc
5 changed files with 187 additions and 17 deletions

View file

@ -3,18 +3,26 @@ use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Config { pub struct Config {
/// directory, where config is located
#[serde(skip)]
pub conf_path: PathBuf,
pub log_path: PathBuf pub log_path: PathBuf
} }
impl Config { impl Config {
pub fn new() -> Self { pub fn new(conf_path: PathBuf) -> Self {
Config { log_path: PathBuf::from("./logs") } let conf_dir: PathBuf = conf_path.parent().unwrap().into();
Config { conf_path: conf_dir, log_path: PathBuf::from("./logs") }
} }
pub fn load(path: PathBuf) -> Self { pub fn load(path: PathBuf) -> Self {
if let Ok(toml_string) = std::fs::read_to_string(path) { if let Ok(toml_string) = std::fs::read_to_string(path.clone()) {
return toml::from_str(&toml_string).unwrap_or(Config::new()); let conf = toml::from_str::<Self>(&toml_string);
if let Ok(mut conf) = conf {
conf.conf_path = path.parent().unwrap().into();
return conf;
}
} }
Config::new() Config::new(path)
} }
} }

View file

@ -2,6 +2,7 @@ use config::Config;
use std::path::PathBuf; use std::path::PathBuf;
pub mod config; pub mod config;
pub mod log;
pub fn load_config() -> Config { pub fn load_config() -> Config {
if let Ok(path_str) = std::env::var("XDG_CONFIG_HOME") { if let Ok(path_str) = std::env::var("XDG_CONFIG_HOME") {
@ -10,5 +11,5 @@ pub fn load_config() -> Config {
path.push("config.toml"); path.push("config.toml");
return Config::load(path); return Config::load(path);
} }
Config::new() Config::new(PathBuf::from("./config.toml"))
} }

91
src/log.rs Normal file
View file

@ -0,0 +1,91 @@
use std::{
io::{Error, ErrorKind},
path::PathBuf
};
use serde::{Deserialize, Serialize};
use toml::value::{Date, Time};
use crate::config::Config;
#[derive(Serialize, Deserialize)]
pub struct Log {
pub date: Date,
pub events: Vec<Event>
}
impl Log {
pub fn new(date: Date) -> Self {
Log { date, events: Vec::new() }
}
pub fn load_from(config: &Config, date: Date) -> Self {
let path = Log::get_filepath(&date, config);
if let Ok(log_string) = std::fs::read_to_string(path) {
return toml::from_str(&log_string).unwrap_or(Log::new(date));
} else {
Log::new(date)
}
}
pub fn save(&self, config: &Config) -> std::io::Result<()> {
Log::try_create_log_dir(config)?;
let path = Log::get_filepath(&self.date, config);
match toml::to_string_pretty(self) {
Ok(toml_string) => std::fs::write(&path, toml_string),
Err(error) => {
return Err(Error::new(ErrorKind::Other, format!("{error}")));
}
}
}
fn get_filepath(date: &Date, config: &Config) -> PathBuf {
let mut path = Log::get_log_dir(&config);
let filename = format!("{}-{}-{}", date.day, date.month, date.year);
path.push(filename);
path.set_extension("toml");
path
}
fn get_log_dir(config: &Config) -> PathBuf {
if config.log_path.is_relative() {
let mut path = config.conf_path.clone();
path.push(&config.log_path);
return path;
} else {
return config.log_path.clone();
}
}
fn try_create_log_dir(config: &Config) -> std::io::Result<()> {
let path = Log::get_log_dir(config);
if !std::fs::exists(&path)? {
std::fs::create_dir_all(path)?
}
Ok(())
}
}
#[derive(Serialize, Deserialize)]
pub struct Event {
pub name: String,
pub start: Time,
pub end: Time,
pub finished: bool
}
impl Event {
pub fn new(name: String, start: i32, end: i32, finished: bool) -> Self {
let start = Time {
hour: (start / 3600) as u8,
minute: ((start / 3600) / 60) as u8,
second: (start % 60) as u8,
nanosecond: 0
};
let end = Time {
hour: (end / 3600) as u8,
minute: ((end % 3600) / 60) as u8,
second: (end % 60) as u8,
nanosecond: 0
};
Event { name, start, end, finished }
}
}

View file

@ -1,21 +1,65 @@
// Prevent console window in addition to Slint window in Windows release builds when, e.g., starting the app via file manager. Ignored on other platforms. // Prevent console window in addition to Slint window in Windows release builds when, e.g., starting the app via file manager. Ignored on other platforms.
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::error::Error; use std::{error::Error, sync::{Arc, Mutex}};
use aliveline::{config::Config, load_config}; use aliveline::{config::Config, load_config, log::{Event, Log}};
use chrono::Timelike; use chrono::{Datelike, Timelike};
use slint::{Model, SharedString, VecModel}; use slint::{Model, SharedString, ToSharedString, VecModel};
use toml::value::{Date as TomlDate, Time};
slint::include_modules!(); slint::include_modules!();
impl From<Event> for TimelineEvent {
fn from(event: Event) -> Self {
let start = (event.start.hour as i32) * 3600 + (event.start.minute as i32) * 60 + (event.start.second as i32);
let end = (event.end.hour as i32) * 3600 + (event.end.minute as i32) * 60 + (event.end.second as i32);
TimelineEvent { start, duration: end - start, label: event.name.to_shared_string(), finished: event.finished }
}
}
impl From<TimelineEvent> for Event {
fn from(event: TimelineEvent) -> Self {
let start = Time {
hour: (event.start / 3600) as u8,
minute: ((event.start % 3600) / 60) as u8,
second: (event.start % 60) as u8,
nanosecond: 0
};
let end = Time {
hour: start.hour + (event.duration / 3600) as u8,
minute: start.minute + ((event.duration % 3600) / 60) as u8,
second: start.second + (event.duration % 60) as u8,
nanosecond: 0
};
Event { start, end, name: event.label.to_string(), finished: event.finished }
}
}
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
let ui = AppWindow::new()?; let ui = AppWindow::new()?;
let now = chrono::Local::now(); let now = chrono::Local::now();
let offset = now.hour() * 3600 + now.minute() * 60 + now.second(); let offset = now.hour() * 3600 + now.minute() * 60 + now.second();
let date: TomlDate = TomlDate { day: now.day() as u8, month: now.month() as u8, year: now.year() as u16 };
let config: Arc<Config> = Arc::new(load_config());
let writing_log: Arc<Mutex<Log>> = Arc::new(Mutex::new(Log::load_from(&config, date)));
ui.invoke_update_record_offset(offset as i32); ui.invoke_update_record_offset(offset as i32);
ui.on_save_log({
let config = config.clone();
let log = writing_log.clone();
move || {
let log_guard = log.lock().expect("Log shouldn't be used twice");
if let Err(error) = (*log_guard).save(&config) {
eprintln!("Error occured while saving log: {error}");
}
}
});
ui.on_update_record_visible_time({ ui.on_update_record_visible_time({
let ui_weak = ui.as_weak(); let ui_weak = ui.as_weak();
move |hours_string: SharedString| { move |hours_string: SharedString| {
@ -30,32 +74,50 @@ fn main() -> Result<(), Box<dyn Error>> {
ui.on_start_new_event({ ui.on_start_new_event({
let ui_weak = ui.as_weak(); let ui_weak = ui.as_weak();
let log = writing_log.clone();
move |event_name: SharedString| { move |event_name: SharedString| {
let ui = ui_weak.unwrap(); let ui = ui_weak.unwrap();
let events_rc = ui.get_record_events(); let events_rc = ui.get_record_events();
let events = events_rc.as_any() let events = events_rc.as_any()
.downcast_ref::<VecModel<TimelineEvent>>() .downcast_ref::<VecModel<TimelineEvent>>()
.unwrap(); .unwrap();
let offset = ui.get_record_offset(); let offset = ui.get_record_offset();
events.push(TimelineEvent {
let event = TimelineEvent {
duration: 0, duration: 0,
finished: false, finished: false,
label: event_name, label: event_name,
start: offset start: offset
}); };
{
let mut log_guard = log.lock().expect("Log shouldn't be used twice");
(*log_guard).events.push(Event::from(event.clone()));
}
ui.invoke_save_log();
events.push(event);
} }
}); });
ui.on_stop_event({ ui.on_stop_event({
let ui_weak = ui.as_weak(); let ui_weak = ui.as_weak();
let log = writing_log.clone();
move || { move || {
let ui = ui_weak.unwrap(); let ui = ui_weak.unwrap();
let events_rc = ui.get_record_events(); let events_rc = ui.get_record_events();
let events = events_rc.as_any().downcast_ref::<VecModel<TimelineEvent>>().unwrap(); let events = events_rc.as_any()
.downcast_ref::<VecModel<TimelineEvent>>().unwrap();
let offset = ui.get_record_offset(); let offset = ui.get_record_offset();
let event_id = events.iter().position(|data| !data.finished).unwrap(); let event_id = events.iter()
let event = events.row_data(event_id).expect("stop-event called without unfinished events"); .position(|data| !data.finished)
.unwrap();
let event = events.row_data(event_id)
.expect("stop-event called without unfinished events");
let new_event = TimelineEvent { let new_event = TimelineEvent {
duration: offset - event.start, duration: offset - event.start,
finished: true, finished: true,
@ -63,6 +125,15 @@ fn main() -> Result<(), Box<dyn Error>> {
start: event.start start: event.start
}; };
{
let mut log_guard = log.lock().expect("Log shouldn't be used twice");
(*log_guard).events.push(Event::from(new_event.clone()));
let index = (*log_guard).events.iter().position(|data| !data.finished).unwrap();
(*log_guard).events.swap_remove(index);
}
ui.invoke_save_log();
events.set_row_data(event_id, new_event); events.set_row_data(event_id, new_event);
} }
}); });
@ -76,8 +147,6 @@ fn main() -> Result<(), Box<dyn Error>> {
} }
}); });
let config: Config = load_config();
println!("logs path: {}", config.log_path.to_str().unwrap());
ui.run()?; ui.run()?;

View file

@ -9,6 +9,7 @@ export component AppWindow inherits Window {
callback stop-event <=> record.stop-event; callback stop-event <=> record.stop-event;
callback chain-event <=> record.chain-event; callback chain-event <=> record.chain-event;
callback update-record-offset(int); callback update-record-offset(int);
callback save-log;
update-record-offset(new-offset) => { update-record-offset(new-offset) => {
record.offset = new-offset; record.offset = new-offset;