Skip to content

Commit

Permalink
Rework logging system
Browse files Browse the repository at this point in the history
  • Loading branch information
AeEn123 committed Jan 10, 2025
1 parent 339f4cb commit 296d8fc
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 64 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.4"
edition = "2021"

[dependencies]
chrono = "0.4.39"
clap = { version = "4.5.23", features = ["derive"] }
eframe = { features = ["glow"], default-features = false, version = "0.29" }
egui = "0.29"
Expand Down
12 changes: 6 additions & 6 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use fluent_bundle::{FluentBundle, FluentResource};
use std::sync::Arc;

use std::collections::HashMap; // Used for input
use crate::{logic, updater}; // Used for functionality
use crate::{log, logic, updater}; // Used for functionality

mod welcome;
mod settings;
Expand Down Expand Up @@ -472,12 +472,12 @@ fn detect_japanese_font() -> Option<String> {
match std::fs::metadata(&resolved_font) {
Ok(metadata) => {
if metadata.is_file() {
println!("{}: valid", resolved_font);
log::info(&format!("{}: valid", resolved_font));
return Some(resolved_font);
}
}
Err(e) => {
println!("{}: invalid - {}", resolved_font, e)
log::warn(&format!("{}: invalid - {}", resolved_font, e))
}
}

Expand All @@ -504,12 +504,12 @@ impl MyApp {
cc.egui_ctx.set_fonts(font);
}
Err(e) => {
println!("Error loading Japanese fonts: {e}")
log::warn(&format!("Error loading Japanese fonts: {e}"))
}
}
}
None => {
println!("No Japanese fonts detected, Japanese characters will not render.")
log::warn("No Japanese fonts detected, Japanese characters will not render.")
}
}

Expand Down Expand Up @@ -597,7 +597,7 @@ pub fn run_gui() {
);

if result.is_err() {
eprintln!("GUI failed: {}", result.unwrap_err())
log::error(&format!("GUI failed: {}", result.unwrap_err()))
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/gui/welcome.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use eframe::egui;
use crate::gui::settings;
use crate::{gui::settings, log};
use crate::logic;
use fluent_bundle::{FluentBundle, FluentResource};
use std::sync::Arc;
Expand All @@ -20,12 +20,12 @@ fn detect_japanese_font() -> Option<String> {
match std::fs::metadata(&resolved_font) {
Ok(metadata) => {
if metadata.is_file() {
println!("{}: valid", resolved_font);
log::info(&format!("{}: valid", resolved_font));
return Some(resolved_font);
}
}
Err(e) => {
println!("{}: invalid - {}", resolved_font, e)
log::warn(&format!("{}: invalid - {}", resolved_font, e))
}
}

Expand All @@ -52,12 +52,12 @@ impl MyApp {
cc.egui_ctx.set_fonts(font);
}
Err(e) => {
println!("Error loading Japanese fonts: {e}")
log::error(&format!("Error loading Japanese fonts: {e}"))
}
}
}
None => {
println!("No Japanese fonts detected, Japanese characters will not render.")
log::warn("No Japanese fonts detected, Japanese characters will not render.")
}
}
Default::default()
Expand Down
29 changes: 29 additions & 0 deletions src/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::sync::Mutex;
use lazy_static::lazy_static;

lazy_static! {
static ref LOG: Mutex<String> = Mutex::new(String::new());
}

fn log(log_type: &str, message: &str) {
let now = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string();
let log_message = format!("{} {}{}", now, log_type, message);

println!("{}", log_message);

let mut log = LOG.lock().unwrap();

log.push_str(&log_message);
}

pub fn info(message: &str) {
log("INFO: ", message)
}

pub fn warn(message: &str) {
log("WARN: ", message)
}

pub fn error(message: &str) {
log("ERROR: ", message)
}
Loading

0 comments on commit 296d8fc

Please sign in to comment.