diff --git a/crates/librqbit/src/lib.rs b/crates/librqbit/src/lib.rs index ba90652..76e6e25 100644 --- a/crates/librqbit/src/lib.rs +++ b/crates/librqbit/src/lib.rs @@ -53,3 +53,8 @@ pub use clone_to_owned::CloneToOwned; pub use librqbit_core::magnet::*; pub use librqbit_core::peer_id::*; pub use librqbit_core::torrent_metainfo::*; + +/// The cargo version of librqbit. +pub fn version() -> &'static str { + env!("CARGO_PKG_VERSION") +} diff --git a/crates/rqbit/src/main.rs b/crates/rqbit/src/main.rs index c81f50d..95a7277 100644 --- a/crates/rqbit/src/main.rs +++ b/crates/rqbit/src/main.rs @@ -178,7 +178,7 @@ enum SubCommand { Download(DownloadOpts), } -// Iint logging and make a channel to send new RUST_LOG values to. +// Init logging and make a channel to send new RUST_LOG values to. fn init_logging(opts: &Opts) -> tokio::sync::mpsc::UnboundedSender { let default_rust_log = match opts.log_level.as_ref() { Some(level) => match level { diff --git a/desktop/src-tauri/Cargo.lock b/desktop/src-tauri/Cargo.lock index 4c699aa..0c7c4be 100644 --- a/desktop/src-tauri/Cargo.lock +++ b/desktop/src-tauri/Cargo.lock @@ -3008,7 +3008,7 @@ dependencies = [ [[package]] name = "rqbit-desktop" -version = "0.0.0" +version = "5.0.0-beta.1" dependencies = [ "anyhow", "base64 0.21.5", @@ -3020,6 +3020,7 @@ dependencies = [ "tauri", "tauri-build", "tokio", + "tracing", "tracing-subscriber", ] diff --git a/desktop/src-tauri/Cargo.toml b/desktop/src-tauri/Cargo.toml index 8c4ad38..5a1f860 100644 --- a/desktop/src-tauri/Cargo.toml +++ b/desktop/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rqbit-desktop" -version = "0.0.0" +version = "5.0.0-beta.1" description = "rqbit torrent client" authors = ["you"] license = "" @@ -22,7 +22,8 @@ anyhow = "1.0.75" base64 = "0.21.5" http = "1.0.0" directories = "5.0.1" -tracing-subscriber = "0.3.18" +tracing-subscriber = {version = "0.3.18", features = ["env-filter"] } +tracing = "0.1" [features] # this feature is used for production builds or when `devPath` points to the filesystem diff --git a/desktop/src-tauri/src/main.rs b/desktop/src-tauri/src/main.rs index 2b6c838..862678e 100644 --- a/desktop/src-tauri/src/main.rs +++ b/desktop/src-tauri/src/main.rs @@ -8,8 +8,9 @@ use librqbit::{ ApiAddTorrentResponse, EmptyJsonResponse, TorrentDetailsResponse, TorrentListResponse, TorrentStats, }, - AddTorrent, AddTorrentOptions, Api, ApiError, Session, SessionOptions, + librqbit_spawn, AddTorrent, AddTorrentOptions, Api, ApiError, Session, SessionOptions, }; +use tracing::error_span; struct State { api: Api, @@ -97,7 +98,44 @@ async fn torrent_action_start( state.api.api_torrent_action_start(id) } +#[tauri::command] +fn get_version() -> &'static str { + env!("CARGO_PKG_VERSION") +} + +fn init_logging() -> tokio::sync::mpsc::UnboundedSender { + use tracing_subscriber::{fmt, prelude::*, EnvFilter}; + let (stderr_filter, reload_stderr_filter) = + tracing_subscriber::reload::Layer::new(EnvFilter::builder().parse("info").unwrap()); + + let layered = tracing_subscriber::registry().with(fmt::layer().with_filter(stderr_filter)); + layered.init(); + + let (reload_tx, mut reload_rx) = tokio::sync::mpsc::unbounded_channel::(); + librqbit_spawn( + "fmt_filter_reloader", + error_span!("fmt_filter_reloader"), + async move { + while let Some(rust_log) = reload_rx.recv().await { + let stderr_env_filter = match EnvFilter::builder().parse(&rust_log) { + Ok(f) => f, + Err(e) => { + eprintln!("can't parse env filter {:?}: {:#?}", rust_log, e); + continue; + } + }; + eprintln!("setting RUST_LOG to {:?}", rust_log); + let _ = reload_stderr_filter.reload(stderr_env_filter); + } + Ok(()) + }, + ); + reload_tx +} + async fn start_session() { + let rust_log_reload_tx = init_logging(); + tauri::async_runtime::set(tokio::runtime::Handle::current()); let download_folder = directories::UserDirs::new() @@ -119,7 +157,14 @@ async fn start_session() { .await .expect("couldn't set up librqbit session"); - let api = Api::new(session, None); + let api = Api::new(session.clone(), None); + + librqbit_spawn( + "http api", + error_span!("http_api"), + librqbit::http_api::HttpApi::new(session, Some(rust_log_reload_tx)) + .make_http_api_and_run("127.0.0.1:3000".parse().unwrap(), false), + ); tauri::Builder::default() .manage(State { api }) @@ -133,13 +178,13 @@ async fn start_session() { torrent_action_forget, torrent_action_start, torrent_create_from_base64_file, + get_version ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); } fn main() { - tracing_subscriber::fmt::init(); tokio::runtime::Builder::new_multi_thread() .enable_all() .build() diff --git a/desktop/src/main.tsx b/desktop/src/main.tsx index 9afb536..a67f5ac 100644 --- a/desktop/src/main.tsx +++ b/desktop/src/main.tsx @@ -2,13 +2,18 @@ import { StrictMode } from "react"; import ReactDOM from 'react-dom/client'; import { APIContext, RqbitWebUI } from "./rqbit-webui-src/rqbit-web"; import { API } from "./api"; +import { invoke } from "@tauri-apps/api"; + +let version = invoke("get_version").then((version) => { + ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render( + + + + + + ); +}); + -ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render( - - - - - -);