1#![cfg(feature = "server")]
20
21use chrono::{DateTime, Utc};
22
23pub struct BuildInfo {
25 pub version: &'static str,
27 pub git_describe: &'static str,
29 pub git_hash: &'static str,
31 pub commit_date: &'static str,
33 pub rustc_version: &'static str,
35 pub build_time: &'static str,
37}
38
39pub static BUILD_INFO: BuildInfo = BuildInfo {
41 version: env!("CARGO_PKG_VERSION"),
42 git_describe: env!("YGG_BUILD_GIT_DESCRIBE"),
43 git_hash: env!("YGG_BUILD_GIT_HASH"),
44 commit_date: env!("YGG_BUILD_GIT_COMMIT_DATE"),
45 rustc_version: env!("YGG_BUILD_RUSTC_VERSION"),
46 build_time: env!("YGG_BUILD_TIME"),
47};
48
49pub fn log_build_info() {
54 let built_at = BUILD_INFO
56 .build_time
57 .parse::<i64>()
58 .ok()
59 .and_then(|ts| DateTime::<Utc>::from_timestamp(ts, 0))
60 .map(|dt| dt.to_rfc3339())
61 .unwrap_or_else(|| BUILD_INFO.build_time.to_string());
62
63 tracing::info!(
64 "build: version={} git={}",
65 BUILD_INFO.version,
66 BUILD_INFO.git_describe
67 );
68 tracing::info!(
69 "build: commit={} date={}",
70 BUILD_INFO.git_hash,
71 BUILD_INFO.commit_date
72 );
73 tracing::info!("build: rustc={}", BUILD_INFO.rustc_version);
74 tracing::info!("build: built_at={}", built_at);
75}
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80
81 #[test]
82 fn build_info_fields_are_populated() {
83 assert!(!BUILD_INFO.version.is_empty());
86 assert!(!BUILD_INFO.git_describe.is_empty());
88 }
89
90 #[test]
91 fn build_time_parses_as_unix_seconds() {
92 let parsed = BUILD_INFO.build_time.parse::<i64>();
94 assert!(
95 parsed.is_ok(),
96 "build_time not a unix second: {}",
97 BUILD_INFO.build_time
98 );
99 assert!(
100 parsed.unwrap() > 1_600_000_000,
101 "build_time implausibly old"
102 );
103 }
104
105 #[test]
106 fn log_build_info_does_not_panic() {
107 log_build_info();
109 }
110}