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

[qemu] Make logging code terser #3653

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions src/platform/backends/qemu/qemu_virtual_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,25 +380,17 @@ void mp::QemuVirtualMachine::shutdown(bool force)
mpl::log(mpl::Level::debug, vm_name, "No process to kill");
}

if (mp::backend::instance_image_has_snapshot(desc.image.image_path, suspend_tag))
{
if (state != State::suspended)
{
mpl::log(
mpl::Level::warning,
vm_name,
fmt::format("{} has the image suspension snapshot, but it is not in the suspended state", vm_name));
}
const auto has_suspend_snapshot = mp::backend::instance_image_has_snapshot(desc.image.image_path, suspend_tag);
if (has_suspend_snapshot != (state == State::suspended)) // clang-format off
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice trick to utilize the symmetry.

mpl::log(mpl::Level::warning, vm_name, fmt::format("Image has {} suspension snapshot, but the state is {}",
has_suspend_snapshot ? "a" : "no",
static_cast<short>(state))); // clang-format on

Copy link
Contributor

Choose a reason for hiding this comment

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

The only problem of this is the integer value returns to us via static_cast<short>(state), however, enum -> string map or a switch statement may not worth it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Right, this is what my comment above about formatters is about. Ideally, we'd write a Formatter for VM::State... see above.

if (has_suspend_snapshot)
{
mpl::log(mpl::Level::info, vm_name, "Deleting suspend image");
mp::backend::delete_instance_suspend_image(desc.image.image_path, suspend_tag);
}
else if (state == State::suspended)
{
mpl::log(mpl::Level::warning,
vm_name,
fmt::format("{} is suspended, but the image does not have the suspension snapshot", vm_name));
}

state = State::off;
}
Expand Down
6 changes: 2 additions & 4 deletions tests/qemu/test_qemu_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,7 @@ TEST_F(QemuBackend, forceShutdownSuspendedStateButNoSuspensionSnapshotInImage)
logger_scope.mock_logger->screen_logs(mpl::Level::debug);
logger_scope.mock_logger->expect_log(mpl::Level::info, "Forcing shutdown");
logger_scope.mock_logger->expect_log(mpl::Level::debug, "No process to kill");
logger_scope.mock_logger->expect_log(mpl::Level::warning,
"is suspended, but the image does not have the suspension snapshot");
logger_scope.mock_logger->expect_log(mpl::Level::warning, "Image has no suspension snapshot, but the state is 7");

mpt::StubVMStatusMonitor stub_monitor;
mp::QemuVirtualMachineFactory backend{data_dir.path()};
Expand Down Expand Up @@ -577,8 +576,7 @@ TEST_F(QemuBackend, forceShutdownRunningStateButWithSuspensionSnapshotInImage)
logger_scope.mock_logger->expect_log(mpl::Level::info, "Forcing shutdown");
logger_scope.mock_logger->expect_log(mpl::Level::debug, "No process to kill");
logger_scope.mock_logger->expect_log(mpl::Level::info, "Deleting suspend image");
logger_scope.mock_logger->expect_log(mpl::Level::warning,
"has the image suspension snapshot, but it is not in the suspended state");
logger_scope.mock_logger->expect_log(mpl::Level::warning, "Image has a suspension snapshot, but the state is 4");

mpt::StubVMStatusMonitor stub_monitor;
mp::QemuVirtualMachineFactory backend{data_dir.path()};
Expand Down
Loading