/build/source/nativelink-util/src/lib.rs
Line | Count | Source |
1 | | // Copyright 2024 The NativeLink Authors. All rights reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | pub mod action_messages; |
16 | | pub mod buf_channel; |
17 | | pub mod channel_body_for_tests; |
18 | | pub mod chunked_stream; |
19 | | pub mod common; |
20 | | pub mod connection_manager; |
21 | | pub mod digest_hasher; |
22 | | pub mod evicting_map; |
23 | | pub mod fastcdc; |
24 | | pub mod fs; |
25 | | pub mod health_utils; |
26 | | pub mod instant_wrapper; |
27 | | pub mod known_platform_property_provider; |
28 | | pub mod metrics_utils; |
29 | | pub mod operation_state_manager; |
30 | | pub mod origin_context; |
31 | | pub mod origin_event; |
32 | | pub mod origin_event_middleware; |
33 | | pub mod origin_event_publisher; |
34 | | pub mod platform_properties; |
35 | | pub mod proto_stream_utils; |
36 | | pub mod resource_info; |
37 | | pub mod retry; |
38 | | pub mod shutdown_guard; |
39 | | pub mod store_trait; |
40 | | pub mod task; |
41 | | pub mod tls_utils; |
42 | | pub mod write_counter; |
43 | | |
44 | | // Re-export tracing mostly for use in macros. |
45 | | pub use tracing as __tracing; |
46 | | |
47 | | /// Initialize tracing. |
48 | 301 | pub fn init_tracing() -> Result<(), nativelink_error::Error> { |
49 | | use tracing_subscriber::prelude::*; |
50 | | |
51 | | static LOGGING_INITIALIZED: parking_lot::Mutex<bool> = parking_lot::Mutex::new(false); |
52 | 301 | let mut logging_initized_guard = LOGGING_INITIALIZED.lock(); |
53 | 301 | if *logging_initized_guard { Branch (53:8): [True: 263, False: 38]
Branch (53:8): [Folded - Ignored]
|
54 | 263 | return Err(nativelink_error::make_err!( |
55 | 263 | nativelink_error::Code::Internal, |
56 | 263 | "Logging already initialized" |
57 | 263 | )); |
58 | 38 | } |
59 | 38 | *logging_initized_guard = true; |
60 | 38 | let env_filter = tracing_subscriber::EnvFilter::builder() |
61 | 38 | .with_default_directive(tracing::metadata::LevelFilter::WARN.into()) |
62 | 38 | .from_env_lossy(); |
63 | 38 | |
64 | 38 | // Setup tracing logger for multiple format types, compact, json, and pretty as a single layer. |
65 | 38 | // Configuration for log format comes from environment variable NL_LOG_FMT due to subscribers |
66 | 38 | // being configured before config parsing. |
67 | 38 | let nl_log_fmt = std::env::var("NL_LOG").unwrap_or_else(|_| "pretty".to_string()); |
68 | 38 | // Layers vector is used for due to how tracing_subscriber::fmt::layer builds type signature |
69 | 38 | // not being able to unify a single trait type before being boxed. For example see |
70 | 38 | // https://docs.rs/tracing-subscriber/0.3.18/tracing_subscriber/layer/index.html |
71 | 38 | let mut layers = Vec::new(); |
72 | 38 | match nl_log_fmt.as_str() { |
73 | 38 | "compact" => layers.push( |
74 | 0 | tracing_subscriber::fmt::layer() |
75 | 0 | .compact() |
76 | 0 | .with_timer(tracing_subscriber::fmt::time::time()) |
77 | 0 | .with_filter(env_filter) |
78 | 0 | .boxed(), |
79 | 0 | ), |
80 | 38 | "json" => layers.push( |
81 | 0 | tracing_subscriber::fmt::layer() |
82 | 0 | .json() |
83 | 0 | .with_timer(tracing_subscriber::fmt::time::time()) |
84 | 0 | .with_filter(env_filter) |
85 | 0 | .boxed(), |
86 | 0 | ), |
87 | 38 | _ => layers.push( |
88 | 38 | tracing_subscriber::fmt::layer() |
89 | 38 | .pretty() |
90 | 38 | .with_timer(tracing_subscriber::fmt::time::time()) |
91 | 38 | .with_filter(env_filter) |
92 | 38 | .boxed(), |
93 | 38 | ), |
94 | | }; |
95 | | |
96 | | // Add a console subscriber if the feature is enabled, see tokio-console for a client console. |
97 | | // https://crates.io/crates/tokio-console |
98 | 38 | if cfg!(feature = "enable_tokio_console") { |
99 | 0 | layers.push(console_subscriber::spawn().boxed()); |
100 | 38 | } |
101 | | |
102 | 38 | tracing_subscriber::registry().with(layers).init(); |
103 | 38 | Ok(()) |
104 | 301 | } |