Logging and Notifications
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
#![allow(clippy::needless_return)]
|
||||
|
||||
mod app;
|
||||
@@ -17,7 +18,9 @@ use oauth::{start_code_listener, start_code_to_token};
|
||||
use pipe::Pipe;
|
||||
use state_machine::Event;
|
||||
use tokio::task::JoinSet;
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
pub static AVAM_APP_ID: &str = "AvamToast-ECEB71694A5E6105";
|
||||
pub static BASE_URL: &str = "https://avam.avii.nl";
|
||||
pub static PROJECT_NAME: &str = "Avii's Virtual Airline Manager";
|
||||
pub static COPYRIGHT: &str = "Avii's Virtual Airline Manager © 2024";
|
||||
@@ -35,6 +38,10 @@ pub struct Arguments {
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), anyhow::Error> {
|
||||
dotenvy::dotenv().ok();
|
||||
|
||||
init_logging()?;
|
||||
|
||||
let (event_sender, event_receiver) = tokio::sync::broadcast::channel(1);
|
||||
let args = Arguments::parse();
|
||||
|
||||
@@ -43,6 +50,7 @@ async fn main() -> Result<(), anyhow::Error> {
|
||||
}
|
||||
|
||||
register_url_scheme()?;
|
||||
register_notification_handler()?;
|
||||
|
||||
let config = Config::new()?;
|
||||
|
||||
@@ -52,7 +60,7 @@ async fn main() -> Result<(), anyhow::Error> {
|
||||
let sender = event_sender.clone();
|
||||
let mut ctrl_c_counter = 0;
|
||||
ctrlc::set_handler(move || {
|
||||
println!("CTRL_C: Quit singal sent");
|
||||
tracing::info!("CTRL_C: Quit singal sent");
|
||||
ctrl_c_counter += 1;
|
||||
if ctrl_c_counter >= 3 {
|
||||
let _ = unregister_url_scheme();
|
||||
@@ -61,7 +69,7 @@ async fn main() -> Result<(), anyhow::Error> {
|
||||
}
|
||||
|
||||
if let Err(e) = sender.send(Event::Quit) {
|
||||
println!("{:#?}", e)
|
||||
tracing::error!("{:#?}", e)
|
||||
};
|
||||
})?;
|
||||
|
||||
@@ -86,7 +94,7 @@ async fn main() -> Result<(), anyhow::Error> {
|
||||
let c = config.clone();
|
||||
let sender = event_sender.clone();
|
||||
let receiver = event_receiver.resubscribe();
|
||||
start_tray_icon(c, sender, receiver).await?;
|
||||
app::start(c, sender, receiver).await?;
|
||||
|
||||
// Wait for everything to finish
|
||||
while let Some(result) = futures.join_next().await {
|
||||
@@ -97,28 +105,12 @@ async fn main() -> Result<(), anyhow::Error> {
|
||||
|
||||
// Cleanup
|
||||
unregister_url_scheme()?;
|
||||
unregister_notification_handler()?;
|
||||
Lock::unlock();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
use app::App;
|
||||
use tokio::sync::broadcast::{Receiver, Sender};
|
||||
use winit::event_loop::EventLoop;
|
||||
async fn start_tray_icon(
|
||||
config: Config,
|
||||
sender: Sender<Event>,
|
||||
receiver: Receiver<Event>,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
let mut app = App::new(config, sender, receiver)?;
|
||||
let event_loop = EventLoop::new()?;
|
||||
|
||||
event_loop.run_app(&mut app)?;
|
||||
println!("EventLoop Shutdonw");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
//
|
||||
@@ -178,3 +170,72 @@ fn unregister_url_scheme() -> Result<(), anyhow::Error> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn register_notification_handler() -> Result<(), anyhow::Error> {
|
||||
use winreg::enums::*;
|
||||
use winreg::RegKey;
|
||||
|
||||
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
|
||||
let avam_schema_root = hkcu.create_subkey(format!(
|
||||
"Software\\Classes\\AppUserModelId\\{}",
|
||||
AVAM_APP_ID
|
||||
))?;
|
||||
let current_exec = std::env::current_exe()?;
|
||||
let mut icon = current_exec.parent().unwrap().to_path_buf();
|
||||
icon.push("icon.png");
|
||||
|
||||
avam_schema_root
|
||||
.0
|
||||
.set_value("DisplayName", &crate::PROJECT_NAME)?;
|
||||
avam_schema_root.0.set_value("IconBackgroundColor", &"0")?;
|
||||
avam_schema_root
|
||||
.0
|
||||
.set_value("IconUri", &icon.to_str().unwrap())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn unregister_notification_handler() -> Result<(), anyhow::Error> {
|
||||
use winreg::enums::*;
|
||||
use winreg::RegKey;
|
||||
|
||||
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
|
||||
hkcu.delete_subkey_all(format!(
|
||||
"Software\\Classes\\AppUserModelId\\{}",
|
||||
AVAM_APP_ID
|
||||
))
|
||||
.ok();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn init_logging() -> Result<(), anyhow::Error> {
|
||||
#[cfg(not(debug_assertions))]
|
||||
use dirs::Dirs;
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
use std::{
|
||||
fs::{self, File},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
let log_file = Dirs::get_log_file()?;
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
if !log_file.exists() {
|
||||
fs::write(&log_file, "")?;
|
||||
}
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
let file = File::options().append(true).open(&log_file)?;
|
||||
|
||||
let fmt = tracing_subscriber::fmt::layer();
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
let fmt = fmt.with_ansi(false).with_writer(Arc::new(file));
|
||||
|
||||
tracing_subscriber::registry().with(fmt).init();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user