rust: log: implement io::Write
PATCH 1/1 rust: log: implement io::Write
This makes it possible to lock the log file; it also makes log_mask_ln! not allocate memory when logging a constant string.
--- include/qemu/log.h | 2 + util/log.c | 12 ++++++ rust/qemu-api/src/log.rs | 87 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 93 insertions(+), 8 deletions(-)
include/qemu/log.h +2 -0
@@ -84,6 +84,8 @@ typedef struct QEMULogItem {extern const QEMULogItem qemu_log_items[]; +ssize_t rust_fwrite (const void *ptr,size_t size,size_t nmemb,FILE *stream); +bool qemu_set_log (int log_flags,Error **errp);bool qemu_set_log_filename (const char *filename,Error **errp);bool qemu_set_log_filename_flags (const char *name,int flags,Error **errp);
util/log.c +12 -0
@@ -558,3 +558,15 @@ void qemu_print_log_usage(FILE *f)fprintf (f,"\nUse \"-d trace:help\" to get a list of trace events.\n\n" );#endif } + +#ifdef CONFIG_HAVE_RUST +ssize_t rust_fwrite (const void *ptr,size_t size,size_t nmemb,FILE *stream) +{ +/* +* Same as fwrite, but return -errno because Rust libc does not provide +* portable access to errno. :( +*/ +int ret =fwrite (ptr, size, nmemb, stream); +return ret <0 ? -errno :0 ; +} +#endif
rust/qemu-api/src/log.rs +79 -8
114 lines not on this page · raw message
Signature · 1 line
2.49.0
rust/qemu-api/src/log.rs line 33
Quoted 9 lines
rust/qemu-api/src/log.rs +9
-0+/// A RAII guard for QEMU's logging infrastructure. Creating the guard +/// locks the log file, and dropping it (letting it go out of scope) unlocks +/// the file. +/// +/// As long as the guard lives, it can be written to using [`std::io::Write`]. +/// +/// The locking is recursive, therefore owning a guard does not prevent +/// using [`log_mask_ln!()`](crate::log_mask_ln). +pub struct LogGuard (NonNull <bindings::FILE >);
Nice! I happen to be implementing a similar RAII for FlatView:
pub struct FlatViewRefGuard(NonNull<FlatView>);
Quoted 1 line
+impl LogGuard {
...
rust/qemu-api/src/log.rs line 64
Quoted 7 lines
rust/qemu-api/src/log.rs +7
-0+/// Writes a formatted string into the log, returning any error encountered. +/// +/// This method is primarily used by the [`log_mask_ln!()`](crate::log_mask_ln) +/// macro, and it is rare for it to be called explicitly. +pub fn log_fmt (args: std::fmt::Arguments ) -> io::Result <()> { +if let Some (mut log) =Self ::new () { +use io::Write ;
I think it's better to place this "use" at the beginning of the file.
rust/qemu-api/src/log.rs line 69
Quoted 5 lines
rust/qemu-api/src/log.rs +5
-0+ log.write_fmt (args)?; + } +Ok (()) + } +}
Overall, LGTM,
Quoted 11 lines
/// A macro to log messages conditionally based on a provided mask.
rust/qemu-api/src/log.rs +2
-0@@ -24,6 +96,8 @@ pub enum Log {/// log level and, if so, formats and logs the message. It is the Rust /// counterpart of the `qemu_log_mask()` macro in the C implementation. /// +/// Errors from writing to the log are ignored. +/// /// # Parameters /// /// - `$mask`: A log level mask. This should be a variant of the `Log` enum.
rust/qemu-api/src/log.rs macro_rules! log_mask_ln { · line 139
Quoted 10 lines
rust/qemu-api/src/log.rs +1
-6@@ -62,12 +136,9 @@ macro_rules! log_mask_ln {if unsafe { (::qemu_api::bindings::qemu_loglevel & ($maskas std::os::raw::c_int )) !=0 } { -let formatted_string =format! ("{}\n" ,format_args! ($fmt $($args)*)); -let c_string = std::ffi::CString ::new (formatted_string).unwrap (); - -unsafe { - ::qemu_api::bindings::qemu_log (c_string.as_ptr ()); - } +#[allow(unused_must_use)]
I found this doesn't work :-( :
error: unused Result that must be used
--> ../rust/hw/char/pl011/src/device.rs:281:21
|
281 | log_mask_ln!(Log::Unimp, "pl011: DMA not implemented");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| = note: this Result may be an Err variant, which should be handled
= note: -D unused-must-use implied by -D warnings
= help: to override -D warnings add #[allow(unused_must_use)]
= note: this error originates in the macro log_mask_ln (in Nightly builds, run with -Z macro-backtrace for more info)
I understand meson sets -D warings so that allow can't work...
What about just ignoring the return value? Afterall pl011 doesn't care the returned value.
@@ -136,8 +137,7 @@ macro_rules! log_mask_ln {
if unsafe {
(::qemu_api::bindings::qemu_loglevel & ($mask as std::os::raw::c_int)) != 0
} {
- #[allow(unused_must_use)]
- ::qemu_api::log::LogGuard::log_fmt(
+ let _ = ::qemu_api::log::LogGuard::log_fmt(
format_args!("{}\n", format_args!($fmt $($args)*)));
}
}};Thanks,
Zhao
Quoted 9 lines
rust/qemu-api/src/log.rs +2
-0+ ::qemu_api::log::LogGuard ::log_fmt ( +format_args! ("{}\n" ,format_args! ($fmt $($args)*))); } }}; }Signature · 1 line
2.49.0
Manos Pitsidianakis
Reviewed-byQuoted 42 lines · Zhao Liu
Quoted 12 lines
/// A macro to log messages conditionally based on a provided mask.
rust/qemu-api/src/log.rs +2
-0@@ -24,6 +96,8 @@ pub enum Log {/// log level and, if so, formats and logs the message. It is the Rust /// counterpart of the `qemu_log_mask()` macro in the C implementation. /// +/// Errors from writing to the log are ignored. +/// /// # Parameters /// /// - `$mask`: A log level mask. This should be a variant of the `Log` enum. rust/qemu-api/src/log.rs macro_rules! log_mask_ln { · line 139
Quoted 10 lines
rust/qemu-api/src/log.rs +1
-6@@ -62,12 +136,9 @@ macro_rules! log_mask_ln {if unsafe { (::qemu_api::bindings::qemu_loglevel & ($maskas std::os::raw::c_int )) !=0 } { -let formatted_string =format! ("{}\n" ,format_args! ($fmt $($args)*)); -let c_string = std::ffi::CString ::new (formatted_string).unwrap (); - -unsafe { - ::qemu_api::bindings::qemu_log (c_string.as_ptr ()); - } +#[allow(unused_must_use)] I found this doesn't work :-( :
error: unused
Resultthat must be used
-->../rust/hw/char/pl011/src/device.rs:281:21| 281 | log_mask_ln!(Log::Unimp, "pl011: DMA not implemented"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |= note: this
Resultmay be anErrvariant, which should be handled
= note:-D unused-must-useimplied by-D warnings
= help: to override-D warningsadd#[allow(unused_must_use)]
= note: this error originates in the macrolog_mask_ln(in Nightly builds, run with -Z macro-backtrace for more info)I understand meson sets
-D waringsso thatallowcan't work...What about just ignoring the return value? Afterall pl011 doesn't care the returned value.
rust/qemu-api/src/log.rs macro_rules! log_mask_ln { · line 143
Quoted 9 lines
rust/qemu-api/src/log.rs +1
-2@@ -136,8 +137,7 @@ macro_rules! log_mask_ln {if unsafe { (::qemu_api::bindings::qemu_loglevel & ($maskas std::os::raw::c_int )) !=0 } { -#[allow(unused_must_use)] - ::qemu_api::log::LogGuard ::log_fmt ( +let _ = ::qemu_api::log::LogGuard ::log_fmt (format_args! ("{}\n" ,format_args! ($fmt $($args)*))); } }};
Just _ = ::qemu_api::log::LogGuard::log_fmt(...); is sufficient, no let needed.
Paolo: I haven't tested it to see the warning Zhao did (busy lately...), but LGTM, as well. With that fixed,
Quoted 6 lines
Quoted 6 lines
rust/qemu-api/src/log.rs +0
-2@@ -136,8 +137,7 @@ macro_rules! log_mask_ln {if unsafe { (::qemu_api::bindings::qemu_loglevel & ($maskas std::os::raw::c_int )) !=0 } { -#[allow(unused_must_use)] - ::qemu_api::log::LogGuard::log_fmt(
Quoted 8 lines
Review macro_rules! log_mask_ln { · line 143
Quoted 4 lines
(quoted diff) +1
-0+ let _ = ::qemu_api::log::LogGuard::log_fmt( format_args!("{}\n", format_args!($fmt $($args)*))); } }};Just
_ = ::qemu_api::log::LogGuard::log_fmt(...);is sufficient, noletneeded.
Good catch! Thank you Manos.
Regards,
Zhao