Skip to content

refactor(updater): replace log crate with tracing for improved logging #2592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/updater-logging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"updater": patch
---

replace tracing with log for conditional logging.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions plugins/updater/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
log = { workspace = true }
tracing = { workspace = true, optional = true }
tokio = "1"
reqwest = { version = "0.12", default-features = false, features = [
"json",
Expand Down Expand Up @@ -71,3 +72,4 @@ zip = ["dep:zip", "dep:tar", "dep:flate2"]
native-tls = ["reqwest/native-tls"]
native-tls-vendored = ["reqwest/native-tls-vendored"]
rustls-tls = ["reqwest/rustls-tls"]
tracing = ["dep:tracing"]
36 changes: 20 additions & 16 deletions plugins/updater/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use std::ffi::OsStr;
use base64::Engine;
use futures_util::StreamExt;
use http::HeaderName;
#[cfg(not(feature = "tracing"))]
use log as logger;
use minisign_verify::{PublicKey, Signature};
use percent_encoding::{AsciiSet, CONTROLS};
use reqwest::{
Expand All @@ -28,6 +30,8 @@ use semver::Version;
use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize};
use tauri::{utils::platform::current_exe, AppHandle, Resource, Runtime};
use time::OffsetDateTime;
#[cfg(feature = "tracing")]
use tracing as logger;
use url::Url;

use crate::{
Expand Down Expand Up @@ -387,14 +391,14 @@ impl Updater {
.replace("{{arch}}", self.arch)
.parse()?;

log::debug!("checking for updates {url}");
logger::debug!("checking for updates {url}");

let mut request = ClientBuilder::new().user_agent(UPDATER_USER_AGENT);
if let Some(timeout) = self.timeout {
request = request.timeout(timeout);
}
if let Some(ref proxy) = self.proxy {
log::debug!("using proxy {proxy}");
logger::debug!("using proxy {proxy}");
let proxy = reqwest::Proxy::all(proxy.as_str())?;
request = request.proxy(proxy);
}
Expand All @@ -415,36 +419,36 @@ impl Updater {
if res.status().is_success() {
// no updates found!
if StatusCode::NO_CONTENT == res.status() {
log::debug!("update endpoint returned 204 No Content");
logger::debug!("update endpoint returned 204 No Content");
return Ok(None);
};

let update_response: serde_json::Value = res.json().await?;
log::debug!("update response: {update_response:?}");
logger::debug!("update response: {update_response:?}");
raw_json = Some(update_response.clone());
match serde_json::from_value::<RemoteRelease>(update_response)
.map_err(Into::into)
{
Ok(release) => {
log::debug!("parsed release response {release:?}");
logger::debug!("parsed release response {release:?}");
last_error = None;
remote_release = Some(release);
// we found a release, break the loop
break;
}
Err(err) => {
log::error!("failed to deserialize update response: {err}");
logger::error!("failed to deserialize update response: {err}");
last_error = Some(err)
}
}
} else {
log::error!(
logger::error!(
"update endpoint did not respond with a successful status code"
);
}
}
Err(err) => {
log::error!("failed to check for updates: {err}");
logger::error!("failed to check for updates: {err}");
last_error = Some(err.into())
}
}
Expand Down Expand Up @@ -713,7 +717,7 @@ impl Update {
};

if let Some(on_before_exit) = self.on_before_exit.as_ref() {
log::debug!("running on_before_exit hook");
logger::debug!("running on_before_exit hook");
on_before_exit();
}

Expand Down Expand Up @@ -882,7 +886,7 @@ impl Update {

#[cfg(feature = "zip")]
if infer::archive::is_gz(bytes) {
log::debug!("extracting AppImage");
logger::debug!("extracting AppImage");
// extract the buffer to the tmp_dir
// we extract our signed archive into our final directory without any temp file
let archive = Cursor::new(bytes);
Expand All @@ -906,7 +910,7 @@ impl Update {
return Err(Error::BinaryNotFoundInArchive);
}

log::debug!("rewriting AppImage");
logger::debug!("rewriting AppImage");
return match std::fs::write(&self.extract_path, bytes)
.and_then(|_| std::fs::set_permissions(&self.extract_path, permissions))
{
Expand Down Expand Up @@ -960,7 +964,7 @@ impl Update {
fn install_deb(&self, bytes: &[u8]) -> Result<()> {
// First verify the bytes are actually a .deb package
if !infer::archive::is_deb(bytes) {
log::warn!("update is not a valid deb package");
logger::warn!("update is not a valid deb package");
return Err(Error::InvalidUpdaterFormat);
}

Expand Down Expand Up @@ -1003,15 +1007,15 @@ impl Update {
.status()
{
if status.success() {
log::debug!("installed deb with pkexec");
logger::debug!("installed deb with pkexec");
return Ok(());
}
}

// 2. Try zenity or kdialog for a graphical sudo experience
if let Ok(password) = self.get_password_graphically() {
if self.install_with_sudo(deb_path, &password)? {
log::debug!("installed deb with GUI sudo");
logger::debug!("installed deb with GUI sudo");
return Ok(());
}
}
Expand All @@ -1024,7 +1028,7 @@ impl Update {
.status()?;

if status.success() {
log::debug!("installed deb with sudo");
logger::debug!("installed deb with sudo");
Ok(())
} else {
Err(Error::DebInstallFailed)
Expand Down Expand Up @@ -1148,7 +1152,7 @@ impl Update {
};

if need_authorization {
log::debug!("app installation needs admin privileges");
logger::debug!("app installation needs admin privileges");
// Use AppleScript to perform moves with admin privileges
let apple_script = format!(
"do shell script \"rm -rf '{src}' && mv -f '{new}' '{src}'\" with administrator privileges",
Expand Down
Loading