feat(desktop): complete lifecycle logs (#5044)

This commit is contained in:
Shreyas 2025-05-21 15:26:16 +05:30 committed by GitHub
parent 3980847983
commit 2ecc60ad1a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 91 additions and 18 deletions

View file

@ -1140,7 +1140,16 @@ version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225"
dependencies = [
"dirs-sys",
"dirs-sys 0.4.1",
]
[[package]]
name = "dirs"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3e8aa94d75141228480295a7d0e7feb620b1a5ad9f12bc40be62411e38cce4e"
dependencies = [
"dirs-sys 0.5.0",
]
[[package]]
@ -1151,10 +1160,22 @@ checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c"
dependencies = [
"libc",
"option-ext",
"redox_users",
"redox_users 0.4.6",
"windows-sys 0.48.0",
]
[[package]]
name = "dirs-sys"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab"
dependencies = [
"libc",
"option-ext",
"redox_users 0.5.0",
"windows-sys 0.59.0",
]
[[package]]
name = "dispatch"
version = "0.2.0"
@ -2036,6 +2057,7 @@ name = "hoppscotch-desktop"
version = "25.4.2"
dependencies = [
"axum",
"dirs 6.0.0",
"file-rotate",
"portpicker",
"serde",
@ -3921,6 +3943,17 @@ dependencies = [
"thiserror 1.0.69",
]
[[package]]
name = "redox_users"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd6f9d3d47bdd2ad6945c5015a226ec6155d0bcdfd8f7cd29f86b71f8de99d2b"
dependencies = [
"getrandom 0.2.15",
"libredox",
"thiserror 2.0.7",
]
[[package]]
name = "regex"
version = "1.11.1"
@ -4859,7 +4892,7 @@ checksum = "e545de0a2dfe296fa67db208266cd397c5a55ae782da77973ef4c4fac90e9f2c"
dependencies = [
"anyhow",
"bytes",
"dirs",
"dirs 5.0.1",
"dunce",
"embed_plist",
"futures-util",
@ -4909,7 +4942,7 @@ checksum = "7bd2a4bcfaf5fb9f4be72520eefcb61ae565038f8ccba2a497d8c28f463b8c01"
dependencies = [
"anyhow",
"cargo_toml",
"dirs",
"dirs 5.0.1",
"glob",
"heck 0.5.0",
"json-patch",
@ -5147,7 +5180,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce2d39224390c41ba544f02b4f1721f42256320b3fb8c371e9425cbddeb4a68c"
dependencies = [
"base64 0.22.1",
"dirs",
"dirs 5.0.1",
"flate2",
"futures-util",
"http",
@ -5667,7 +5700,7 @@ checksum = "d48a05076dd272615d03033bf04f480199f7d1b66a8ac64d75c625fc4a70c06b"
dependencies = [
"core-graphics",
"crossbeam-channel",
"dirs",
"dirs 5.0.1",
"libappindicator",
"muda",
"objc2",

View file

@ -37,6 +37,7 @@ portpicker = "0.1.1"
tokio = "1.43.0"
tauri-plugin-process = "2.2.0"
file-rotate = "0.8.0"
dirs = "6.0.0"
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
tauri-plugin-updater = "2.3.1"

View file

@ -9,6 +9,7 @@ use tauri_plugin_appload::VendorConfigBuilder;
use tauri_plugin_deep_link::DeepLinkExt;
use tauri_plugin_window_state::StateFlags;
pub const HOPPSCOTCH_DESKTOP_IDENTIFIER: &'static str = "io.hoppscotch.desktop";
static SERVER_PORT: OnceLock<u16> = OnceLock::new();
#[tauri::command]
@ -22,9 +23,6 @@ pub fn run() {
.setup(|app| {
let handle = app.handle().clone();
logger::setup(app.handle().clone())?;
tracing::info!("Logger setup complete");
let server_port = portpicker::pick_unused_port().expect("Cannot find unused port");
tracing::info!("Selected server port: {}", server_port);
SERVER_PORT

View file

@ -1,16 +1,18 @@
use std::path::PathBuf;
use file_rotate::{compression::Compression, suffix::AppendCount, ContentLimit, FileRotate};
use tauri::{AppHandle, Manager, Runtime};
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
use crate::HOPPSCOTCH_DESKTOP_IDENTIFIER;
pub struct LogGuard(pub tracing_appender::non_blocking::WorkerGuard);
pub fn setup<R: Runtime>(app_handle: AppHandle<R>) -> Result<(), Box<dyn std::error::Error>> {
let log_dir = app_handle.path().app_log_dir()?;
std::fs::create_dir_all(&log_dir)?;
pub fn setup(log_dir: &PathBuf) -> Result<LogGuard, Box<dyn std::error::Error>> {
std::fs::create_dir_all(log_dir)?;
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| format!("debug").into());
let log_file_path = log_dir.join("io.hoppscotch.desktop.log");
let log_file_path = log_dir.join(&format!("{}.log", HOPPSCOTCH_DESKTOP_IDENTIFIER));
tracing::info!(log_file_path =? &log_file_path);
let file = FileRotate::new(
@ -41,12 +43,10 @@ pub fn setup<R: Runtime>(app_handle: AppHandle<R>) -> Result<(), Box<dyn std::er
.with(console_layer)
.init();
app_handle.manage(LogGuard(guard));
tracing::info!(
log_file = %log_file_path.display(),
"Logging initialized with single file"
);
Ok(())
Ok(LogGuard(guard))
}

View file

@ -1,7 +1,48 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use hoppscotch_desktop_lib::{
logger::{self, LogGuard},
HOPPSCOTCH_DESKTOP_IDENTIFIER,
};
fn main() {
println!("Starting Hoppscotch Desktop application...");
// Follows how `tauri` does this
// see: https://github.com/tauri-apps/tauri/blob/dev/crates/tauri/src/path/desktop.rs
let path = {
#[cfg(target_os = "macos")]
let path = dirs::home_dir()
.map(|dir| dir.join("Library/Logs").join(HOPPSCOTCH_DESKTOP_IDENTIFIER));
#[cfg(not(target_os = "macos"))]
let path =
dirs::data_local_dir().map(|dir| dir.join(HOPPSCOTCH_DESKTOP_IDENTIFIER).join("logs"));
path
};
let Some(log_file_path) = path else {
eprint!("Failed to setup logging!");
println!("Starting Hoppscotch Desktop...");
return hoppscotch_desktop_lib::run()
};
let Ok(LogGuard(guard)) = logger::setup(&log_file_path) else {
eprint!("Failed to setup logging!");
println!("Starting Hoppscotch Desktop...");
return hoppscotch_desktop_lib::run()
};
// This keeps the guard alive, this is scoped to `main`
// so it can only drop when the entire app exits,
// so safe to have it like this.
let _guard = guard;
tracing::info!("Starting Hoppscotch Desktop...");
hoppscotch_desktop_lib::run()
}