mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-03-12 18:29:06 +00:00
Fixed the conversion of log paths (FileSpec) to String (#306)
This commit is contained in:
parent
dbc2b4f6bc
commit
2e84f10a97
@ -30,14 +30,14 @@ pub fn start_tauri() {
|
||||
*MAIN_WINDOW.lock().unwrap() = Some(window);
|
||||
|
||||
info!(Source = "Bootloader Tauri"; "Setup is running.");
|
||||
let logger_path = app.path_resolver().app_local_data_dir().unwrap();
|
||||
let logger_path = logger_path.join("data");
|
||||
let data_path = app.path_resolver().app_local_data_dir().unwrap();
|
||||
let data_path = data_path.join("data");
|
||||
|
||||
DATA_DIRECTORY.set(logger_path.to_str().unwrap().to_string()).map_err(|_| error!("Was not abe to set the data directory.")).unwrap();
|
||||
DATA_DIRECTORY.set(data_path.to_str().unwrap().to_string()).map_err(|_| error!("Was not abe to set the data directory.")).unwrap();
|
||||
CONFIG_DIRECTORY.set(app.path_resolver().app_config_dir().unwrap().to_str().unwrap().to_string()).map_err(|_| error!("Was not able to set the config directory.")).unwrap();
|
||||
|
||||
info!(Source = "Bootloader Tauri"; "Reconfigure the file logger to use the app data directory {logger_path:?}");
|
||||
switch_to_file_logging(logger_path).map_err(|e| error!("Failed to switch logging to file: {e}")).unwrap();
|
||||
info!(Source = "Bootloader Tauri"; "Reconfigure the file logger to use the app data directory {data_path:?}");
|
||||
switch_to_file_logging(data_path).map_err(|e| error!("Failed to switch logging to file: {e}")).unwrap();
|
||||
|
||||
Ok(())
|
||||
})
|
||||
|
@ -1,7 +1,8 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::env::{current_dir, temp_dir};
|
||||
use std::error::Error;
|
||||
use std::fmt::Debug;
|
||||
use std::path::PathBuf;
|
||||
use std::path::{absolute, PathBuf};
|
||||
use std::sync::OnceLock;
|
||||
use flexi_logger::{DeferredNow, Duplicate, FileSpec, Logger, LoggerHandle};
|
||||
use flexi_logger::writers::FileLogWriter;
|
||||
@ -51,12 +52,13 @@ pub fn init_logging() {
|
||||
};
|
||||
|
||||
let log_path = FileSpec::default()
|
||||
.directory(get_startup_log_path())
|
||||
.basename(log_basename)
|
||||
.suppress_timestamp()
|
||||
.suffix("log");
|
||||
|
||||
// Store the startup log path:
|
||||
LOG_STARTUP_PATH.set(log_path.as_pathbuf(None).canonicalize().unwrap().to_str().unwrap().to_string()).expect("Cannot store the startup log path");
|
||||
let _ = LOG_STARTUP_PATH.set(convert_log_path_to_string(&log_path));
|
||||
|
||||
let runtime_logger = Logger::try_with_str(log_config).expect("Cannot create logging")
|
||||
.log_to_file(log_path)
|
||||
@ -74,14 +76,59 @@ pub fn init_logging() {
|
||||
LOGGER.set(runtime_logger).expect("Cannot set LOGGER");
|
||||
}
|
||||
|
||||
/// Switch the logging system to a file-based output.
|
||||
fn convert_log_path_to_string(log_path: &FileSpec) -> String {
|
||||
let log_path = log_path.as_pathbuf(None);
|
||||
|
||||
// Case: The path is already absolute:
|
||||
if log_path.is_absolute() {
|
||||
return log_path.to_str().unwrap().to_string();
|
||||
}
|
||||
|
||||
// Case: The path is relative. Let's try to convert it to an absolute path:
|
||||
match log_path.canonicalize() {
|
||||
// Case: The path exists:
|
||||
Ok(log_path) => log_path.to_str().unwrap().to_string(),
|
||||
|
||||
// Case: The path does not exist. Let's try to build the
|
||||
// absolute path without touching the file system:
|
||||
Err(_) => match absolute(log_path.clone()) {
|
||||
|
||||
// Case: We could build the absolute path:
|
||||
Ok(log_path) => log_path.to_str().unwrap().to_string(),
|
||||
|
||||
// Case: We could not reconstruct the path using the working directory.
|
||||
Err(_) => log_path.to_str().unwrap().to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Note: Rust plans to remove the deprecation flag for std::env::home_dir() in Rust 1.86.0.
|
||||
#[allow(deprecated)]
|
||||
fn get_startup_log_path() -> String {
|
||||
match std::env::home_dir() {
|
||||
// Case: We could determine the home directory:
|
||||
Some(home_dir) => home_dir.to_str().unwrap().to_string(),
|
||||
|
||||
// Case: We could not determine the home directory. Let's try to use the working directory:
|
||||
None => match current_dir() {
|
||||
|
||||
// Case: We could determine the working directory:
|
||||
Ok(working_directory) => working_directory.to_str().unwrap().to_string(),
|
||||
|
||||
// Case: We could not determine the working directory. Let's use the temporary directory:
|
||||
Err(_) => temp_dir().to_str().unwrap().to_string(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Switch the logging system to a file-based output inside the given directory.
|
||||
pub fn switch_to_file_logging(logger_path: PathBuf) -> Result<(), Box<dyn Error>>{
|
||||
let log_path = FileSpec::default()
|
||||
.directory(logger_path)
|
||||
.basename("events")
|
||||
.suppress_timestamp()
|
||||
.suffix("log");
|
||||
LOG_APP_PATH.set(log_path.as_pathbuf(None).to_str().unwrap().to_string()).expect("Cannot store the app log path");
|
||||
let _ = LOG_APP_PATH.set(convert_log_path_to_string(&log_path));
|
||||
LOGGER.get().expect("No LOGGER was set").handle.reset_flw(&FileLogWriter::builder(log_path))?;
|
||||
|
||||
Ok(())
|
||||
|
Loading…
Reference in New Issue
Block a user