From ea53dea8a7c620a9af30b30f883de8346bff74ee Mon Sep 17 00:00:00 2001 From: S Handley Date: Thu, 5 Mar 2020 19:26:07 +0000 Subject: [PATCH] Fixes https://gitlab.com/veloren/veloren/issues/484 partially by saving logs to a fixed place (defined in the settings file) --- voxygen/src/logging.rs | 22 ++++++++++++++++------ voxygen/src/settings.rs | 39 +++++++++++++++++++++++++++++++++++++-- voxygen/src/window.rs | 11 ++++++----- 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/voxygen/src/logging.rs b/voxygen/src/logging.rs index 4085b3c30f..50ecbe6481 100644 --- a/voxygen/src/logging.rs +++ b/voxygen/src/logging.rs @@ -1,4 +1,5 @@ use fern::colors::{Color, ColoredLevelConfig}; +use std::fs; use crate::settings::Settings; @@ -39,12 +40,20 @@ pub fn init( )) }); - // Try to create the log file. - // Incase of it failing we simply print it out to the console. - let mut log_file_created = Ok(()); - match fern::log_file(&format!("voxygen-{}.log", time.format("%Y-%m-%d-%H"))) { - Ok(log_file) => file_cfg = file_cfg.chain(log_file), - Err(e) => log_file_created = Err(e), + // Try to create the logs file parent directories. + let mut log_file_created = fs::create_dir_all(&settings.log.logs_path); + + if log_file_created.is_ok() { + // Try to create the log file. + match fern::log_file( + settings + .log + .logs_path + .join(&format!("voxygen-{}.log", time.format("%Y-%m-%d-%H"))), + ) { + Ok(log_file) => file_cfg = file_cfg.chain(log_file), + Err(e) => log_file_created = Err(e), + } } let stdout_cfg = fern::Dispatch::new() @@ -65,6 +74,7 @@ pub fn init( .apply() .expect("Failed to setup logging!"); + // Incase that the log file creation failed simply print it to the console if let Err(e) = log_file_created { log::error!("Failed to create log file! {}", e); } diff --git a/voxygen/src/settings.rs b/voxygen/src/settings.rs index 8d339f2837..5bae056d85 100644 --- a/voxygen/src/settings.rs +++ b/voxygen/src/settings.rs @@ -5,7 +5,7 @@ use crate::{ ui::ScaleMode, window::KeyMouse, }; -use directories::ProjectDirs; +use directories::{ProjectDirs, UserDirs}; use glutin::{MouseButton, VirtualKeyCode}; use log::warn; use serde_derive::{Deserialize, Serialize}; @@ -177,10 +177,28 @@ pub struct Log { // Whether to create a log file or not. // Default is to create one. pub log_to_file: bool, + // The path on which the logs will be stored + pub logs_path: PathBuf, } impl Default for Log { - fn default() -> Self { Self { log_to_file: true } } + fn default() -> Self { + let proj_dirs = ProjectDirs::from("net", "veloren", "voxygen") + .expect("System's $HOME directory path not found!"); + + // Chooses a path to store the logs by the following order: + // - The VOXYGEN_LOGS environment variable + // - The ProjectsDirs data local directory + // This only selects if there isn't already an entry in the settings file + let logs_path = std::env::var_os("VOXYGEN_LOGS") + .map(PathBuf::from) + .unwrap_or(proj_dirs.data_local_dir().join("logs")); + + Self { + log_to_file: true, + logs_path, + } + } } /// `GraphicsSettings` contains settings related to framerate and in-game @@ -273,10 +291,26 @@ pub struct Settings { // TODO: Remove at a later date, for dev testing pub logon_commands: Vec, pub language: LanguageSettings, + pub screenshots_path: PathBuf, } impl Default for Settings { fn default() -> Self { + let user_dirs = UserDirs::new().expect("System's $HOME directory path not found!"); + + // Chooses a path to store the screenshots by the following order: + // - The VOXYGEN_SCREENSHOT environment variable + // - The user's picture directory + // - The executable's directory + // This only selects if there isn't already an entry in the settings file + let screenshots_path = std::env::var_os("VOXYGEN_SCREENSHOT") + .map(PathBuf::from) + .or(user_dirs.picture_dir().map(|dir| dir.join("veloren"))) + .or(std::env::current_exe() + .ok() + .and_then(|dir| dir.parent().map(|val| PathBuf::from(val)))) + .expect("Couldn't choose a place to store the screenshots"); + Settings { controls: ControlSettings::default(), gameplay: GameplaySettings::default(), @@ -288,6 +322,7 @@ impl Default for Settings { send_logon_commands: false, logon_commands: Vec::new(), language: LanguageSettings::default(), + screenshots_path, } } } diff --git a/voxygen/src/window.rs b/voxygen/src/window.rs index 1b43c15787..59c9b6c4ed 100644 --- a/voxygen/src/window.rs +++ b/voxygen/src/window.rs @@ -594,7 +594,7 @@ impl Window { }); if take_screenshot { - self.take_screenshot(); + self.take_screenshot(&settings); } if toggle_fullscreen { @@ -659,15 +659,16 @@ impl Window { pub fn send_supplement_event(&mut self, event: Event) { self.supplement_events.push(event) } - pub fn take_screenshot(&mut self) { + pub fn take_screenshot(&mut self, settings: &Settings) { match self.renderer.create_screenshot() { Ok(img) => { + let mut path = settings.screenshots_path.clone(); + std::thread::spawn(move || { - use std::{path::PathBuf, time::SystemTime}; + use std::time::SystemTime; // Check if folder exists and create it if it does not - let mut path = PathBuf::from("./screenshots"); if !path.exists() { - if let Err(err) = std::fs::create_dir(&path) { + if let Err(err) = std::fs::create_dir_all(&path) { warn!("Couldn't create folder for screenshot: {:?}", err); } }