Migrate to structured logging

This commit is contained in:
micialware committed 2026-08-23 22:48:00 +03:00
1 parent 0367d5001b
commit 0baf6cadf7
13 files changed
+202 -30

No files matched your search

+2 -1
View File
@@ -4,6 +4,7 @@ use iced::{keyboard, window, Subscription};
use iced_core::{Font, Size, Theme};
use iced_core::window::Position;
use iced_core::window::settings::PlatformSpecific;
use log::info;
use rusqlite::Connection;
use crate::data_provider::card_sets::load_sets;
use crate::data_provider::settings::get_setting;
@@ -15,7 +16,7 @@ use crate::navigation::RootMessage::Keyboard;
pub const USER_FONT: Font = Font::with_name("Noto Sans JP");
pub fn start() -> iced::Result {
println!("Welcome to JapLearn");
info!("Welcome to JapLearn");
iced::application(ScreenState::boot, ScreenState::update, ScreenState::view)
.window(window_settings())
.subscription(subscription)
+3 -2
View File
@@ -1,6 +1,7 @@
use crate::lang::{CardStatistics, DeckSettings};
use rusqlite::{Connection, params};
use std::time::Instant;
use log::info;
pub fn load_stats_of_deck(set: &DeckSettings, connection: &Connection) -> Vec<CardStatistics> {
let mut stmt = connection
@@ -58,7 +59,7 @@ pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection)
stats[index].id = id.into();
}
println!("Added {} cards for {:?}", stats.len(), time.elapsed());
info!("Added {} cards for {:?}", stats.len(), time.elapsed());
}
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) {
@@ -70,7 +71,7 @@ pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) {
(&stat.score, &stat.last_open.timestamp(), &stat.id),
)
.unwrap();
println!("Updated stat: {}", time.elapsed().as_millis());
info!("Updated stat: {}", time.elapsed().as_millis());
}
pub fn delete_stats(stat: &[CardStatistics], connection: &Connection) {
+4 -3
View File
@@ -1,5 +1,6 @@
use crate::notification::NotificationData;
use hashbrown::HashMap;
use log::info;
use rusqlite::Connection;
use serde_json::Value;
@@ -53,7 +54,7 @@ pub fn load_groups(connection: &Connection) -> Result<ImportData, NotificationDa
.map(|x| x.as_str().unwrap().to_string())
.collect();
let id = collection.get("id");
if id.is_none() {
if id.is_none() {
return Err(NotificationData::IncorrectAnkiFile);
}
let id = id.unwrap().as_u64().unwrap();
@@ -62,7 +63,7 @@ pub fn load_groups(connection: &Connection) -> Result<ImportData, NotificationDa
return Err(NotificationData::IncorrectAnkiFile);
}
let name = name.unwrap().as_str().unwrap().to_string();
let mut group = ImportGroup {
id,
name,
@@ -74,7 +75,7 @@ pub fn load_groups(connection: &Connection) -> Result<ImportData, NotificationDa
if let Some((_, count)) = counts.iter().find(|(id, _)| *id == group.id as i64) {
group.length = *count as u64
}
println!("{:?}", group);
info!("{:?}", group);
total_data.0.push(group);
}
}
+4 -3
View File
@@ -1,5 +1,6 @@
use crate::dictionary::app_data_dir;
use std::io;
use log::info;
use reqwest::Error;
use tokio::fs::{File, OpenOptions};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
@@ -72,7 +73,7 @@ pub fn decompress(data: Vec<u8>) -> Vec<u8> {
let mut decompressed = Vec::new();
let res = io::copy(&mut decoder, &mut decompressed);
if let Err(e) = res {
println!("{}", e);
info!("{}", e);
}
decompressed
}
@@ -89,7 +90,7 @@ pub async fn first_sync() -> Result<String, NotificationData> {
.text()
.await
.map_err(|_| WebApiUnavailable)?;
println!("id: {}", id);
info!("id: {}", id);
Ok(id)
}
@@ -97,7 +98,7 @@ pub async fn get_web_version(key: &str) -> Result<u32, NotificationData> {
let id_url = format!("{API_URL}{key}/version");
let client = reqwest::Client::new();
let version = client.get(&id_url).send().await.map_err(|_| WebApiUnavailable)?.text().await.map_err(|_| WebApiUnavailable)?;
println!("version: {}", version);
info!("version: {}", version);
Ok(version.parse::<u32>().unwrap_or_default())
}
+4 -3
View File
@@ -24,6 +24,7 @@ use std::ops::Add;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};
use log::info;
use crate::app::AppState;
#[derive(Clone)]
@@ -438,8 +439,8 @@ impl DictionaryState {
col = col.push(lazy_line);
}
println!("Drawing {index} lines");
println!("Words rendering time: {:?}", time.elapsed());
info!("Drawing {index} lines");
info!("Words rendering time: {:?}", time.elapsed());
scrollable(col).height(Fill).into()
}
@@ -554,7 +555,7 @@ impl DictionaryState {
})
.collect();
println!("Time {}", time.elapsed().as_micros());
info!("Time {}", time.elapsed().as_micros());
}
fn groups_panel(&self) -> iced::Element<'_, DictionaryMessage> {
+2 -2
View File
@@ -146,8 +146,8 @@ impl DictionaryQuizState {
if !self.is_help {
self.score.total += 1;
}
if self.answer == self.correct
|| split_with_coma(self.correct.as_str()).contains(&self.answer)
if self.answer.to_lowercase() == self.correct.to_lowercase()
|| split_with_coma(self.correct.to_lowercase().as_str()).contains(&self.answer.to_lowercase())
{
if !self.is_help {
self.score.correct += 1;
+2 -1
View File
@@ -24,6 +24,7 @@ use std::fs::{File, OpenOptions};
use std::io::BufReader;
use std::path::PathBuf;
use std::sync::Arc;
use log::info;
use tokio::task::spawn_blocking;
use zip::ZipArchive;
use crate::app::AppState;
@@ -426,7 +427,7 @@ impl ImportState {
let skip_empty = self.skip_empty;
let group = self.import_data.as_ref().unwrap().0[self.selected_index].clone();
let progress = self.progress.clone().unwrap();
println!("{}", group.name);
info!("{}", group.name);
Task::perform(
async move {
spawn_blocking(move || {
+3 -2
View File
@@ -22,6 +22,7 @@ use std::num::ParseIntError;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Instant;
use log::info;
const MAX_HISTORY_LEN: usize = 20;
const MAX_HISTORY_LEN_PART: f32 = 0.33;
@@ -661,7 +662,7 @@ impl DeckSettings {
let time = Instant::now();
let engine = Engine::new();
let ast = engine.compile(&self.filter);
println!("Compile time is {:?}", time.elapsed());
info!("Compile time is {:?}", time.elapsed());
ast.is_ok()
}
pub fn get_word_list(&self, state: &AppState) -> Vec<usize> {
@@ -705,7 +706,7 @@ impl DeckSettings {
.map(|(index, _)| index)
.collect();
println!("Collecting available words is {:?}", time.elapsed());
info!("Collecting available words is {:?}", time.elapsed());
list
} else {
+1
View File
@@ -28,5 +28,6 @@ static GLOBAL: MiMalloc = MiMalloc;
fn main() -> iced::Result {
env_logger::init();
start()
}
+11 -10
View File
@@ -33,6 +33,7 @@ use parking_lot::Mutex;
use rusqlite::Connection;
use std::sync::Arc;
use std::time::Instant;
use log::info;
impl Default for ScreenState {
fn default() -> Self {
@@ -95,9 +96,9 @@ pub struct ScreenState {
impl ScreenState {
pub fn boot() -> (ScreenState, Task<RootMessage>) {
println!("Booting");
info!("Booting");
create_db();
println!("Db created");
info!("Db created");
let state = ScreenState::default();
let reading_additional_task = Task::perform(Self::load_additional_data(), |data| data);
@@ -128,7 +129,7 @@ impl ScreenState {
}
drop(state_guard);
println!("Boot finished");
info!("Boot finished");
(state, final_task)
}
@@ -161,9 +162,9 @@ impl ScreenState {
async fn load_web_backup(string: String) -> Result<bool, NotificationData> {
let local = get_local_version().await;
let web = get_web_version(string.as_str()).await?;
println!("{} {}", local, web);
info!("{} {}", local, web);
if web > local {
println!("Web version is newer than local version");
info!("Web version is newer than local version");
load_data(string, true).await?;
set_local_version(web).await;
} else {
@@ -173,7 +174,7 @@ impl ScreenState {
}
pub fn update(&mut self, message: RootMessage) -> Task<RootMessage> {
println!("Updating");
info!("Updating");
let time = Instant::now();
if let Keyboard(e) = message {
return self.handle_keyboard(e);
@@ -185,7 +186,7 @@ impl ScreenState {
}
if let UpdateData = message {
println!("Loading data");
info!("Loading data");
let mut state = self.app_state.lock();
if cfg!(windows) {
state.connection = Connection::open_in_memory().unwrap();
@@ -228,7 +229,7 @@ impl ScreenState {
Import
);
}
println!("Update took {:?}", time.elapsed());
info!("Update took {:?}", time.elapsed());
task.0
}
@@ -255,7 +256,7 @@ impl ScreenState {
pub fn view(&self) -> Element<'_, RootMessage> {
println!("View update");
info!("View update");
let time = Instant::now();
let view = view_navigation!(
self.stack,
@@ -276,7 +277,7 @@ impl ScreenState {
let notifications = self.notification_manager.draw().map(RootMessage::Notification);
println!("View took {:?}", time.elapsed());
info!("View took {:?}", time.elapsed());
stack![view, notifications].width(Fill).height(Fill).into()
}
+2 -1
View File
@@ -11,6 +11,7 @@ use iced::{Center, Element, Fill, Font, Length, Task};
use parking_lot::Mutex;
use std::sync::Arc;
use std::time::Duration;
use log::error;
use crate::app::{fill_state, AppState};
use crate::notification::{NotificationRequest, NotificationData};
@@ -246,7 +247,7 @@ impl SyncState {
.connection
.query_row("PRAGMA wal_checkpoint(TRUNCATE)", [], |_| Ok(()))
.unwrap_or_else(|e| {
eprintln!("Ошибка при checkpoint WAL: {e}");
error!("Ошибка при checkpoint WAL: {e}");
});
}
}