1use solar_interface::diagnostics::DiagCtxt;
4
5#[cfg(all(feature = "jemalloc", unix))]
6use tikv_jemallocator as _;
7
8cfg_if::cfg_if! {
11 if #[cfg(all(feature = "jemalloc", unix, not(debug_assertions)))] {
12 type AllocatorInner = tikv_jemallocator::Jemalloc;
13 } else {
14 type AllocatorInner = std::alloc::System;
15 }
16}
17
18cfg_if::cfg_if! {
19 if #[cfg(feature = "tracy-allocator")] {
20 pub(super) type WrappedAllocator = tracing_tracy::client::ProfiledAllocator<AllocatorInner>;
21 pub(super) const fn new_wrapped_allocator() -> WrappedAllocator {
22 Allocator::new(AllocatorInner {}, 100)
23 }
24 } else {
25 pub(super) type WrappedAllocator = AllocatorInner;
26 pub(super) const fn new_wrapped_allocator() -> WrappedAllocator {
27 AllocatorInner {}
28 }
29 }
30}
31
32pub type Allocator = WrappedAllocator;
34
35pub const fn new_allocator() -> Allocator {
37 new_wrapped_allocator()
38}
39
40pub fn init_logger() -> impl Sized {
42 match try_init_logger() {
43 Ok(guard) => guard,
44 Err(e) => DiagCtxt::new_early().fatal(e).emit(),
45 }
46}
47
48fn try_init_logger() -> Result<impl Sized, String> {
49 use tracing_subscriber::prelude::*;
50
51 let (profile_layer, guard) = match std::env::var("SOLAR_PROFILE").as_deref() {
52 Ok("chrome") => {
53 if !cfg!(feature = "tracing-chrome") {
54 return Err("chrome profiler support is not compiled in".to_string());
55 }
56 let (layer, guard) = chrome_layer();
57 (Some(layer.boxed()), Some(guard))
58 }
59 Ok("tracy") => {
60 if !cfg!(feature = "tracy") {
61 return Err("tracy profiler support is not compiled in".to_string());
62 }
63 (Some(tracy_layer().boxed()), Default::default())
64 }
65 Ok(s) => return Err(format!("unknown profiler '{s}'; valid values: 'chrome', 'tracy'")),
66 Err(_) => Default::default(),
67 };
68 tracing_subscriber::Registry::default()
69 .with(tracing_subscriber::EnvFilter::from_default_env())
70 .with(profile_layer)
71 .with(tracing_subscriber::fmt::layer())
72 .try_init()
73 .map(|()| guard)
74 .map_err(|e| e.to_string())
75}
76
77#[cfg(feature = "tracy")]
78fn tracy_layer() -> tracing_tracy::TracyLayer<impl tracing_tracy::Config> {
79 struct Config(tracing_subscriber::fmt::format::DefaultFields);
80 impl tracing_tracy::Config for Config {
81 type Formatter = tracing_subscriber::fmt::format::DefaultFields;
82 fn formatter(&self) -> &Self::Formatter {
83 &self.0
84 }
85 fn format_fields_in_zone_name(&self) -> bool {
86 false
87 }
88 }
89
90 tracing_tracy::client::register_demangler!();
91
92 tracing_tracy::TracyLayer::new(Config(Default::default()))
93}
94
95#[cfg(not(feature = "tracy"))]
96fn tracy_layer() -> tracing_subscriber::layer::Identity {
97 tracing_subscriber::layer::Identity::new()
98}
99
100#[cfg(feature = "tracing-chrome")]
101fn chrome_layer<S>() -> (tracing_chrome::ChromeLayer<S>, tracing_chrome::FlushGuard)
102where
103 S: tracing::Subscriber
104 + for<'span> tracing_subscriber::registry::LookupSpan<'span>
105 + Send
106 + Sync,
107{
108 tracing_chrome::ChromeLayerBuilder::new().include_args(true).build()
109}
110
111#[cfg(not(feature = "tracing-chrome"))]
112fn chrome_layer() -> (tracing_subscriber::layer::Identity, ()) {
113 (tracing_subscriber::layer::Identity::new(), ())
114}
115
116#[allow(dead_code)]
117pub(crate) fn env_to_bool(value: Option<&std::ffi::OsStr>) -> bool {
118 value.is_some_and(|value| value == "1" || value == "true")
119}