fusio_log/
option.rs

1use std::sync::Arc;
2
3use fusio::DynFs;
4pub use fusio_dispatch::FsOptions;
5use futures_core::TryStream;
6
7use crate::{error::LogError, Decode, Encode, Logger};
8
9pub(crate) const DEFAULT_BUF_SIZE: usize = 4 * 1024;
10
11#[cfg(not(any(
12    feature = "tokio",
13    feature = "web",
14    feature = "monoio",
15    feature = "aws"
16)))]
17compile_error!("one of these features must be enabled: tokio, monoio, web, aws");
18
19#[cfg(all(
20    feature = "aws",
21    not(any(feature = "tokio-http", feature = "web-http", feature = "monoio-http"))
22))]
23compile_error!("aws feature must be used with tokio-http, monoio-http or web-http feature");
24
25#[derive(Clone)]
26pub struct Options {
27    pub(crate) path: fusio::path::Path,
28    pub(crate) buf_size: usize,
29    pub(crate) fs_option: FsOptions,
30    pub(crate) truncate: bool,
31}
32
33impl Options {
34    /// Create a new log options.
35    #[cfg(any(feature = "tokio", feature = "web", feature = "monoio"))]
36    pub fn new(path: fusio::path::Path) -> Self {
37        Self {
38            path,
39            buf_size: DEFAULT_BUF_SIZE,
40            fs_option: FsOptions::Local,
41            truncate: false,
42        }
43    }
44
45    /// Create a new log options with [`FsOptions`].
46    ///
47    /// See [`FsOptions`] for more details.
48    pub fn with_fs_options(path: fusio::path::Path, fs_option: FsOptions) -> Self {
49        Self {
50            path,
51            buf_size: DEFAULT_BUF_SIZE,
52            fs_option,
53            truncate: false,
54        }
55    }
56
57    /// Disable buffer for the log. It is recommended to keep the buffer enabled and use
58    /// [`Logger::flush`] to flush the data to the log file.
59    pub fn disable_buf(self) -> Self {
60        Self {
61            buf_size: 0,
62            ..self
63        }
64    }
65
66    /// Set the buffer size for the log. Default is 4K.
67    pub fn buf_size(self, buf_size: usize) -> Self {
68        Self { buf_size, ..self }
69    }
70
71    /// Set the filesystem options for the log.
72    ///
73    /// See [`FsOptions`] for more details.
74    pub fn fs(self, fs_option: FsOptions) -> Self {
75        Self { fs_option, ..self }
76    }
77
78    /// Open log file with truncate option.
79    pub fn truncate(self, truncate: bool) -> Self {
80        Self { truncate, ..self }
81    }
82
83    /// Open the log file. Return error if open file failed.
84    pub async fn build<T>(self) -> Result<Logger<T>, LogError>
85    where
86        T: Encode,
87    {
88        let logger = Logger::<T>::new(self).await?;
89        Ok(logger)
90    }
91
92    /// Open the log with the given [`DynFs`]. Return error if open file failed.
93    pub async fn build_with_fs<T>(self, fs: Arc<dyn DynFs>) -> Result<Logger<T>, LogError>
94    where
95        T: Encode,
96    {
97        let logger = Logger::<T>::with_fs(fs, self).await?;
98        Ok(logger)
99    }
100
101    /// Recover the log from existing log file. Return a stream of log entries.
102    pub async fn recover<T>(
103        self,
104    ) -> Result<impl TryStream<Ok = Vec<T>, Error = LogError> + Unpin, LogError>
105    where
106        T: Decode,
107    {
108        Logger::<T>::recover(self).await
109    }
110
111    /// Recover the log from the given [`DynFs`] and [`Options`]. Return a stream of log entries.
112    pub async fn recover_with_fs<T>(
113        self,
114        fs: Arc<dyn DynFs>,
115    ) -> Result<impl TryStream<Ok = Vec<T>, Error = LogError> + Unpin, LogError>
116    where
117        T: Decode,
118    {
119        Logger::<T>::recover_with_fs(&self.path, fs).await
120    }
121}