Coverage Report

Created: 2024-10-22 12:33

/build/source/nativelink-util/src/lib.rs
Line
Count
Source (jump to first uncovered line)
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 platform_properties;
32
pub mod proto_stream_utils;
33
pub mod resource_info;
34
pub mod retry;
35
pub mod store_trait;
36
pub mod task;
37
pub mod tls_utils;
38
pub mod write_counter;
39
40
// Re-export tracing mostly for use in macros.
41
pub use tracing as __tracing;
42
43
/// Initialize tracing.
44
301
pub fn init_tracing() -> Result<(), nativelink_error::Error> {
45
    use tracing_subscriber::prelude::*;
46
47
    static LOGGING_INITIALIZED: parking_lot::Mutex<bool> = parking_lot::Mutex::new(false);
48
301
    let mut logging_initized_guard = LOGGING_INITIALIZED.lock();
49
301
    if *logging_initized_guard {
  Branch (49:8): [True: 264, False: 37]
  Branch (49:8): [Folded - Ignored]
50
264
        return Err(nativelink_error::make_err!(
51
264
            nativelink_error::Code::Internal,
52
264
            "Logging already initialized"
53
264
        ));
54
37
    }
55
37
    *logging_initized_guard = true;
56
37
    let env_filter = tracing_subscriber::EnvFilter::builder()
57
37
        .with_default_directive(tracing::metadata::LevelFilter::WARN.into())
58
37
        .from_env_lossy();
59
37
60
37
    // Setup tracing logger for multiple format types, compact, json, and pretty as a single layer.
61
37
    // Configuration for log format comes from environment variable NL_LOG_FMT due to subscribers
62
37
    // being configured before config parsing.
63
37
    let nl_log_fmt = std::env::var("NL_LOG").unwrap_or_else(|_| "pretty".to_string());
64
37
    // Layers vector is used for due to how tracing_subscriber::fmt::layer builds type signature
65
37
    // not being able to unify a single trait type before being boxed. For example see
66
37
    // https://docs.rs/tracing-subscriber/0.3.18/tracing_subscriber/layer/index.html
67
37
    let mut layers = Vec::new();
68
37
    match nl_log_fmt.as_str() {
69
37
        "compact" => layers.push(
70
0
            tracing_subscriber::fmt::layer()
71
0
                .compact()
72
0
                .with_timer(tracing_subscriber::fmt::time::time())
73
0
                .with_filter(env_filter)
74
0
                .boxed(),
75
0
        ),
76
37
        "json" => layers.push(
77
0
            tracing_subscriber::fmt::layer()
78
0
                .json()
79
0
                .with_timer(tracing_subscriber::fmt::time::time())
80
0
                .with_filter(env_filter)
81
0
                .boxed(),
82
0
        ),
83
37
        _ => layers.push(
84
37
            tracing_subscriber::fmt::layer()
85
37
                .pretty()
86
37
                .with_timer(tracing_subscriber::fmt::time::time())
87
37
                .with_filter(env_filter)
88
37
                .boxed(),
89
37
        ),
90
    };
91
92
    // Add a console subscriber if the feature is enabled, see tokio-console for a client console.
93
    // https://crates.io/crates/tokio-console
94
37
    if cfg!(feature = "enable_tokio_console") {
95
0
        layers.push(console_subscriber::spawn().boxed());
96
37
    }
97
98
37
    tracing_subscriber::registry().with(layers).init();
99
37
    Ok(())
100
301
}