use simplelog to log messages

Signed-off-by: Fabrizio Iannetti <fabrizio.iannetti@gmail.com>
This commit is contained in:
Fabrizio Iannetti 2024-01-28 10:20:02 +01:00
parent e7a2ee9d80
commit ae9abb7768
4 changed files with 814 additions and 431 deletions

1172
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -15,6 +15,8 @@ lttng-ust = { version = "0.1.0", optional = true }
icalendar = "0.15.4" icalendar = "0.15.4"
directories = "5.0.1" directories = "5.0.1"
clap = { version = "4.3.11", features = ["derive"] } clap = { version = "4.3.11", features = ["derive"] }
log = { version = "0.4", features = ["std", "serde"] }
simplelog = { version = "^0.12.0", default-features = false }
[build-dependencies] [build-dependencies]
#lttng-ust-generate = "0.1.0" #lttng-ust-generate = "0.1.0"

View File

@ -3,31 +3,36 @@ mod ui;
mod model; mod model;
use ui::calendar; use ui::calendar;
use model::events::EventsCollection; use model::{
use model::ical_bridge::load_calendar; events::EventsCollection,
ical_bridge::load_calendar,
};
use chrono::{Datelike, NaiveDate, Months, Utc}; use chrono::{Datelike, NaiveDate, Months, Utc};
use calendar::{CalendarParams, CalendarView }; use calendar::{CalendarParams, CalendarView };
use calendar::CalendarViewMode as ViewMode; use calendar::CalendarViewMode as ViewMode;
use std::path; use std::path;
use clap;
use clap::Parser; use clap::Parser;
use log::info;
use simplelog::{SimpleLogger, Config};
use iced::{ use iced::{
alignment, alignment,
executor, executor,
Alignment, Application, Command, Alignment,
Element, Length, Settings, Application,
Command,
Element,
Length,
Settings,
widget::{
Column,
Row,
Container,
Button,
Text,
pick_list,
},
theme,
}; };
use iced::widget::{
Column, Row,
Container,
Button, Text,
pick_list,
};
//use iced::button;
use iced::theme;
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
extern crate lttng_ust; extern crate lttng_ust;
@ -52,12 +57,21 @@ struct CliArgs {
} }
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
set_up_logger();
let args = CliArgs::parse(); let args = CliArgs::parse();
let calendar_files = args.files.iter().map(|arg| path::PathBuf::from(arg)).collect(); let calendar_files = args.files.iter().map(|arg| {
//let calendar_files = vec![path::PathBuf::from("/home/fab/calendario.ics")]; info!("CalFile: {}", arg);
path::PathBuf::from(arg)
}).collect();
CalendarApp::run(Settings::with_flags(CalendarFlags {calendar_paths: calendar_files})) CalendarApp::run(Settings::with_flags(CalendarFlags {calendar_paths: calendar_files}))
} }
fn set_up_logger() {
SimpleLogger::init(log::LevelFilter::Info, Config::default()).unwrap();
}
#[derive(Default)] #[derive(Default)]
struct CalendarApp { struct CalendarApp {
month: NaiveDate, month: NaiveDate,
@ -149,15 +163,19 @@ impl Controls {
fn get_msg_next(&self) -> Message { fn get_msg_next(&self) -> Message {
match self.mode { match self.mode {
Some(ViewMode::Week) => Message::NextWeek,
Some(ViewMode::Month) => Message::NextMonth, Some(ViewMode::Month) => Message::NextMonth,
_ => Message::NextYear Some(ViewMode::Year) => Message::NextYear,
None => todo!(),
} }
} }
fn get_msg_prev(&self) -> Message { fn get_msg_prev(&self) -> Message {
match self.mode { match self.mode {
Some(ViewMode::Week) => Message::PrevWeek,
Some(ViewMode::Month) => Message::PrevMonth, Some(ViewMode::Month) => Message::PrevMonth,
_ => Message::PrevYear Some(ViewMode::Year) => Message::PrevYear,
None => todo!(),
} }
} }
} }

View File

@ -3,6 +3,7 @@ use icalendar::{Component, DatePerhapsTime, CalendarDateTime, CalendarComponent}
use chrono::NaiveDate; use chrono::NaiveDate;
use crate::EventsCollection; use crate::EventsCollection;
use std::fs; use std::fs;
use log::warn;
fn cdt_as_date(cdt: &CalendarDateTime) -> NaiveDate { fn cdt_as_date(cdt: &CalendarDateTime) -> NaiveDate {
match cdt { match cdt {
@ -25,12 +26,16 @@ fn create_event(events: &mut EventsCollection, event: &icalendar::Event) {
pub fn load_calendar(calendar_path: &std::path::Path, events: &mut EventsCollection) { pub fn load_calendar(calendar_path: &std::path::Path, events: &mut EventsCollection) {
let mut calendar = icalendar::Calendar::new(); let mut calendar = icalendar::Calendar::new();
let contents = fs::read_to_string(calendar_path)
.expect("Could not read the calendar"); let contents = match fs::read_to_string(calendar_path) {
Ok(file_content) => file_content,
Err(e) => { warn!("Could not read the calendar: {}", e); return; },
};
let res = icalendar::parser::read_calendar(&contents); let res = icalendar::parser::read_calendar(&contents);
match res { match res {
Ok(parsed_calendar) => { calendar.extend(parsed_calendar.components); } Ok(parsed_calendar) => { calendar.extend(parsed_calendar.components); },
Err(e) => println!("{}", e) Err(e) => { warn!("Error parsing calendar {}", e); return; }
} }
for cc in calendar.iter() { for cc in calendar.iter() {