pub struct Instance { /* private fields */ }instance only.Expand description
The running copy of this program. Created once per process by
Instance::start; every caller sees the same value.
Implementations§
Source§impl Instance
impl Instance
Sourcepub fn start() -> &'static Instance
pub fn start() -> &'static Instance
Creates the instance on the first call and returns it on every call.
Never fails: an unreadable host or user name becomes "unknown". Safe to
call from many threads at once; exactly one instance is created (E-62).
Cost: the first call creates a ULID and reads the host and user names (5 to 20 µs [est.]); later calls about 1 ns.
Sourcepub fn id(&self) -> InstanceId
pub fn id(&self) -> InstanceId
The instance id, a ULID: sortable by start time, unique without coordination.
Sourcepub fn started_at(&self) -> SystemTime
pub fn started_at(&self) -> SystemTime
When the process started (the wall clock at the first start()).
Sourcepub fn uptime(&self) -> Duration
pub fn uptime(&self) -> Duration
How long ago it started, by the monotonic clock: wall-clock steps do not affect it.
Sourcepub fn user_at_start(&self) -> &str
pub fn user_at_start(&self) -> &str
The effective user at start, before any privilege drop, or "unknown".
The current user is process::user().
Sourcepub fn is_current_process(&self) -> bool
pub fn is_current_process(&self) -> bool
False in a child created by fork() without exec, which inherits the
parent’s instance: a forked worker must not log under the parent’s id.
Sourcepub fn write_kv(&self, out: &mut impl Write) -> Result
pub fn write_kv(&self, out: &mut impl Write) -> Result
Writes instance.id=... instance.started_at=... ..., without allocating.
let mut line = String::new();
sys_lib::Instance::start().write_kv(&mut line)?;
assert!(line.starts_with("instance.id="));Sourcepub fn write_json(&self, out: &mut impl Write) -> Result
pub fn write_json(&self, out: &mut impl Write) -> Result
Writes one JSON object, without allocating.