notedeck.rs (3146B)
1 #![warn(clippy::all, rust_2018_idioms)] 2 #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release 3 use notedeck::{ 4 app_creation::generate_native_options, 5 storage::{DataPath, DataPathType}, 6 Damus, 7 }; 8 use std::{path::PathBuf, str::FromStr}; 9 10 use tracing_subscriber::EnvFilter; 11 12 // Entry point for wasm 13 //#[cfg(target_arch = "wasm32")] 14 //use wasm_bindgen::prelude::*; 15 16 fn setup_logging(path: &DataPath) { 17 #[allow(unused_variables)] // need guard to live for lifetime of program 18 let (maybe_non_blocking, maybe_guard) = { 19 let log_path = path.path(DataPathType::Log); 20 // Setup logging to file 21 22 use tracing_appender::{ 23 non_blocking, 24 rolling::{RollingFileAppender, Rotation}, 25 }; 26 27 let file_appender = RollingFileAppender::new( 28 Rotation::DAILY, 29 log_path, 30 format!("notedeck-{}.log", env!("CARGO_PKG_VERSION")), 31 ); 32 33 let (non_blocking, _guard) = non_blocking(file_appender); 34 35 (Some(non_blocking), Some(_guard)) 36 }; 37 38 // Log to stdout (if you run with `RUST_LOG=debug`). 39 if let Some(non_blocking_writer) = maybe_non_blocking { 40 use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt}; 41 42 let console_layer = fmt::layer().with_target(true).with_writer(std::io::stdout); 43 44 // Create the file layer (writes to the file) 45 let file_layer = fmt::layer() 46 .with_ansi(false) 47 .with_writer(non_blocking_writer); 48 49 let env_filter = 50 EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")); 51 52 // Set up the subscriber to combine both layers 53 tracing_subscriber::registry() 54 .with(console_layer) 55 .with(file_layer) 56 .with(env_filter) 57 .init(); 58 } else { 59 tracing_subscriber::fmt() 60 .with_env_filter(EnvFilter::from_default_env()) 61 .init(); 62 } 63 } 64 65 // Desktop 66 #[cfg(not(target_arch = "wasm32"))] 67 #[tokio::main] 68 async fn main() { 69 let base_path = DataPath::default_base().unwrap_or(PathBuf::from_str(".").unwrap()); 70 let path = DataPath::new(&base_path); 71 72 setup_logging(&path); 73 74 let _res = eframe::run_native( 75 "Damus Notedeck", 76 generate_native_options(path), 77 Box::new(|cc| { 78 Ok(Box::new(Damus::new( 79 &cc.egui_ctx, 80 base_path, 81 std::env::args().collect(), 82 ))) 83 }), 84 ); 85 } 86 87 #[cfg(target_arch = "wasm32")] 88 pub fn main() { 89 // Make sure panics are logged using `console.error`. 90 console_error_panic_hook::set_once(); 91 92 // Redirect tracing to console.log and friends: 93 tracing_wasm::set_as_global_default(); 94 95 wasm_bindgen_futures::spawn_local(async { 96 let web_options = eframe::WebOptions::default(); 97 eframe::start_web( 98 "the_canvas_id", // hardcode it 99 web_options, 100 Box::new(|cc| Box::new(Damus::new(cc, "."))), 101 ) 102 .await 103 .expect("failed to start eframe"); 104 }); 105 }