tor_log_ratelim/lib.rs
1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3// @@ begin lint list maintained by maint/add_warning @@
4#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
5#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
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::unchecked_time_subtraction)]
34#![deny(clippy::unnecessary_wraps)]
35#![warn(clippy::unseparated_literal_suffix)]
36#![deny(clippy::unwrap_used)]
37#![deny(clippy::mod_module_files)]
38#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
39#![allow(clippy::uninlined_format_args)]
40#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
41#![allow(clippy::result_large_err)] // temporary workaround for arti#587
42#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
43#![allow(clippy::needless_lifetimes)] // See arti#1765
44#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
45#![allow(clippy::collapsible_if)] // See arti#2342
46#![deny(clippy::unused_async)]
47#![deny(clippy::string_slice)] // See arti#2571
48//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
49
50/// Implementation notes
51///
52/// We build our logging in a few layers.
53///
54/// At the lowest level, there is a [`Loggable`] trait, for events which can
55/// accumulate and eventually be flushed; this combines with the
56/// [`RateLim`](ratelim::RateLim) structure, which is responsible for managing
57/// the decision of when to flush these [`Loggable`]s.
58///
59/// The role of RateLim is to decide
60/// when to flush the information in a `Loggable`,
61/// and to flush the `Loggable` as needed.
62/// The role of a `Loggable` is to
63/// accumulate information
64/// and to know how to flush that information as a log message
65/// when it is told to do so.
66///
67/// One layer up, there is [`LogState`](logstate::LogState), which is used to to
68/// implement `Loggable` as used by [`log_ratelim!`].
69/// It can remember the name of an activity, accumulate
70/// successes and failures, and remember an error and associated message.
71///
72/// The highest layer is the [`log_ratelim!`] macro, which uses
73/// [`RateLim`](ratelim::RateLim) and [`LogState`](logstate::LogState) to record
74/// successes and failures, and launch background tasks as needed.
75mod implementation_notes {}
76
77mod logstate;
78mod macros;
79mod ratelim;
80
81use std::time::Duration;
82
83pub use ratelim::rt::{InstallRuntimeError, install_runtime};
84
85/// Re-exports for macros.
86#[doc(hidden)]
87pub mod macro_prelude {
88 pub use crate::{
89 Activity, Loggable,
90 logstate::LogState,
91 ratelim::{RateLim, rt::rt_support},
92 };
93 pub use std::sync::LazyLock;
94 pub use std::sync::{Arc, Mutex, Weak};
95 pub use tor_error::ErrorReport;
96 pub use tracing;
97
98 /// Alias to force use of RandomState, regardless of features enabled in `weak_tables`.
99 ///
100 /// See <https://github.com/tov/weak-table-rs/issues/23> for discussion.
101 pub type WeakValueHashMap<K, V> = weak_table::WeakValueHashMap<K, V, std::hash::RandomState>;
102}
103
104/// A group of events that can be logged singly or in a summary over a period of time.
105#[doc(hidden)]
106pub trait Loggable: 'static + Send {
107 /// Log these events immediately, if there is anything to log.
108 ///
109 /// The `summarizing` argument is the amount of time that this `Loggable``
110 /// has been accumulating information.
111 ///
112 /// Implementations should return `Active` if they have logged that
113 /// some activity happened, and `Dormant` if they had nothing to log, or
114 /// if they are logging "I didn't see that problem for a while."
115 ///
116 /// After a `Loggable` has been dormant for a while, its timer will be reset.
117 fn flush(&mut self, summarizing: Duration) -> Activity;
118}
119
120/// A description of the whether a `Loggable` had something to say.
121#[doc(hidden)]
122#[derive(Clone, Copy, Debug, Eq, PartialEq)]
123#[allow(clippy::exhaustive_enums)] // okay, since this is doc(hidden).
124pub enum Activity {
125 /// There was a failure to report
126 Active,
127 /// There is nothing to report (no successes or failures).
128 Dormant,
129 /// There is a possible resolution to report (>0 successes and no failures).
130 AppearsResolved,
131}