blob: 92a0abc1073571b22964c3e46b7057bc0c13f71f [file] [log] [blame]
Alice Wang83c6aef2023-11-03 17:17:34 +00001// Copyright 2023, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Helpers for the watchdog module.
16
17/// This module provides helpers for simplified use of the watchdog module.
18#[cfg(feature = "watchdog")]
19pub mod watchdog {
20 use lazy_static::lazy_static;
21 use std::sync::Arc;
22 use std::time::Duration;
23 pub use watchdog_rs::WatchPoint;
24 use watchdog_rs::Watchdog;
25
26 lazy_static! {
27 /// A Watchdog thread, that can be used to create watch points.
28 static ref WD: Arc<Watchdog> = Watchdog::new(Duration::from_secs(10));
29 }
30
31 /// Sets a watch point with `id` and a timeout of `millis` milliseconds.
32 pub fn watch_millis(id: &'static str, millis: u64) -> Option<WatchPoint> {
33 Watchdog::watch(&WD, id, Duration::from_millis(millis))
34 }
35
36 /// Like `watch_millis` but with a callback that is called every time a report
37 /// is printed about this watch point.
38 pub fn watch_millis_with(
39 id: &'static str,
40 millis: u64,
41 callback: impl Fn() -> String + Send + 'static,
42 ) -> Option<WatchPoint> {
43 Watchdog::watch_with(&WD, id, Duration::from_millis(millis), callback)
44 }
45}
46
47/// This module provides empty/noop implementations of the watch dog utility functions.
48#[cfg(not(feature = "watchdog"))]
49pub mod watchdog {
50 /// Noop watch point.
51 pub struct WatchPoint();
52 /// Sets a Noop watch point.
53 fn watch_millis(_: &'static str, _: u64) -> Option<WatchPoint> {
54 None
55 }
56
57 pub fn watch_millis_with(
58 _: &'static str,
59 _: u64,
60 _: impl Fn() -> String + Send + 'static,
61 ) -> Option<WatchPoint> {
62 None
63 }
64}