Prevent allocations caused by Logger::log_trace

C++ would always construct an `std::string` from the string constant
every iteration. Since this also happened when `YABRIDGE_DEBUG_LEVEL` is
not set to 2, this ended up causing unnecessary allocations.
This commit is contained in:
Robbert van der Helm
2021-05-23 00:21:21 +02:00
parent cf2fbf602c
commit df93944f3b
6 changed files with 40 additions and 36 deletions
+16 -10
View File
@@ -28,6 +28,8 @@
#include <boost/asio/streambuf.hpp>
#include <boost/process/async_pipe.hpp>
#include "../utils.h"
/**
* Boost 1.72 was released with a known breaking bug caused by a missing
* typedef: https://github.com/boostorg/process/issues/116.
@@ -132,16 +134,6 @@ class Logger {
*/
void log(const std::string& message);
/**
* Log a message that should only be printed when the `verbosity` is set to
* `all_events`. This should only be used for simple primitive messages
* without any formatting since the actual check happens within this
* function.
*
* @param message The message to write.
*/
void log_trace(const std::string& message);
/**
* Write output from an async pipe to the log on a line by line basis.
* Useful for logging the Wine process's STDOUT and STDERR streams.
@@ -172,6 +164,20 @@ class Logger {
});
}
/**
* Log a message that should only be printed when the `verbosity` is set to
* `all_events`. This uses a lambda since producing a string always
* allocates.
*
* @param message A lambda producing a string that should be written.
*/
template <invocable_returning<std::string> F>
void log_trace(F&& fn) {
if (verbosity >= Verbosity::all_events) {
log(fn());
}
}
/**
* The verbosity level of this logger instance. Based on this certain
* messages may or may not be shown.