error_log/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2#![forbid(unsafe_code)]
3#![warn(clippy::all, rustdoc::all, missing_docs)]
4/*!
5Library to store errors and log messages and display them later.
6
7book: www.lesnake.xyz/opt/error_log
8*/
9
10extern crate alloc;
11
12mod display;
13mod entry;
14mod get;
15mod helper;
16mod macros;
17mod manage;
18mod messages;
19mod presets;
20mod traits;
21
22macro_rules! if_std {
23    ($($i:item)*) => ($(
24        #[cfg(feature = "std")] $i
25    )*)
26}
27macro_rules! if_not_std {
28    ($($i:item)*) => ($(
29        #[cfg(not(feature = "std"))] $i
30    )*)
31}
32
33pub use crate::entry::{Entries, EntriesExt, Entry, EntryContent};
34use crate::helper::{format_unix_timestamp, instant_display_helper, now};
35pub use crate::presets::*;
36use alloc::{fmt::Debug, string::String, vec::Vec};
37use core::fmt::Display;
38pub use log::LevelFilter;
39pub(crate) use {if_not_std, if_std};
40if_std! {
41    pub use std::{println, print};
42}
43if_not_std! {
44    pub use libc_print::std_name::{println, print};
45}
46
47/**
48A trait Combining debug and display bounds
49*/
50pub trait DebugDisplay: Debug + Display {}
51
52impl<T: Debug + Display> DebugDisplay for T {}
53
54/**
55A Object to store multiple error messages and display them at once
56
57# Operations
58x:`ErrorLog`, E:`Error`, T:`ok` value, U:any type
59- `*x`: [`ok()`][Self::ok]/[`ok_mut()`][Self::ok_mut]: get (mutable) `ok` value as [`Option`]
60- `x += Result<U, E>`: Shorthand for [`push_result()`][Self::push_result]
61- `x += E`: Shorthand for [`push_err`()][Self::push_err]
62- `x *= Result <T, E>`: Shorthand for [`merge_result`()][Self::merge_result]
63*/
64#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
65pub struct ErrorLog<T, E> {
66    format_mode: FormatMode,
67    entries: Entries<E>,
68    #[cfg(feature = "instant-display")]
69    instant_display: bool,
70    delimiter: String,
71    join: bool,
72    max_level: LevelFilter,
73    max_level_used: LevelFilter,
74    ok: Option<T>,
75    display_fn: fn(LevelFilter, i64, String),
76}
77
78impl<T, E> Default for ErrorLog<T, E> {
79    fn default() -> Self {
80        Self {
81            ok: None,
82            entries: Vec::new(),
83            format_mode: FormatMode::default(),
84            display_fn: |lvl, timestamp, e| {
85                println!("{lvl} {}: {e}", format_unix_timestamp(timestamp))
86            },
87            max_level: LevelFilter::Trace,
88            delimiter: "".into(),
89            join: false,
90            max_level_used: LevelFilter::Off,
91            #[cfg(feature = "instant-display")]
92            instant_display: false,
93        }
94    }
95}
96
97#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
98/// How the error should be printed
99pub enum FormatMode {
100    /// Uses `{}` (Default)
101    #[default]
102    Normal,
103    /// Uses `{:?}`
104    Debug,
105    /// Uses `{:#?}`
106    PrettyDebug,
107}