Much more sensible logging implementation

- Use of dirs::cache_dir for logfiles location
- Logfile name convention is now "UAD_%Y%m%d.log"
- Logger now only appends (no more overwrite)
- Logger now creates a new logfile each day
- Creation of a `uad_dir()` helper function to access directories UAD uses
This commit is contained in:
0x192 2022-01-09 18:59:11 +01:00
commit ed246bf9bf
No known key found for this signature in database
GPG key ID: 34D27465928A0A1D
4 changed files with 25 additions and 16 deletions

View file

@ -14,12 +14,15 @@ and `Removed`.
- [[#52](https://github.com/0x192/universal-android-debloater/issues/52)] `uk.co.ee.myee` to the debloat lists (thanks [@lawson58](https://github.com/lawson85)).
- [[#58](https://github.com/0x192/universal-android-debloater/issues/52)] `android` to the debloat lists with the tag `Unsafe`.
- [[#49](https://github.com/0x192/universal-android-debloater/issues/49)] Multi-device support: You are now able to select a device among the list of all ADB connected devices/emulators.
- [[#44](https://github.com/0x192/universal-android-debloater/issues/44)] Persistent settings: Settings (only `theme` for now) are saved to a config file. Its location follows [the standards of the different platforms](https://github.com/dirs-dev/dirs-rs#example).
- [[#44](https://github.com/0x192/universal-android-debloater/issues/44)] Persistent settings: Settings (only `theme` for now) are saved to a config file. Its location follows [the standards of the different OS](https://github.com/dirs-dev/dirs-rs#example).
### Changed
- Review of the package lists recommendations. The `Recommended` debloat list is now safer (less likely to remove something you'd want to keep).
- [[#65](https://github.com/0x192/universal-android-debloater/issues/65)] ADB commands now run in parallel and asynchronously! This means no more UI freeze when performing long/many actions! :rocket:
- UI now updates itself in real time when performing ADB actions (thanks to async & multithreading). Before, it waited for the end of all actions.
- Logfiles are now located in a more conventional place: [cache_dir](https://docs.rs/dirs/latest/dirs/).
- Previous logs are no longer overwritten. The logger now only appends to the current logfile of the day (UAD_%Y%m%d.log).
- Each new day the logger will create a new file on UAD launch.
### Fixed
- Miscellaneous minor issues in some package descriptions.

View file

@ -1,4 +1,5 @@
use crate::gui::views::settings::Settings;
use crate::CONFIG_DIR;
use serde::{Deserialize, Serialize};
use static_init::dynamic;
use std::fs;
@ -10,7 +11,7 @@ pub struct Config {
}
#[dynamic]
static CONFIG_FILE: PathBuf = config_dir();
static CONFIG_FILE: PathBuf = CONFIG_DIR.join("config.toml");
impl Default for Config {
fn default() -> Self {
@ -32,8 +33,7 @@ impl Config {
pub fn load_configuration_file() -> Self {
match fs::read_to_string(&*CONFIG_FILE) {
Ok(s) => toml::from_str(&s).unwrap_or_else(|e| panic!("Invalid config file: `{}`", e)),
Err(e) => {
println!("{}", e);
Err(_) => {
let default_conf = toml::to_string(&Config::default()).unwrap();
fs::write(&*CONFIG_FILE, default_conf)
.expect("Could not write config file to disk!");
@ -42,11 +42,3 @@ impl Config {
}
}
}
fn config_dir() -> PathBuf {
let config_dir = dirs::config_dir().unwrap().join("uad");
if !config_dir.exists() {
let _ = fs::create_dir_all(&config_dir);
}
config_dir.join("config.toml")
}

View file

@ -4,11 +4,11 @@ use crate::core::uad_lists::{Package, PackageState, Removal, UadList};
use crate::gui::views::list::Selection;
use crate::gui::widgets::package_row::PackageRow;
use crate::gui::ICONS;
use iced::{alignment, Length, Text};
use std::collections::HashMap;
use std::fs;
use std::io::{self, prelude::*, BufReader};
use std::path::PathBuf;
pub fn fetch_packages(
uad_lists: &'static HashMap<String, Package>,
@ -140,3 +140,9 @@ pub fn string_to_theme(theme: String) -> Theme {
_ => Theme::lupin(),
}
}
pub fn setup_uad_dir(dir: Option<PathBuf>) -> PathBuf {
let dir = dir.unwrap().join("uad");
fs::create_dir_all(&dir).expect("Can't create cache directory");
dir
}

View file

@ -4,17 +4,25 @@
extern crate log;
use crate::core::config::Config;
use crate::core::utils::setup_uad_dir;
use fern::{
colors::{Color, ColoredLevelConfig},
FormatCallback,
};
use log::Record;
use static_init::dynamic;
use std::path::PathBuf;
use std::{fmt::Arguments, fs::OpenOptions};
mod core;
mod gui;
#[dynamic]
static CONFIG_DIR: PathBuf = setup_uad_dir(dirs::config_dir());
#[dynamic]
static CACHE_DIR: PathBuf = setup_uad_dir(dirs::cache_dir());
#[dynamic]
static IN_FILE_CONFIGURATION: Config = Config::load_configuration_file();
@ -47,9 +55,9 @@ pub fn setup_logger() -> Result<(), fern::InitError> {
let log_file = OpenOptions::new()
.write(true)
.create(true)
.append(false)
.truncate(true)
.open("uad.log")?;
.append(true)
.truncate(false)
.open(CACHE_DIR.join(format!("UAD_{}.log", chrono::Local::now().format("%Y%m%d"))))?;
let file_dispatcher = fern::Dispatch::new()
.format(make_formatter(false))