logged_stream/record.rs
1use chrono::DateTime;
2use chrono::Utc;
3use std::fmt;
4
5//////////////////////////////////////////////////////////////////////////////////////////////////////////////
6// Record
7//////////////////////////////////////////////////////////////////////////////////////////////////////////////
8
9/// This structure represents a log record and contains message string, creation timestamp ([`DateTime`]<[`Utc`]>)
10/// and record kind ([`RecordKind`]).
11#[derive(Debug, Clone, PartialEq, Eq, Hash)]
12pub struct Record {
13 pub kind: RecordKind,
14 pub message: String,
15 pub time: DateTime<Utc>,
16}
17
18impl Record {
19 /// Construct a new instance of [`Record`] using provided message and kind.
20 pub fn new(kind: RecordKind, message: String) -> Self {
21 Self {
22 kind,
23 message,
24 time: Utc::now(),
25 }
26 }
27}
28
29//////////////////////////////////////////////////////////////////////////////////////////////////////////////
30// RecordKind
31//////////////////////////////////////////////////////////////////////////////////////////////////////////////
32
33/// This enumeration represents log record kind. It is contained inside [`Record`] and helps to determine
34/// how to work with log record message content which is different for each log record kind.
35#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
36pub enum RecordKind {
37 /// A manual marker emitted by the user via
38 /// [`LoggedStream::log_open`](crate::LoggedStream::log_open) — for example to record the start
39 /// of a connection. Never produced automatically.
40 Open,
41 /// Bytes were read from the wrapped stream.
42 Read,
43 /// Bytes were written to the wrapped stream.
44 Write,
45 /// A real IO error occurred while reading or writing (transient `WouldBlock` / `WriteZero`
46 /// conditions are skipped).
47 Error,
48 /// An asynchronous stream was shut down via `poll_shutdown`.
49 Shutdown,
50 /// The stream wrapper was dropped.
51 Drop,
52}
53
54impl fmt::Display for RecordKind {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 write!(f, "{}", char::from(*self))
57 }
58}
59
60impl From<RecordKind> for char {
61 fn from(kind: RecordKind) -> Self {
62 match kind {
63 RecordKind::Open => '+',
64 RecordKind::Read => '<',
65 RecordKind::Write => '>',
66 RecordKind::Error => '!',
67 RecordKind::Shutdown => '-',
68 RecordKind::Drop => 'x',
69 }
70 }
71}