{"record":{"id":"5cabe8afd462b36a","repo":"Universal-Debloater-Alliance/universal-android-debloater-next-generation","slug":"setup-logging","errorCode":null,"errorMessage":"setup logging","messagePattern":"setup logging","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"crates/uad-gui/src/main.rs","lineNumber":27,"sourceCode":"use std::{fmt::Arguments, fs::OpenOptions, path::PathBuf};\nuse uad_core::utils::setup_uad_dir;\n\nuse uad_gui::gui::UadGui;\n\nstatic CACHE_DIR: LazyLock<PathBuf> =\n    LazyLock::new(|| setup_uad_dir(&dirs::cache_dir().expect(\"Can't detect cache dir\")));\n\nfn main() -> iced::Result {\n    // Safety: This function is safe to call in a single-threaded program.\n    // The exact requirement is: you must ensure that there are no other threads concurrently writing or\n    // reading(!) the environment through functions or global variables other than the ones in this module.\n    unsafe {\n        // Force WGPU/Iced to use discrete GPU to prevent crashes on PCs with two GPUs.\n        // See #848 and related pull 850.\n        std::env::set_var(\"WGPU_POWER_PREF\", \"high\");\n    }\n\n    setup_logger().expect(\"setup logging\");\n    UadGui::start()\n}\n\n/// Sets up logging to a new file in `CACHE_DIR\"/uadng.log\"`\n/// Also attaches the terminal on Windows machines\n/// '''\n/// match `setup_logger().expect(\"Error` setting up logger\")\n/// '''\nfn setup_logger() -> Result<(), fern::InitError> {\n    #[cfg(target_os = \"windows\")]\n    {\n        attach_windows_console();\n    }\n\n    let colors = ColoredLevelConfig::new().info(Color::Green);\n\n    let make_formatter = |use_colors: bool| {\n        move |out: FormatCallback, message: &Arguments, record: &Record| {","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/Universal-Debloater-Alliance/universal-android-debloater-next-generation/blob/64465c850c7ed36329e67165ac08501abffb218e/crates/uad-gui/src/main.rs#L9-L45","documentation":"main calls setup_logger().expect(\"setup logging\"), which creates and locks a log file (CACHE_DIR/uadng.log) and attaches the terminal on Windows. If logger initialization fails (cache dir unresolvable/unwritable, log file can't be created), the app panics before launching the GUI, so the user sees a panic message instead of the application.","triggerScenarios":"Running the binary when CACHE_DIR is unwritable or missing (read-only home, disk full, sandboxed launch without filesystem access), or on Windows when attaching the terminal/console fails; also triggered indirectly when error 16's CACHE_DIR was computed but the directory couldn't be created inside setup_logger.","commonSituations":"Flatpak/snap or portable installs with read-only cache paths; running under a service account whose cache dir isn't writable; antivirus blocking creation of uadng.log on Windows; two instances racing to lock the same log file.","solutions":["Ensure the cache directory exists and is writable (check `ls -ld ~/.cache`, free disk space) before launching.","Replace .expect with a graceful path: fall back to logging to stderr/no-op logger so the GUI still starts.","In sandboxed installs, redirect the log dir to a writable location (XDG_CACHE_HOME) via the launcher wrapper.","Close the other running instance that may hold the log file lock, or use a rotating/unique log filename."],"exampleFix":"// before\nsetup_logger().expect(\"setup logging\");\nUadGui::start()\n// after\nif let Err(e) = setup_logger() {\n    eprintln!(\"Logging disabled: {e}\");\n}\nUadGui::start()","handlingStrategy":"try-catch","validationCode":"if !cache_dir.exists() || std::fs::OpenOptions::new().write(true).create_new(cache_dir.join(\".probe\")).is_err() {\n    eprintln!(\"cache dir not writable; logging will be degraded\");\n}","typeGuard":null,"tryCatchPattern":"if let Err(e) = setup_logger() {\n    eprintln!(\"failed to init logging: {e}; continuing without file logging\");\n}","preventionTips":["Never let logging initialization be fatal — degrade to stderr instead.","Check disk space and write permissions on the cache dir before first launch.","Avoid two instances contending for the same log file; use unique or rotating filenames."],"tags":["panic","startup","logging","filesystem"],"backgroundTag":"module-init-failed","analyzedSha":"64465c850c7ed36329e67165ac08501abffb218e","analyzedAt":"2026-09-12T09:09:23.137Z","contentChangedAt":"2026-09-12T09:09:23.137Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}