Skip to main content

flareapp_core/
process.rs

1//! The command line this process was started with.
2
3use std::env;
4use std::sync::OnceLock;
5
6/// Returns the arguments this process was started with, starting with the
7/// program.
8///
9/// Arguments that are not valid UTF-8 get replacement characters. `env::args()`
10/// panics on them, and the Flare client must never crash the app.
11pub fn command_args() -> &'static [String] {
12    static ARGS: OnceLock<Vec<String>> = OnceLock::new();
13
14    ARGS.get_or_init(|| {
15        env::args_os()
16            .map(|argument| argument.to_string_lossy().into_owned())
17            .collect()
18    })
19}
20
21/// Returns the file name of this process's executable, like `my-app`, or
22/// `my-app.exe` on Windows. OpenTelemetry calls it `process.executable.name`.
23/// Returns `None` when the operating system does not tell.
24///
25/// Used by the Flare integrations. Not part of the public API.
26pub fn executable_name() -> Option<&'static str> {
27    static NAME: OnceLock<Option<String>> = OnceLock::new();
28
29    NAME.get_or_init(|| {
30        let executable = env::current_exe().ok()?;
31        let name = executable.file_name()?;
32
33        Some(name.to_string_lossy().into_owned())
34    })
35    .as_deref()
36}
37
38/// Returns the command line of this process: the arguments joined by spaces.
39pub fn command_line() -> &'static str {
40    static LINE: OnceLock<String> = OnceLock::new();
41
42    LINE.get_or_init(|| command_args().join(" "))
43}