Skip to main content

Instance

Struct Instance 

Source
pub struct Instance { /* private fields */ }
Available on crate feature 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

Source

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.

Source

pub fn id(&self) -> InstanceId

The instance id, a ULID: sortable by start time, unique without coordination.

Source

pub fn started_at(&self) -> SystemTime

When the process started (the wall clock at the first start()).

Source

pub fn uptime(&self) -> Duration

How long ago it started, by the monotonic clock: wall-clock steps do not affect it.

Source

pub fn host(&self) -> &str

The host name at start, or "unknown". An identifying value.

Source

pub fn pid(&self) -> u32

The process id at start.

Source

pub fn user_at_start(&self) -> &str

The effective user at start, before any privilege drop, or "unknown". The current user is process::user().

Source

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.

Source

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="));
Source

pub fn write_json(&self, out: &mut impl Write) -> Result

Writes one JSON object, without allocating.

Trait Implementations§

Source§

impl Debug for Instance

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Instance

01J9ZQ4M8T6Y2K5V3W1X7R0B9C web-01 pid 48213 up 3h12m04s.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.