1
0
Fork 0
mirror of https://github.com/doukutsu-rs/doukutsu-rs synced 2024-10-31 19:44:20 +00:00

Add logging to file

This commit is contained in:
biroder 2023-05-11 17:40:11 +03:00
parent 9409bb35fe
commit 277f5d957b
4 changed files with 90 additions and 13 deletions

View file

@ -65,6 +65,7 @@ cpal = { git = "https://github.com/doukutsu-rs/cpal", rev = "9d269d8724102404e73
directories = "3" directories = "3"
discord-rich-presence = "0.2" discord-rich-presence = "0.2"
downcast = "0.11" downcast = "0.11"
fern = "0.6.2"
glutin = { git = "https://github.com/doukutsu-rs/glutin.git", rev = "2dd95f042e6e090d36f577cbea125560dd99bd27", optional = true, default_features = false, features = ["x11"] } glutin = { git = "https://github.com/doukutsu-rs/glutin.git", rev = "2dd95f042e6e090d36f577cbea125560dd99bd27", optional = true, default_features = false, features = ["x11"] }
imgui = "0.8" imgui = "0.8"
image = { version = "0.24", default-features = false, features = ["png", "bmp"] } image = { version = "0.24", default-features = false, features = ["png", "bmp"] }
@ -85,7 +86,6 @@ serde = { version = "1", features = ["derive"] }
serde_derive = "1" serde_derive = "1"
serde_cbor = { version = "0.11", optional = true } serde_cbor = { version = "0.11", optional = true }
serde_json = "1.0" serde_json = "1.0"
simple_logger = { version = "1.16", features = ["colors", "threads"] }
strum = "0.24" strum = "0.24"
strum_macros = "0.24" strum_macros = "0.24"
# remove and replace when drain_filter is in stable # remove and replace when drain_filter is in stable

View file

@ -44,6 +44,8 @@ pub enum GameError {
InvalidValue(String), InvalidValue(String),
/// Something went wrong while executing a debug command line command. /// Something went wrong while executing a debug command line command.
CommandLineError(String), CommandLineError(String),
/// Something went wrong while initializing logger
LoggerError(String),
} }
impl fmt::Display for GameError { impl fmt::Display for GameError {
@ -147,3 +149,10 @@ impl<T> From<SendError<T>> for GameError {
GameError::EventLoopError(errstr) GameError::EventLoopError(errstr)
} }
} }
impl From<log::SetLoggerError> for GameError {
fn from(s: log::SetLoggerError) -> GameError {
let errstr = format!("Set logger error: {}", s);
GameError::LoggerError(errstr)
}
}

View file

@ -1,4 +1,5 @@
use std::cell::UnsafeCell; use std::cell::UnsafeCell;
use std::path::PathBuf;
use std::sync::Mutex; use std::sync::Mutex;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@ -212,12 +213,79 @@ impl Game {
} }
} }
// For the most part this is just a copy-paste of the code from FilesystemContainer because it logs
// some messages during init, but the default logger cannot be replaced with another
// one or deinited(so we can't create the console-only logger and replace it by the
// console&file logger after FilesystemContainer has been initialized)
fn get_logs_dir() -> GameResult<PathBuf> {
let mut logs_dir = PathBuf::new();
#[cfg(target_os = "android")]
{
logs_dir = PathBuf::from(ndk_glue::native_activity().internal_data_path().to_string_lossy().to_string());
}
#[cfg(not(any(target_os = "android", target_os = "horizon")))]
{
let project_dirs = match directories::ProjectDirs::from("", "", "doukutsu-rs") {
Some(dirs) => dirs,
None => {
use crate::framework::error::GameError;
return Err(GameError::FilesystemError(String::from(
"No valid home directory path could be retrieved.",
)));
}
};
logs_dir = project_dirs.data_local_dir().to_path_buf();
}
#[cfg(target_os = "horizon")]
{
logs_dir = PathBuf::from("sdmc:/switch/doukutsu-rs");
}
logs_dir.push("logs");
Ok(logs_dir)
}
fn init_logger() -> GameResult {
let logs_dir = get_logs_dir()?;
let _ = std::fs::create_dir_all(&logs_dir);
let mut dispatcher = fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"{} [{}] {}",
record.level(),
record.module_path().unwrap().to_owned(),
message
))
})
.level(log::LevelFilter::Info)
.chain(std::io::stderr());
let date = chrono::Utc::now();
let mut file = logs_dir.clone();
file.push(format!("log_{}", date.format("%Y-%m-%d")));
file.set_extension("txt");
dispatcher = dispatcher.chain(fern::log_file(file).unwrap());
dispatcher.apply()?;
//log::info!("===GAME LAUNCH===");
Ok(())
}
pub fn init(options: LaunchOptions) -> GameResult { pub fn init(options: LaunchOptions) -> GameResult {
let _ = simple_logger::SimpleLogger::new() let _ = init_logger();
.without_timestamps()
.with_colors(true)
.with_level(log::Level::Info.to_level_filter())
.init();
let mut context = Box::pin(Context::new()); let mut context = Box::pin(Context::new());