lg2: minor size optimization in do_log
Push the calculation of the number of logging arguments into the
variadic 'do_log' function rather than calculate it at the lg2
call site. This reduces each call site by a few instructions.
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Ic8fcbdf65fb34d251d8110c5bbd05c2e20c66c18
diff --git a/lib/include/phosphor-logging/lg2/conversion.hpp b/lib/include/phosphor-logging/lg2/conversion.hpp
index fe0d7a1..5f6c298 100644
--- a/lib/include/phosphor-logging/lg2/conversion.hpp
+++ b/lib/include/phosphor-logging/lg2/conversion.hpp
@@ -254,7 +254,7 @@
static void done(level l, const std::source_location& s, const char* m,
Ts&&... ts)
{
- do_log(l, s, m, sizeof...(Ts) / 3, std::forward<Ts>(ts)...);
+ do_log(l, s, m, std::forward<Ts>(ts)..., nullptr);
}
/** Apply the tuple from the end of 'step' into done.
diff --git a/lib/include/phosphor-logging/lg2/logger.hpp b/lib/include/phosphor-logging/lg2/logger.hpp
index 6dca12c..7459589 100644
--- a/lib/include/phosphor-logging/lg2/logger.hpp
+++ b/lib/include/phosphor-logging/lg2/logger.hpp
@@ -12,12 +12,13 @@
* This is a variadic argument function (std::va_list) where the variadic
* arguments are a set of 3: { header, flags, value }.
*
+ * The argument set is finalized with a 'nullptr' valued header.
+ *
* @param[in] level - The logging level to use.
* @param[in] source_location - The original source location of the upper-level
* log call.
* @param[in] char* - The primary message to log.
- * @param[in] size_t - The number of sets in the variadic arguments.
*/
-void do_log(level, const std::source_location&, const char*, size_t, ...);
+void do_log(level, const std::source_location&, const char*, ...);
} // namespace lg2::details
diff --git a/lib/lg2_logger.cpp b/lib/lg2_logger.cpp
index ac7a75c..fd3921c 100644
--- a/lib/lg2_logger.cpp
+++ b/lib/lg2_logger.cpp
@@ -143,13 +143,11 @@
isatty(fileno(stderr)) ? cerr_extra_output : noop_extra_output;
// Do_log implementation.
-void do_log(level l, const std::source_location& s, const char* m, size_t count,
- ...)
+void do_log(level l, const std::source_location& s, const char* m, ...)
{
using namespace std::string_literals;
- const size_t entries = count + static_locs;
- std::vector<std::string> strings{entries};
+ std::vector<std::string> strings{static_locs};
std::string message{m};
@@ -162,11 +160,16 @@
// Handle all the va_list args.
std::va_list args;
- va_start(args, count);
- for (size_t i = static_locs; i < entries; ++i)
+ va_start(args, m);
+ while (true)
{
// Get the header out.
- std::string h = va_arg(args, const char*);
+ auto h_ptr = va_arg(args, const char*);
+ if (h_ptr == nullptr)
+ {
+ break;
+ }
+ std::string h{h_ptr};
// Get the format flag.
auto f = va_arg(args, uint64_t);
@@ -204,7 +207,7 @@
}
// Create the field for this value.
- strings[i] = h + '=' + value;
+ strings.emplace_back(h + '=' + value);
// Check for {HEADER} in the message and replace with value.
auto h_brace = '{' + h + '}';
@@ -225,7 +228,7 @@
});
// Output the iovec.
- sd_journal_sendv(iov.data(), entries);
+ sd_journal_sendv(iov.data(), strings.size());
extra_output_method(l, s, message);
}