1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! Hostname helper used by the syslog event formatters.
//!
//! `nix::unistd::gethostname` is already in the workspace and is the
//! cheapest way to obtain the kernel-reported hostname on Linux. The
//! return type is an `OsString`; we lossy-decode to `String` so the
//! formatters can drop it into a UTF-8 line without further error
//! handling.
use unistd;
/// Read the local hostname with a deterministic fallback.
///
/// On hosts where `gethostname(2)` succeeds the kernel-reported name is
/// returned (decoded with `OsString::to_string_lossy`). When the call
/// fails, or when the name is empty, we fall back to the literal string
/// `"localhost"` so the syslog `HOSTNAME` field is always a single
/// non-empty token. RFC 5424 section 6.2.4 requires HOSTNAME to consist
/// of one or more printable US-ASCII characters; to satisfy that we
/// also replace any whitespace or non-printable byte with `_`.
///
/// # Examples
///
/// ```
/// use dynomite::core::log::local_hostname;
/// let h = local_hostname();
/// assert!(!h.is_empty());
/// assert!(!h.contains(' '));
/// ```