Skip to content

Commit

Permalink
Add an HTTP API endpoint + UI widgets to stream logs (#49)
Browse files Browse the repository at this point in the history
* Added JSON logs to Desktop

* Move logging config into librqbit for reuse

* Log printer now available in both Desktop and Web UI

* Fix JS type error
  • Loading branch information
ikatson authored Dec 9, 2023
1 parent 9385524 commit 2017c5e
Show file tree
Hide file tree
Showing 21 changed files with 463 additions and 334 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer"
}
}
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions crates/dht/src/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::time::Duration;
use tokio_util::sync::CancellationToken;

use anyhow::Context;
use tracing::{debug, error, error_span, info, trace, warn};
use tracing::{error, error_span, info, trace, warn};

use crate::peer_store::PeerStore;
use crate::routing_table::RoutingTable;
Expand Down Expand Up @@ -86,8 +86,8 @@ impl PersistentDht {
};

info!(
"will store DHT routing table to {:?} periodically",
&config_filename
filename=?config_filename,
"will store DHT routing table periodically",
);

if let Some(parent) = config_filename.parent() {
Expand All @@ -100,13 +100,14 @@ impl PersistentDht {
let reader = BufReader::new(dht_json);
match serde_json::from_reader::<_, DhtSerialize<RoutingTable, PeerStore>>(reader) {
Ok(r) => {
info!("loaded DHT routing table from {:?}", &config_filename);
info!(filename=?config_filename, "loaded DHT routing table from");
Some(r)
}
Err(e) => {
warn!(
"cannot deserialize routing table from file {:?}: {:#}",
&config_filename, e
filename=?config_filename,
"cannot deserialize routing table: {:#}",
e
);
None
}
Expand Down Expand Up @@ -152,9 +153,9 @@ impl PersistentDht {
tokio::time::sleep(dump_interval).await;

match dump_dht(&dht, &config_filename, &tempfile_name) {
Ok(_) => debug!("dumped DHT to {:?}", &config_filename),
Ok(_) => trace!(filename=?config_filename, "dumped DHT"),
Err(e) => {
error!("error dumping DHT to {:?}: {:#}", &config_filename, e)
error!(filename=?config_filename, "error dumping DHT: {:#}", e)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/librqbit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ rand = "0.8"
openssl = {version="0.10", optional=true}
crypto-hash = {version="0.3", optional=true}
sha1 = {version = "0.10", optional=true}
tracing-subscriber = {version = "0.3", default-features = false}
tracing-subscriber = {version = "0.3", default-features = false, features = ["json"]}

uuid = {version = "1.2", features = ["v4"]}
futures = "0.3"
Expand Down
7 changes: 4 additions & 3 deletions crates/librqbit/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use crate::{
torrent_state::{
peer::stats::snapshot::{PeerStatsFilter, PeerStatsSnapshot},
ManagedTorrentHandle,
}, log_subscriber::LineBroadcast,
},
tracing_subscriber_config_utils::LineBroadcast,
};

pub use crate::torrent_state::stats::{LiveStats, TorrentStats};
Expand All @@ -39,12 +40,12 @@ impl Api {
pub fn new(
session: Arc<Session>,
rust_log_reload_tx: Option<UnboundedSender<String>>,
line_broadcast: Option<LineBroadcast>
line_broadcast: Option<LineBroadcast>,
) -> Self {
Self {
session,
rust_log_reload_tx,
line_broadcast
line_broadcast,
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/librqbit/src/http_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,12 @@ impl HttpApi {
};

let app = app
.layer(cors_layer)
.layer(cors_layer)
.layer(tower_http::trace::TraceLayer::new_for_http())
.with_state(state)
.into_make_service();

info!("starting HTTP server on {}", addr);
info!(%addr, "starting HTTP server");

use tokio::net::TcpListener;
let listener = TcpListener::bind(&addr)
Expand Down
2 changes: 1 addition & 1 deletion crates/librqbit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ mod dht_utils;
mod file_ops;
pub mod http_api;
pub mod http_api_client;
pub mod log_subscriber;
mod peer_connection;
mod peer_info_reader;
mod session;
mod spawn_utils;
mod torrent_state;
pub mod tracing_subscriber_config_utils;
mod tracker_comms;
mod type_aliases;

Expand Down
47 changes: 0 additions & 47 deletions crates/librqbit/src/log_subscriber.rs

This file was deleted.

14 changes: 7 additions & 7 deletions crates/librqbit/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ impl Session {

std::fs::rename(&tmp_filename, &self.persistence_filename)
.context("error renaming persistence file")?;
debug!("wrote persistence to {:?}", &self.persistence_filename);
trace!(filename=?self.persistence_filename, "wrote persistence");
Ok(())
}

Expand Down Expand Up @@ -779,7 +779,7 @@ impl Session {
})
.collect();

debug!("querying DHT for {:?}", info_hash);
debug!(?info_hash, "querying DHT");
let (info, dht_rx, initial_peers) = match read_metainfo_from_peer_receiver(
self.peer_id,
info_hash,
Expand All @@ -794,7 +794,7 @@ impl Session {
anyhow::bail!("DHT died, no way to discover torrent metainfo")
}
};
debug!("received result from DHT: {:?}", info);
debug!(?info, "received result from DHT");
(
info_hash,
info,
Expand Down Expand Up @@ -828,7 +828,7 @@ impl Session {

let dht_rx = match self.dht.as_ref() {
Some(dht) if !opts.paused && !opts.list_only => {
debug!("reading peers for {:?} from DHT", torrent.info_hash);
debug!(info_hash=?torrent.info_hash, "reading peers from DHT");
Some(dht.get_peers(torrent.info_hash, announce_port)?)
}
_ => None,
Expand Down Expand Up @@ -911,7 +911,7 @@ impl Session {
continue;
}
if !list_only {
info!("Will download {:?}", filename);
info!(?filename, "will download");
}
}
Ok(Some(only_files))
Expand Down Expand Up @@ -1043,14 +1043,14 @@ impl Session {
match (paused, delete_files) {
(Err(e), true) => Err(e).context("torrent deleted, but could not delete files"),
(Err(e), false) => {
warn!("could not delete torrent files: {:?}", e);
warn!(error=?e, "could not delete torrent files");
Ok(())
}
(Ok(Some(paused)), true) => {
drop(paused.files);
for file in paused.filenames {
if let Err(e) = std::fs::remove_file(&file) {
warn!("could not delete file {:?}: {:?}", file, e);
warn!(?file, error=?e, "could not delete file");
}
}
Ok(())
Expand Down
Loading

0 comments on commit 2017c5e

Please sign in to comment.