debug log belongs in cache directory not config directory (#179)

* log files are now written to cache dir instead of config dir

* lint

* fmt
This commit is contained in:
Christian Visintin
2023-05-09 15:32:42 +02:00
committed by GitHub
parent 9bf23b3f6b
commit 9009002b6e
26 changed files with 96 additions and 160 deletions

View File

@@ -10,8 +10,6 @@ use self_update::{
Status,
};
/// ### UpdateStatus
///
/// The status of the update in case of success
#[derive(Debug, Eq, PartialEq)]
pub enum UpdateStatus {

View File

@@ -730,7 +730,7 @@ mod tests {
// Limit is 2
assert_eq!(client.iter_recents().count(), 2);
// Check that 192.168.1.1 has been removed
let key: String = client.iter_recents().nth(0).unwrap().to_string();
let key: String = client.iter_recents().next().unwrap().to_string();
assert!(matches!(
client
.hosts

View File

@@ -5,10 +5,8 @@
// Ext
use std::path::{Path, PathBuf};
/// ### get_config_dir
///
/// Get termscp configuration directory path.
/// Returns None, if it's not possible to get it
/// Get termscp config directory path and initialize it.
/// Returns None if it's not possible to initialize it
pub fn init_config_dir() -> Result<Option<PathBuf>, String> {
// Get file
#[cfg(not(test))]
@@ -19,27 +17,51 @@ pub fn init_config_dir() -> Result<Option<PathBuf>, String> {
lazy_static! {
static ref CONF_DIR: Option<PathBuf> = Some(std::env::temp_dir());
}
if CONF_DIR.is_some() {
// Get path of bookmarks
let mut p: PathBuf = CONF_DIR.as_ref().unwrap().clone();
// Append termscp dir
p.push("termscp/");
// If directory doesn't exist, create it
if p.exists() {
return Ok(Some(p));
}
// directory doesn't exist; create dir recursively
match std::fs::create_dir_all(p.as_path()) {
Ok(_) => Ok(Some(p)),
Err(err) => Err(err.to_string()),
}
if let Some(dir) = CONF_DIR.as_deref() {
init_dir(dir).map(Option::Some)
} else {
Ok(None)
}
}
/// ### get_bookmarks_paths
///
/// Get termscp cache directory path and initialize it.
/// Returns None if it's not possible to initialize it
pub fn init_cache_dir() -> Result<Option<PathBuf>, String> {
// Get file
#[cfg(not(test))]
lazy_static! {
static ref CACHE_DIR: Option<PathBuf> = dirs::cache_dir();
}
#[cfg(test)]
lazy_static! {
static ref CACHE_DIR: Option<PathBuf> = Some(std::env::temp_dir());
}
if let Some(dir) = CACHE_DIR.as_deref() {
init_dir(dir).map(Option::Some)
} else {
Ok(None)
}
}
/// Init a termscp env dir
fn init_dir(p: &Path) -> Result<PathBuf, String> {
// Get path of bookmarks
let mut p: PathBuf = p.to_path_buf();
// Append termscp dir
p.push("termscp/");
// If directory doesn't exist, create it
if p.exists() {
return Ok(p);
}
// directory doesn't exist; create dir recursively
match std::fs::create_dir_all(p.as_path()) {
Ok(_) => Ok(p),
Err(err) => Err(err.to_string()),
}
}
/// Get paths for bookmarks client
/// Returns: path of bookmarks.toml
pub fn get_bookmarks_paths(config_dir: &Path) -> PathBuf {
@@ -49,8 +71,6 @@ pub fn get_bookmarks_paths(config_dir: &Path) -> PathBuf {
bookmarks_file
}
/// ### get_config_paths
///
/// Returns paths for config client
/// Returns: path of config.toml and path for ssh keys
pub fn get_config_paths(config_dir: &Path) -> (PathBuf, PathBuf) {
@@ -62,17 +82,13 @@ pub fn get_config_paths(config_dir: &Path) -> (PathBuf, PathBuf) {
(bookmarks_file, keys_dir)
}
/// ### get_log_paths
///
/// Returns the path for the supposed log file
pub fn get_log_paths(config_dir: &Path) -> PathBuf {
let mut log_file: PathBuf = PathBuf::from(config_dir);
pub fn get_log_paths(cache_dir: &Path) -> PathBuf {
let mut log_file: PathBuf = PathBuf::from(cache_dir);
log_file.push("termscp.log");
log_file
}
/// ### get_theme_path
///
/// Get paths for theme provider
/// Returns: path of theme.toml
pub fn get_theme_path(config_dir: &Path) -> PathBuf {
@@ -101,6 +117,15 @@ mod tests {
assert!(std::fs::remove_dir_all(conf_dir.as_path()).is_ok());
}
#[test]
#[serial]
fn should_get_cache_dir() {
// Create and get cache_dir
let cache_dir: PathBuf = init_cache_dir().ok().unwrap().unwrap();
// Remove dir
assert!(std::fs::remove_dir_all(cache_dir.as_path()).is_ok());
}
#[test]
#[serial]
fn test_system_environment_get_config_dir_err() {
@@ -148,8 +173,8 @@ mod tests {
#[serial]
fn test_system_environment_get_log_paths() {
assert_eq!(
get_log_paths(Path::new("/home/omar/.config/termscp/")),
PathBuf::from("/home/omar/.config/termscp/termscp.log"),
get_log_paths(Path::new("/home/omar/.cache/termscp/")),
PathBuf::from("/home/omar/.cache/termscp/termscp.log"),
);
}

View File

@@ -2,32 +2,26 @@
//!
//! `logging` is the module which initializes the logging system for termscp
// locals
use crate::system::environment::{get_log_paths, init_config_dir};
use super::environment::{get_log_paths, init_cache_dir};
use crate::utils::file::open_file;
// ext
pub use simplelog::LevelFilter as LogLevel;
use simplelog::{ConfigBuilder, WriteLogger};
use std::fs::File;
use std::path::PathBuf;
/// ### init
///
/// Initialize logger
pub fn init(level: LogLevel) -> Result<(), String> {
// Init config dir
let config_dir: PathBuf = match init_config_dir() {
// Init cache dir
let cache_dir = match init_cache_dir() {
Ok(Some(p)) => p,
Ok(None) => {
return Err(String::from(
"This system doesn't seem to support CONFIG_DIR",
"This system doesn't seem to support CACHE_DIR",
))
}
Err(err) => return Err(err),
};
let log_file_path: PathBuf = get_log_paths(config_dir.as_path());
let log_file_path = get_log_paths(cache_dir.as_path());
// Open log file
let file: File = open_file(log_file_path.as_path(), true, true, false)
let file = open_file(log_file_path.as_path(), true, true, false)
.map_err(|e| format!("Failed to open file {}: {}", log_file_path.display(), e))?;
// Prepare log config
let config = ConfigBuilder::new().set_time_format_rfc3339().build();