Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better log filepath retrieval #555

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ ninput = { git = "https://github.com/blu-dev/ninput" }
lazysimd = { git = "https://github.com/Raytwo/lazysimd" }

[patch.crates-io]
nnsdk = { git = "https://github.com/Raytwo/nnsdk-rs", branch="account_fs" }
nnsdk = { git = "https://github.com/ultimate-research/nnsdk-rs"}
# Specifying latest commit because git only doesn't use latest changes
native-tls = { git = "https://github.com/skyline-rs/rust-native-tls", rev = "f202fca" }

Expand Down
39 changes: 5 additions & 34 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,14 @@ use std::{
use log::{LevelFilter, Metadata, Record, SetLoggerError};
use once_cell::sync::Lazy;
use parking_lot::Mutex;

use skyline::nn::time;
use crate::config;

/// Since we can't rely on most time based libraries, this is a seconds -> date/time string based on the `chrono` crates implementation
fn format_time_string(seconds: u64) -> String {
let leapyear = |year| -> bool { year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) };

static YEAR_TABLE: [[u64; 12]; 2] = [
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
];

let mut year = 1970;

let seconds_in_day = seconds % 86400;
let mut day_number = seconds / 86400;

let sec = seconds_in_day % 60;
let min = (seconds_in_day % 3600) / 60;
let hours = seconds_in_day / 3600;
loop {
let year_length = if leapyear(year) { 366 } else { 365 };

if day_number >= year_length {
day_number -= year_length;
year += 1;
} else {
break;
}
}
let mut month = 0;
while day_number >= YEAR_TABLE[if leapyear(year) { 1 } else { 0 }][month] {
day_number -= YEAR_TABLE[if leapyear(year) { 1 } else { 0 }][month];
month += 1;
}
fn get_time_string() -> String {
let datetime: time::CalendarTime = time::get_calendar_time();

format!("{:04}-{:02}-{:02}_{:02}-{:02}-{:02}", year, month + 1, day_number + 1, hours, min, sec)
format!("{:04}-{:02}-{:02}_{:02}-{:02}-{:02}", datetime.year, datetime.month, datetime.day, datetime.hour, datetime.minute, datetime.second)
}

static LOG_PATH: &str = "sd:/ultimate/arcropolis/logs";
Expand Down Expand Up @@ -74,7 +45,7 @@ static FILE_WRITER: Lazy<FileLogger> = Lazy::new(|| {
let seconds = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Clock may have gone backwards!");
let path = Path::new(LOG_PATH).join(format!("{}.log", format_time_string(seconds.as_secs())));
let path = Path::new(LOG_PATH).join(format!("{}.log", get_time_string()));
let _ = std::fs::create_dir_all(LOG_PATH);
std::fs::File::create(path).map_or_else(
|_| {
Expand Down
Loading