mirror of
https://github.com/robbert-vdh/yabridge.git
synced 2026-05-16 05:33:07 +02:00
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:
+16
-10
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user