Logging (#8)
- Add `Logger`, `Config` - Back to `nixos-unstable` - `nix flake update`
This commit is contained in:
@@ -19,6 +19,7 @@ use ratatui::{
|
||||
layout::{Constraint, Layout, Rect},
|
||||
widgets::{Block, StatefulWidget, Widget},
|
||||
};
|
||||
use tracing::debug;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum Mode {
|
||||
@@ -90,6 +91,7 @@ impl App {
|
||||
}
|
||||
|
||||
fn handle_key_event(&mut self, key: KeyEvent) {
|
||||
debug!("Received key {:?}", key.code);
|
||||
match key.code {
|
||||
KeyCode::Char('q') | KeyCode::Esc => self.mode = Mode::Quit,
|
||||
KeyCode::Char('c') => self.content = Content::Countdown,
|
||||
|
||||
34
src/config.rs
Normal file
34
src/config.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use crate::constants::APP_NAME;
|
||||
use color_eyre::eyre::{eyre, Result};
|
||||
use directories::ProjectDirs;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
pub struct Config {
|
||||
pub log_dir: PathBuf,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn init() -> Result<Self> {
|
||||
let log_dir = get_default_state_dir()?.join("logs");
|
||||
fs::create_dir_all(&log_dir)?;
|
||||
|
||||
Ok(Self { log_dir })
|
||||
}
|
||||
}
|
||||
|
||||
// fn new
|
||||
pub fn get_project_dir() -> Result<ProjectDirs> {
|
||||
let dirs = ProjectDirs::from("", "", APP_NAME)
|
||||
.ok_or_else(|| eyre!("Failed to get project directories"))?;
|
||||
|
||||
Ok(dirs)
|
||||
}
|
||||
|
||||
fn get_default_state_dir() -> Result<PathBuf> {
|
||||
let directory = get_project_dir()?
|
||||
.state_dir()
|
||||
.map(|d| d.to_path_buf())
|
||||
.ok_or_else(|| eyre!("Failed to get state directory"))?;
|
||||
|
||||
Ok(directory)
|
||||
}
|
||||
@@ -1,2 +1,4 @@
|
||||
pub static APP_NAME: &str = env!("CARGO_PKG_NAME");
|
||||
|
||||
pub static TICK_VALUE_MS: u64 = 1000 / 10; // 0.1 sec in milliseconds
|
||||
pub static FPS_VALUE_MS: u64 = 1000 / 60; // 60 FPS in milliseconds
|
||||
|
||||
37
src/logging.rs
Normal file
37
src/logging.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use color_eyre::eyre::Result;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use tracing::level_filters::LevelFilter;
|
||||
use tracing_subscriber::{
|
||||
self, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt,
|
||||
};
|
||||
|
||||
use crate::constants::APP_NAME;
|
||||
|
||||
pub struct Logger {
|
||||
log_dir: PathBuf,
|
||||
}
|
||||
|
||||
impl Logger {
|
||||
pub fn new(log_dir: PathBuf) -> Self {
|
||||
Self { log_dir }
|
||||
}
|
||||
|
||||
pub fn init(&self) -> Result<()> {
|
||||
let log_path = self.log_dir.join(format!("{}.log", APP_NAME));
|
||||
let log_file = fs::File::create(log_path)?;
|
||||
let fmt_layer = tracing_subscriber::fmt::layer()
|
||||
.with_file(true)
|
||||
.with_line_number(true)
|
||||
.with_writer(log_file)
|
||||
.with_target(false)
|
||||
.with_ansi(false);
|
||||
let filter = tracing_subscriber::filter::EnvFilter::from_default_env()
|
||||
.add_directive(LevelFilter::DEBUG.into());
|
||||
tracing_subscriber::registry()
|
||||
.with(fmt_layer)
|
||||
.with(filter)
|
||||
.init();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
15
src/main.rs
15
src/main.rs
@@ -1,7 +1,11 @@
|
||||
mod app;
|
||||
mod clock;
|
||||
mod config;
|
||||
mod constants;
|
||||
mod events;
|
||||
#[cfg(debug_assertions)]
|
||||
mod logging;
|
||||
|
||||
mod terminal;
|
||||
mod utils;
|
||||
mod widgets;
|
||||
@@ -11,11 +15,16 @@ use color_eyre::Result;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
color_eyre::install()?;
|
||||
let terminal = terminal::init()?;
|
||||
let config = config::Config::init()?;
|
||||
#[cfg(debug_assertions)]
|
||||
logging::Logger::new(config.log_dir).init()?;
|
||||
|
||||
color_eyre::install()?;
|
||||
|
||||
let terminal = terminal::setup()?;
|
||||
let events = events::Events::new();
|
||||
App::new().run(terminal, events).await?;
|
||||
terminal::restore()?;
|
||||
terminal::teardown()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,22 +1,26 @@
|
||||
use std::io::{stdout, Stdout};
|
||||
use std::io;
|
||||
|
||||
use color_eyre::eyre::Result;
|
||||
use crossterm::{execute, terminal::*};
|
||||
use crossterm::{
|
||||
cursor, execute,
|
||||
terminal::{EnterAlternateScreen, LeaveAlternateScreen},
|
||||
};
|
||||
use ratatui::{backend::CrosstermBackend, Terminal as RatatuiTerminal};
|
||||
|
||||
pub type Terminal = RatatuiTerminal<CrosstermBackend<Stdout>>;
|
||||
pub type Terminal = RatatuiTerminal<CrosstermBackend<io::Stdout>>;
|
||||
|
||||
pub fn init() -> Result<Terminal> {
|
||||
enable_raw_mode()?;
|
||||
execute!(stdout(), EnterAlternateScreen)?;
|
||||
let mut terminal = RatatuiTerminal::new(CrosstermBackend::new(stdout()))?;
|
||||
pub fn setup() -> Result<Terminal> {
|
||||
let mut stdout = std::io::stdout();
|
||||
crossterm::terminal::enable_raw_mode()?;
|
||||
execute!(stdout, EnterAlternateScreen, cursor::Hide)?;
|
||||
let mut terminal = RatatuiTerminal::new(CrosstermBackend::new(stdout))?;
|
||||
terminal.clear()?;
|
||||
terminal.hide_cursor()?;
|
||||
Ok(terminal)
|
||||
}
|
||||
|
||||
pub fn restore() -> Result<()> {
|
||||
execute!(stdout(), LeaveAlternateScreen)?;
|
||||
disable_raw_mode()?;
|
||||
pub fn teardown() -> Result<()> {
|
||||
execute!(io::stdout(), LeaveAlternateScreen, cursor::Show)?;
|
||||
crossterm::terminal::disable_raw_mode()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user