Merge branch 'capucho/screenshots-logs-directory' into 'master'

Save screenshots and logs in a fixed directory

Closes #484

See merge request veloren/veloren!835
This commit is contained in:
Acrimon 2020-03-05 19:26:07 +00:00
commit 511bbd3899
3 changed files with 59 additions and 13 deletions

View File

@ -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);
}

View File

@ -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<String>,
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,
}
}
}

View File

@ -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);
}
}