pub enum WriteMode {
    Direct,
    SupportCapture,
    BufferAndFlush,
    BufferAndFlushWith(usize, Duration),
    BufferDontFlush,
    BufferDontFlushWith(usize),
    Async,
    AsyncWith {
        pool_capa: usize,
        message_capa: usize,
        flush_interval: Duration,
    },
}
Expand description

Describes whether the log output should be written synchronously or asynchronously, and if and how I/O should be buffered and flushed.

Is used in Logger::write_mode.

Buffering reduces the program’s I/O overhead, and thus increases overall performance, which can become relevant if logging is used heavily. On the other hand, if logging is used with low frequency, buffering can defer the appearance of log lines significantly, so regular flushing is usually advisable with buffering.

Note that for all options except Direct you should keep the LoggerHandle alive up to the very end of your program to ensure that all buffered log lines are flushed out (which happens automatically when the LoggerHandle is dropped) before the program terminates. See here for an example.

Note further that flushing uses an extra thread (with minimal stack).

The console is a slow output device (at least on Windows). With WriteMode::Async it can happen that in phases with vast log output the log lines appear significantly later than they were written. Also, a final printing phase is possible at the end of the program when the logger handle is dropped (and all output is flushed automatically).

WriteMode::Direct (i.e. without buffering) is the slowest option with all output devices, showing that buffered I/O pays off. But it takes slightly more resources, especially if you do not suppress flushing.

Using log_to_stdout() and then redirecting the output to a file makes things faster, but is still significantly slower than writing to files directly.

Variants§

§

Direct

Do not buffer (default).

Every log line is directly written to the output, without buffering. This allows seeing new log lines in real time, and does not need additional threads.

§

SupportCapture

Do not buffer and support cargo test’s capture.

Much like Direct, just a bit slower, and allows cargo test to capture log output and print it only for failing tests.

§

BufferAndFlush

Same as BufferAndFlushWith with default capacity (DEFAULT_BUFFER_CAPACITY) and default interval (DEFAULT_FLUSH_INTERVAL).

§

BufferAndFlushWith(usize, Duration)

Tuple Fields

§0: usize

Buffer capacity.

§1: Duration

Flush interval.

Buffer and flush with given buffer capacity and flush interval.

§

BufferDontFlush

Same as BufferDontFlushWith with default capacity (DEFAULT_BUFFER_CAPACITY).

§

BufferDontFlushWith(usize)

Tuple Fields

§0: usize

Buffer capacity.

Buffer with given buffer capacity, but don’t flush.

This might be handy if you want to minimize I/O effort and don’t want to create the extra thread for flushing and don’t care if log lines appear with delay.

§

Async

Available on crate feature async only.

Same as AsyncWith, using default values for all parameters.

§

AsyncWith

Fields

§pool_capa: usize

Capacity of the pool for the message buffers.

§message_capa: usize

Capacity of an individual message buffer.

§flush_interval: Duration

The interval for flushing the output.

With Duration::ZERO flushing is suppressed.

Available on crate feature async only.

Log lines are sent through an unbounded channel to an output thread, which does the I/O, and, if log_to_file() is chosen, also the rotation and the cleanup.

Uses buffered output to reduce overhead, and a bounded message pool to reduce allocations. The log output is flushed regularly with the given interval.

See here for an example.

Trait Implementations§

source§

impl Clone for WriteMode

source§

fn clone(&self) -> WriteMode

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for WriteMode

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq<WriteMode> for WriteMode

source§

fn eq(&self, other: &WriteMode) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for WriteMode

source§

impl Eq for WriteMode

source§

impl StructuralEq for WriteMode

source§

impl StructuralPartialEq for WriteMode

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more