1
#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg))]
2
#![doc = include_str!("../README.md")]
3
// @@ begin lint list maintained by maint/add_warning @@
4
#![cfg_attr(not(ci_arti_stable), allow(renamed_and_removed_lints))]
5
#![cfg_attr(not(ci_arti_nightly), allow(unknown_lints))]
6
#![warn(missing_docs)]
7
#![warn(noop_method_call)]
8
#![warn(unreachable_pub)]
9
#![warn(clippy::all)]
10
#![deny(clippy::await_holding_lock)]
11
#![deny(clippy::cargo_common_metadata)]
12
#![deny(clippy::cast_lossless)]
13
#![deny(clippy::checked_conversions)]
14
#![warn(clippy::cognitive_complexity)]
15
#![deny(clippy::debug_assert_with_mut_call)]
16
#![deny(clippy::exhaustive_enums)]
17
#![deny(clippy::exhaustive_structs)]
18
#![deny(clippy::expl_impl_clone_on_copy)]
19
#![deny(clippy::fallible_impl_from)]
20
#![deny(clippy::implicit_clone)]
21
#![deny(clippy::large_stack_arrays)]
22
#![warn(clippy::manual_ok_or)]
23
#![deny(clippy::missing_docs_in_private_items)]
24
#![warn(clippy::needless_borrow)]
25
#![warn(clippy::needless_pass_by_value)]
26
#![warn(clippy::option_option)]
27
#![deny(clippy::print_stderr)]
28
#![deny(clippy::print_stdout)]
29
#![warn(clippy::rc_buffer)]
30
#![deny(clippy::ref_option_ref)]
31
#![warn(clippy::semicolon_if_nothing_returned)]
32
#![warn(clippy::trait_duplication_in_bounds)]
33
#![deny(clippy::unnecessary_wraps)]
34
#![warn(clippy::unseparated_literal_suffix)]
35
#![deny(clippy::unwrap_used)]
36
#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
37
#![allow(clippy::uninlined_format_args)]
38
#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
39
#![allow(clippy::result_large_err)] // temporary workaround for arti#587
40
#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
41
//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
42

            
43
/// Implementation notes
44
///
45
/// We build our logging in a few layers.
46
///
47
/// At the lowest level, there is a [`Loggable`] trait, for events which can
48
/// accumulate and eventually be flushed; this combines with the
49
/// [`RateLim`](ratelim::RateLim) structure, which is responsible for managing
50
/// the decision of when to flush these [`Loggable`]s.
51
///
52
/// The role of RateLim is to decide
53
/// when to flush the information in a `Loggable`,
54
/// and to flush the `Loggable` as needed.
55
/// The role of a `Loggable` is to
56
/// accumulate information
57
/// and to know how to flush that information as a log message
58
/// when it is told to do so.
59
///
60
/// One layer up, there is [`LogState`](logstate::LogState), which is used to to
61
/// implement `Loggable` as used by [`log_ratelim!`].
62
/// It can remember the name of an activity, accumulate
63
/// successes and failures, and remember an error and associated message.
64
///
65
/// The highest layer is the [`log_ratelim!`] macro, which uses
66
/// [`RateLim`](ratelim::RateLim) and [`LogState`](logstate::LogState) to record
67
/// successes and failures, and launch background tasks as needed.
68
mod implementation_notes {}
69

            
70
mod logstate;
71
mod macros;
72
mod ratelim;
73

            
74
use std::time::Duration;
75

            
76
pub use ratelim::rt::{install_runtime, InstallRuntimeError};
77

            
78
/// Re-exports for macros.
79
#[doc(hidden)]
80
pub mod macro_prelude {
81
    pub use crate::{
82
        logstate::LogState,
83
        ratelim::{rt::rt_support, RateLim},
84
        Activity, Loggable,
85
    };
86
    pub use once_cell::sync::Lazy;
87
    pub use std::sync::{Arc, Mutex, Weak};
88
    pub use tor_error::ErrorReport;
89
    pub use tracing;
90
    pub use weak_table::WeakValueHashMap;
91
}
92

            
93
/// An group of events that an be logged singly or in a summary over a period of time.
94
#[doc(hidden)]
95
pub trait Loggable: 'static + Send {
96
    /// Log these events immediately, if there is anything to log.
97
    ///
98
    /// The `summarizing` argument is the amount of time that this `Loggable``
99
    /// has been accumulating information.
100
    ///
101
    /// Implementations should return `Active` if they have logged that
102
    /// some activity happened, and `Dormant` if they had nothing to log, or
103
    /// if they are logging "I didn't see that problem for a while."
104
    ///
105
    ///  After a `Loggable` has been dormant for a while, its timer will be reset.
106
    fn flush(&mut self, summarizing: Duration) -> Activity;
107
}
108

            
109
/// A description of the whether a `Loggable` had something to say.
110
#[doc(hidden)]
111
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
112
#[allow(clippy::exhaustive_enums)] // okay, since this is doc(hidden).
113
pub enum Activity {
114
    /// There was a failure to report
115
    Active,
116
    /// There is nothing to report except perhaps a lack of failures.
117
    Dormant,
118
}