Skip to main content

flowsurface_data/
log.rs

1use std::path::PathBuf;
2use std::{fs, io};
3
4use crate::data_path;
5
6const LOG_FILE: &str = "flowsurface-current.log";
7
8pub fn file() -> Result<fs::File, Error> {
9    let path = path()?;
10
11    Ok(fs::OpenOptions::new()
12        .write(true)
13        .create(true)
14        .append(false)
15        .truncate(true)
16        .open(path)?)
17}
18
19pub fn path() -> Result<PathBuf, Error> {
20    let full_path = data_path(Some(LOG_FILE));
21
22    let parent = full_path
23        .parent()
24        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Invalid log file path"))?;
25
26    if !parent.exists() {
27        fs::create_dir_all(parent)?;
28    }
29
30    Ok(full_path)
31}
32
33#[derive(Debug, thiserror::Error)]
34pub enum Error {
35    #[error(transparent)]
36    Io(#[from] io::Error),
37    #[error(transparent)]
38    SetLog(#[from] log::SetLoggerError),
39    #[error(transparent)]
40    ParseLevel(#[from] log::ParseLevelError),
41}