tor_log_ratelim/lib.rs
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#![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_duration_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//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
45
46/// Implementation notes
47///
48/// We build our logging in a few layers.
49///
50/// At the lowest level, there is a [`Loggable`] trait, for events which can
51/// accumulate and eventually be flushed; this combines with the
52/// [`RateLim`](ratelim::RateLim) structure, which is responsible for managing
53/// the decision of when to flush these [`Loggable`]s.
54///
55/// The role of RateLim is to decide
56/// when to flush the information in a `Loggable`,
57/// and to flush the `Loggable` as needed.
58/// The role of a `Loggable` is to
59/// accumulate information
60/// and to know how to flush that information as a log message
61/// when it is told to do so.
62///
63/// One layer up, there is [`LogState`](logstate::LogState), which is used to to
64/// implement `Loggable` as used by [`log_ratelim!`].
65/// It can remember the name of an activity, accumulate
66/// successes and failures, and remember an error and associated message.
67///
68/// The highest layer is the [`log_ratelim!`] macro, which uses
69/// [`RateLim`](ratelim::RateLim) and [`LogState`](logstate::LogState) to record
70/// successes and failures, and launch background tasks as needed.
71mod implementation_notes {}
72
73mod logstate;
74mod macros;
75mod ratelim;
76
77use std::time::Duration;
78
79pub use ratelim::rt::{install_runtime, InstallRuntimeError};
80
81/// Re-exports for macros.
82#[doc(hidden)]
83pub mod macro_prelude {
84 pub use crate::{
85 logstate::LogState,
86 ratelim::{rt::rt_support, RateLim},
87 Activity, Loggable,
88 };
89 pub use once_cell::sync::Lazy;
90 pub use std::sync::{Arc, Mutex, Weak};
91 pub use tor_error::ErrorReport;
92 pub use tracing;
93 pub use weak_table::WeakValueHashMap;
94}
95
96/// A group of events that can be logged singly or in a summary over a period of time.
97#[doc(hidden)]
98pub trait Loggable: 'static + Send {
99 /// Log these events immediately, if there is anything to log.
100 ///
101 /// The `summarizing` argument is the amount of time that this `Loggable``
102 /// has been accumulating information.
103 ///
104 /// Implementations should return `Active` if they have logged that
105 /// some activity happened, and `Dormant` if they had nothing to log, or
106 /// if they are logging "I didn't see that problem for a while."
107 ///
108 /// After a `Loggable` has been dormant for a while, its timer will be reset.
109 fn flush(&mut self, summarizing: Duration) -> Activity;
110}
111
112/// A description of the whether a `Loggable` had something to say.
113#[doc(hidden)]
114#[derive(Clone, Copy, Debug, Eq, PartialEq)]
115#[allow(clippy::exhaustive_enums)] // okay, since this is doc(hidden).
116pub enum Activity {
117 /// There was a failure to report
118 Active,
119 /// There is nothing to report except perhaps a lack of failures.
120 Dormant,
121}