This repository has been archived by the owner on Jan 12, 2025. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Before this commit, the caller of log_impl() (= the macro log!()) already did a memory allocation for constructing a String containing the message to log. This was then borrowed when calling log_impl(). This function then did another memory allocation plus copy to prepend "virtme-ng-init:" to the message. Then, if /dev/kmsg exists, another allocation is done to add a kernel log level tag at the beginning and a newline at the end. This commit starts by changing log!() from format!() to format_args!() which constructs an instance of std::fmt::Arguments. This is just a specification of how to do the formatting, but nothing was actually done yet. This is then used in log_impl() to directly construct a string with "virtme-ng-init:" prepended to the message to log. Since I wanted to avoid memory allocations, this then replaces .trim_end_matches('\n') with a manual loop. That way, we still have a String and do not switch over to a &str for the following code. Additionally, the "is the message to log empty?"-check was reformulated to "was there nothing appended to my prefix?". This makes things slightly more unreadable, but I couldn't find a better way to check whether the Arguments instance is empty. There is a slight change in behaviour here: Previously, log!("\n") would log an empty line since the code first checked if the message is empty and only afterwards trimmed trailing newlines. This is now done the other way around. Finally, when writing to /dev/kmsg, the String that we already have is mutated in places instead of allocating another one and copying all data to it. Why? Just because. I doubt that this will have any measurable performance benefits, but somehow it felt like the right thing to do. Signed-off-by: Uli Schlachter <psychon@znc.in>
- Loading branch information