Skip to content

Commit

Permalink
docs: add keyword, and add docs for rolling file structs
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Aug 2, 2024
1 parent 4c7f006 commit d42346d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ repository = "https://github.com/tisonkun/logforth"
rust-version = "1.71.0"
version = "0.7.2"

categories = [
"development-tools::debugging",
]
keywords = ["logging", "log", "opentelemetry", "fastrace"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docs"]
Expand Down
15 changes: 15 additions & 0 deletions src/append/rolling_file/non_blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ use crossbeam_channel::Sender;
use crate::append::rolling_file::worker::Worker;
use crate::append::rolling_file::Message;

/// A guard that flushes log records associated to a [`NonBlocking`] on a drop.
///
/// Writing to a [`NonBlocking`] writer will **not** immediately write the log record to the
/// underlying output. Instead, the log record will be written by a dedicated logging thread at
/// some later point. To increase throughput, the non-blocking writer will flush to the underlying
/// output on a periodic basis rather than every time a log record is written. This means that if
/// the program terminates abruptly (such as through an uncaught `panic` or a `std::process::exit`),
/// some log records may not be written.
///
/// Since logs near a crash are often necessary for diagnosing the failure, `WorkerGuard` provides a
/// mechanism to ensure that _all_ buffered logs are flushed to their output. `WorkerGuard` should
/// be assigned in the `main` function or whatever the entrypoint of the program is. This will
/// ensure that the guard will be dropped during an unwinding or when `main` exits successfully.
#[derive(Debug)]
pub struct WorkerGuard {
_guard: Option<JoinHandle<()>>,
Expand Down Expand Up @@ -72,6 +85,7 @@ impl Drop for WorkerGuard {
}
}

/// A non-blocking, off-thread writer.
#[derive(Clone, Debug)]
pub struct NonBlocking {
sender: Sender<Message>,
Expand Down Expand Up @@ -110,6 +124,7 @@ impl NonBlocking {
}
}

/// A builder for [`NonBlocking`].
#[derive(Debug)]
pub struct NonBlockingBuilder {
thread_name: String,
Expand Down
8 changes: 8 additions & 0 deletions src/append/rolling_file/rolling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use time::Duration;
use time::OffsetDateTime;
use time::Time;

/// A file writer with the ability to rotate log files at a fixed schedule.
#[derive(Debug)]
pub struct RollingFileWriter {
state: State,
Expand Down Expand Up @@ -63,6 +64,7 @@ impl Write for RollingFileWriter {
}
}

/// A builder for [`RollingFileWriter`].
#[derive(Debug)]
pub struct RollingFileWriterBuilder {
rotation: Rotation,
Expand Down Expand Up @@ -313,12 +315,18 @@ fn open_file(dir: &Path, filename: &str) -> anyhow::Result<File> {
.context("failed to create log file")
}

/// Defines a fixed period for rolling of a log file.
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum Rotation {
/// Minutely Rotation
Minutely,
/// Hourly Rotation
Hourly,
/// Daily Rotation
Daily,
/// No Rotation
Never,
// TODO(tisonkun): consider support rotating on file size exceeding a threshold.
}

impl Rotation {
Expand Down

0 comments on commit d42346d

Please sign in to comment.