Skip to content

Commit 31a75dc

Browse files
committed
Add optional tracing capabilities
1 parent bd218af commit 31a75dc

File tree

3 files changed

+80
-18
lines changed

3 files changed

+80
-18
lines changed

Diff for: Cargo.lock

+51
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ cxx-build = "~1.0"
1919
cxx = "~1.0"
2020
thiserror = "~1.0"
2121
chrono = "~0.4"
22+
tracing = { version = "~0.1", optional = true }
2223

2324
[dev-dependencies]
2425
argh = "0.1.7"

Diff for: src/lib.rs

+28-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use chrono::{DateTime, NaiveDateTime, Utc};
2+
use cxx::{self, SharedPtr};
13
///! Accurate distributed timestamps.
24
///!
35
///! Bindings to [clockkit](https://github.com/camilleg/clockkit).
4-
///! Currently requires `nightly` for the use of `feature(atomic_mut_ptr)`.
56
///!
67
///! Clockkit provides timestamps to distributed networked PCs
78
///! with guaranteed bounds on latency and jitter, typically under 10 microseconds,
@@ -16,11 +17,7 @@
1617
///! Originally created for a full-motion
1718
///! [driving simulator](https://web.archive.org/web/20170517201424/http://www.isl.uiuc.edu/Labs/Driving%20Simulator/Driving%20Simulator.html)
1819
///! with eye tracking and a quickly churning set of other sensors and outputs, for over a decade.
19-
20-
use std::{sync::Mutex, thread::JoinHandle, time::Duration, path::Path};
21-
22-
use chrono::{DateTime, NaiveDateTime, Utc};
23-
use cxx::{self, SharedPtr};
20+
use std::{fmt::Debug, path::Path, sync::Mutex, thread::JoinHandle, time::Duration};
2421
use thiserror::Error;
2522

2623
/// Things that can go wrong.
@@ -36,8 +33,8 @@ pub enum Error {
3633
#[error("Overflow")]
3734
Overflow,
3835
/// Invalid value.
39-
#[error("Invalid value")]
40-
Invalid,
36+
#[error("Invalid value: {0}")]
37+
Invalid(i64),
4138
/// Could not read config file.
4239
#[error("Could not read config file")]
4340
ConfigRead(#[from] std::io::Error),
@@ -51,7 +48,6 @@ pub enum Error {
5148

5249
// Obviously invalid values. 9223372036854775807 usec, or 293,000 years.
5350
const USEC_INVALID: i64 = i64::MAX;
54-
// constexpr tp tpInvalid = TpFromUsec(usecInvalid);
5551

5652
#[cxx::bridge]
5753
mod ffi {
@@ -64,6 +60,7 @@ mod ffi {
6460
/// .port(1234)
6561
/// .build_clock();
6662
#[namespace = "bridge"]
63+
#[derive(Debug)]
6764
struct ConfigReader {
6865
server: String,
6966
port: u16,
@@ -123,13 +120,14 @@ impl ffi::ConfigReader {
123120
/// phasePanic:5000
124121
/// updatePanic:5000000
125122
/// ```
123+
#[cfg_attr(feature="tracing", tracing::instrument(level = "INFO", fields(path=%path.as_ref().display())))]
126124
pub fn from_config_file(path: impl AsRef<Path>) -> Result<Self, Error> {
127125
let mut res = Self::default();
128126

129127
let config = std::fs::read_to_string(path.as_ref())?;
130128
for line in config.lines() {
131129
if line.starts_with('#') {
132-
continue
130+
continue;
133131
}
134132
let mut parts = line.trim().splitn(2, ':');
135133
if let Some(key) = parts.next() {
@@ -149,6 +147,8 @@ impl ffi::ConfigReader {
149147
}
150148
}
151149
}
150+
#[cfg(feature = "tracing")]
151+
tracing::debug!(config=?res, "Read config from file");
152152
Ok(res)
153153
}
154154

@@ -206,9 +206,10 @@ pub struct PhaseLockedClock {
206206
}
207207

208208
/// Helper function to create a NaiveDateTime from a timestamp in μs.
209+
#[cfg_attr(feature = "tracing", tracing::instrument(level = "DEBUG"))]
209210
fn make_timestamp(usec: i64) -> Result<NaiveDateTime, Error> {
210211
if usec == USEC_INVALID {
211-
return Err(Error::Invalid);
212+
return Err(Error::Invalid(usec));
212213
}
213214
let sec: i64 = usec / 1_000_000;
214215
let usec: Result<u32, _> = match (usec % 1_000_000).try_into() {
@@ -232,19 +233,27 @@ impl PhaseLockedClock {
232233
}
233234

234235
/// Run the PLC in its own thread
236+
#[cfg_attr(feature = "tracing", tracing::instrument(level = "INFO", skip_all))]
235237
pub fn start(&self) {
236238
if let Ok(mut guard) = self.handle.lock() {
237239
// Only start the clock if there is no handle present, otherwise it's running.
238240
if (*guard).is_none() {
239241
let plc = self.ptr.clone();
240-
*guard = Some(std::thread::spawn(move || ffi::run1(plc)))
242+
*guard = Some(std::thread::spawn(move || ffi::run1(plc)));
243+
244+
#[cfg(feature = "tracing")]
245+
tracing::info!("PhaseLockedClock started");
246+
} else {
247+
#[cfg(feature = "tracing")]
248+
tracing::warn!("PhaseLockedClock is already running");
241249
}
242250
} else {
243251
panic!("Unable to start PhaseLockedClock due to poisened mutex");
244252
};
245253
}
246254

247255
/// Stop the PLC.
256+
#[cfg_attr(feature = "tracing", tracing::instrument(level = "INFO", skip_all))]
248257
pub fn stop(&self) {
249258
let plc = self.ptr.clone();
250259
ffi::cancel(plc);
@@ -255,6 +264,7 @@ impl PhaseLockedClock {
255264
/// phasePanic: A PhaseLockedClock whose offset exceeds this,
256265
/// relative to its reference clock, declares itself out of sync.
257266
/// Default: 5ms
267+
#[cfg_attr(feature = "tracing", tracing::instrument(level = "INFO", skip_all))]
258268
pub fn set_phase_panic(&mut self, dur: Duration) {
259269
let dur = dur
260270
.as_micros()
@@ -264,11 +274,12 @@ impl PhaseLockedClock {
264274
ffi::setPhasePanic(plc, dur)
265275
}
266276

267-
/// Set the threshold for the update panic.
268-
///
269-
/// updatePanic: A PhaseLockedClock that hasn't updated successfully
270-
/// for longer than this declares itself out of sync.
271-
/// Default: 5s
277+
/// Set the threshold for the update panic.
278+
///
279+
/// updatePanic: A PhaseLockedClock that hasn't updated successfully
280+
/// for longer than this declares itself out of sync.
281+
/// Default: 5s
282+
#[cfg_attr(feature = "tracing", tracing::instrument(level = "INFO", skip_all))]
272283
pub fn set_update_panic(&mut self, dur: Duration) {
273284
let dur = dur
274285
.as_micros()
@@ -304,4 +315,3 @@ impl Drop for PhaseLockedClock {
304315
unsafe impl Send for ffi::PhaseLockedClock {}
305316

306317
unsafe impl Sync for ffi::PhaseLockedClock {}
307-

0 commit comments

Comments
 (0)