anki/rslib/src/log.rs
Damien Elmes 5004cd332b
Integrate FSRS into Anki (#2654)
* Pack FSRS data into card.data

* Update FSRS card data when preset or weights change

+ Show FSRS stats in card stats

* Show a warning when there's a limited review history

* Add some translations; tweak UI

* Fix default requested retention

* Add browser columns, fix calculation of R

* Property searches

eg prop:d>0.1

* Integrate FSRS into reviewer

* Warn about long learning steps

* Hide minimum interval when FSRS is on

* Don't apply interval multiplier to FSRS intervals

* Expose memory state to Python

* Don't set memory state on new cards

* Port Jarret's new tests; add some helpers to make tests more compact

https://github.com/open-spaced-repetition/fsrs-rs/pull/64

* Fix learning cards not being given memory state

* Require update to v3 scheduler

* Don't exclude single learning step when calculating memory state

* Use relearning step when learning steps unavailable

* Update docstring

* fix single_card_revlog_to_items (#2656)

* not need check the review_kind for unique_dates

* add email address to CONTRIBUTORS

* fix last first learn & keep early review

* cargo fmt

* cargo clippy --fix

* Add Jarrett to about screen

* Fix fsrs_memory_state being initialized to default in get_card()

* Set initial memory state on graduate

* Update to latest FSRS

* Fix experiment.log being empty

* Fix broken colpkg imports

Introduced by "Update FSRS card data when preset or weights change"

* Update memory state during (re)learning; use FSRS for graduating intervals

* Reset memory state when cards are manually rescheduled as new

* Add difficulty graph; hide eases when FSRS enabled

* Add retrievability graph

* Derive memory_state from revlog when it's missing and shouldn't be

---------

Co-authored-by: Jarrett Ye <jarrett.ye@outlook.com>
2023-09-16 16:09:26 +10:00

85 lines
2.5 KiB
Rust

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use std::fs;
use std::fs::OpenOptions;
use std::io;
use once_cell::sync::OnceCell;
use tracing::subscriber::set_global_default;
use tracing_appender::non_blocking::NonBlocking;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::fmt;
use tracing_subscriber::fmt::Layer;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::EnvFilter;
use crate::prelude::*;
const LOG_ROTATE_BYTES: u64 = 50 * 1024 * 1024;
/// Enable logging to the console, and optionally also to a file.
pub fn set_global_logger(path: Option<&str>) -> Result<()> {
if std::env::var("BURN_LOG").is_ok() {
return Ok(());
}
static ONCE: OnceCell<()> = OnceCell::new();
ONCE.get_or_try_init(|| -> Result<()> {
let file_writer = if let Some(path) = path {
Some(Layer::new().with_writer(get_appender(path)?))
} else {
None
};
let subscriber = tracing_subscriber::registry()
.with(fmt::layer().with_target(false))
.with(file_writer)
.with(EnvFilter::from_default_env());
set_global_default(subscriber).or_invalid("global subscriber already set")?;
Ok(())
})?;
Ok(())
}
/// Holding on to this guard does not actually ensure the log file will be fully
/// written, as statics do not implement Drop.
static APPENDER_GUARD: OnceCell<WorkerGuard> = OnceCell::new();
fn get_appender(path: &str) -> Result<NonBlocking> {
maybe_rotate_log(path)?;
let file = OpenOptions::new().create(true).append(true).open(path)?;
let (appender, guard) = tracing_appender::non_blocking(file);
if APPENDER_GUARD.set(guard).is_err() {
invalid_input!("log file should be set only once");
}
Ok(appender)
}
fn maybe_rotate_log(path: &str) -> io::Result<()> {
let current_bytes = match fs::metadata(path) {
Ok(meta) => meta.len(),
Err(e) => {
if e.kind() == io::ErrorKind::NotFound {
0
} else {
return Err(e);
}
}
};
if current_bytes < LOG_ROTATE_BYTES {
return Ok(());
}
let path2 = format!("{}.1", path);
let path3 = format!("{}.2", path);
// if a rotated file already exists, rename it
if let Err(e) = fs::rename(&path2, path3) {
if e.kind() != io::ErrorKind::NotFound {
return Err(e);
}
}
// and rotate the primary log
fs::rename(path, path2)
}