Skip to content

Commit

Permalink
feat: logs clean up
Browse files Browse the repository at this point in the history
Logs will now clean up when older than 7 days
  • Loading branch information
1zun4 committed Dec 27, 2024
1 parent f7cdb15 commit c0c7e7d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with LiquidLauncher. If not, see <https://www.gnu.org/licenses/>.
*/

// #![feature(exit_status_error)] - wait for feature to be stable
#![feature(duration_constructors)]
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
Expand Down Expand Up @@ -65,9 +64,12 @@ static HTTP_CLIENT: Lazy<Client> = Lazy::new(|| {
pub fn main() -> Result<()> {
use tracing_subscriber::{fmt, EnvFilter};

let log_folder = LAUNCHER_DIRECTORY.data_dir().join("logs");
let logs = LAUNCHER_DIRECTORY.data_dir().join("logs");
if let Err(e) = utils::clean_directory(&logs, 7) {
error!("Failed to clear log folder: {:?}", e);
}

let file_appender = tracing_appender::rolling::hourly(log_folder, "launcher.log");
let file_appender = tracing_appender::rolling::daily(logs, "launcher.log");

let subscriber = tracing_subscriber::registry()
.with(EnvFilter::from("liquidlauncher=debug"))
Expand Down
29 changes: 29 additions & 0 deletions src-tauri/src/utils/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ use serde::Deserialize;
use std::fmt::Display;
use sysinfo::{RefreshKind, System, SystemExt};

use std::fs;
use std::path::Path;
use std::time::{Duration, SystemTime};

/// Get the total memory of the system in
pub fn sys_memory() -> u64 {
let sys = System::new_with_specifics(RefreshKind::new().with_memory());
Expand Down Expand Up @@ -148,3 +152,28 @@ impl Display for Architecture {
f.write_str(self.get_simple_name().unwrap())
}
}

pub fn clean_directory(
path: &Path,
max_age_days: u64,
) -> Result<()> {
let now = SystemTime::now();
let max_age = Duration::from_days(max_age_days);

for entry in fs::read_dir(path)? {
let entry = entry?;
let metadata = entry.metadata()?;

if !metadata.is_file() {
continue;
}

if let Ok(modified) = metadata.modified() {
if now.duration_since(modified)? > max_age {
let _ = fs::remove_file(entry.path());
}
}
}

Ok(())
}

0 comments on commit c0c7e7d

Please sign in to comment.