vmm/logger/
mod.rs

1// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Crate that implements Firecracker specific functionality as far as logging and metrics
5//! collecting.
6
7mod logging;
8mod metrics;
9
10pub use log::{Level, debug, error, info, log_enabled, trace, warn};
11pub use logging::{
12    DEFAULT_INSTANCE_ID, DEFAULT_LEVEL, INSTANCE_ID, LOGGER, LevelFilter, LevelFilterFromStrError,
13    LoggerConfig, LoggerInitError, LoggerUpdateError,
14};
15pub use metrics::{
16    IncMetric, LatencyAggregateMetrics, METRICS, MetricsError, ProcessTimeReporter,
17    SharedIncMetric, SharedStoreMetric, StoreMetric,
18};
19use utils::time::{ClockType, get_time_us};
20
21/// Alias for `std::io::LineWriter<std::fs::File>`.
22pub type FcLineWriter = std::io::LineWriter<std::fs::File>;
23
24/// Prefix to be used in log lines for functions/modules in Firecracker
25/// that are not generally available.
26const DEV_PREVIEW_LOG_PREFIX: &str = "[DevPreview]";
27
28/// Log a standard warning message indicating a given feature name
29/// is in development preview.
30pub fn log_dev_preview_warning(feature_name: &str, msg_opt: Option<String>) {
31    match msg_opt {
32        None => warn!("{DEV_PREVIEW_LOG_PREFIX} {feature_name} is in development preview."),
33        Some(msg) => {
34            warn!("{DEV_PREVIEW_LOG_PREFIX} {feature_name} is in development preview - {msg}")
35        }
36    }
37}
38
39/// Helper function for updating the value of a store metric with elapsed time since some time in a
40/// past.
41pub fn update_metric_with_elapsed_time(metric: &SharedStoreMetric, start_time_us: u64) -> u64 {
42    let delta_us = get_time_us(ClockType::Monotonic) - start_time_us;
43    metric.store(delta_us);
44    delta_us
45}