Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: add logging functionality #191

Closed
wants to merge 1 commit into from
Closed

Conversation

dodsonmg
Copy link
Contributor

@dodsonmg dodsonmg commented Nov 16, 2021

Checking to see if this is the kind of logging implementation you're looking for to address #79.

It uses the log crate and implements a simple logger that I mostly copied from cloud-hypervisor:
https://github.com/cloud-hypervisor/cloud-hypervisor/blob/f151a8602c1e8b2b17258aef99abd33140622839/src/main.rs#L74-L117

I left most of the println! and eprintln! statements in main.rs and the api crates alone because these provide immediate feedback to the CLI user (e.g., printing a help message) and it didn't seem appropriate to redirect these to stderr with a timestamp.

I replaced remaining statements in all the crates with appropriate info!, warn!, or error! messages. I also replaced usage of the debug! macro from the utils crate with the debug! macro provided by the log crate.

The implementation defaults to the Debug level, meaning only log entries initiated with the trace! macro are filtered out. Alternately, I could implement a command line argument to set the log level, similar to the cloud-hypervisor implementation.

Also, I would appreciate feedback on how I might go about writing unit tests for the logger module. The tests I've found elsewhere (e.g., for firecracker) support a much more complex logger implementation.

Uses the `log` crate and implements a simple logger.

Signed-off-by: Michael Dodson <[email protected]>
@@ -6,7 +6,7 @@ use vm_device::bus::MmioAddress;
use vm_device::MutDeviceMmio;
use vm_superio::{rtc_pl031::NoEvents, Rtc};

use utils::debug;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The debug macro was doing something special, is this one working the same way? Specifically, we don't want to logs to be filled with data coming from the guest when we have release builds because that can be used to generate a DoS when the log file is not properly rate limited.

pub(crate) fn init() {
// Default to Debug.
// TODO: Accept a command line argument to set the log level.
let log_level = LevelFilter::Debug;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can have this as a parameter for the init function, and have a default for it (which can be LevelFilter::Debug). This would make it easier to extend it.

// TODO: Accept a command line argument to set the log level.
let log_level = LevelFilter::Debug;

let log_file: Box<dyn std::io::Write + Send> = Box::new(std::io::stderr());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also be a parameter for which we have a default.

@@ -17,6 +17,8 @@ utils = { path = "../utils" }
vm-vcpu-ref = { path = "../vm-vcpu-ref" }
arch = { path = "../arch" }

log = { version = ">=0.4", features = ["std"] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the std feature required?

@andreeaflorescu
Copy link
Member

Once we add a configuration for the logger we might find a few things worth testing. Until that point I am not sure we can do anything to test the logger that would not be a "coverage test" (i.e. a test that is not checking anything useful, and it's written just so that the coverage does not drop).

writeln!(
*(*(self.output.lock().unwrap())),
"vmm-reference: {:?}: <{}> {}:{}:{} -- {}",
duration,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why duration instead of a wall clock time stamp?

@andreeaflorescu
Copy link
Member

Closing this PR as it is stale. @dodsonmg feel free to re-open if you want to continue working on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants