Automatically expanding the message with runtime data #3329
-
There are already questions on how to add at runtime the value of a variable (in my case an I don't want to enforce the user to pass this value manually. Instead, I was thinking of writing a function that makes this automatically. Example: instead of writing spdlog::debug("{0} blabla {1} blabla", variable1, variable2); the user writes
which in the background turns the logging into
Here is my namespace mylogger {
template <typename... Args>
inline void debug(spdlog::format_string_t<Args...> fmt, Args &&...args) {
auto variable3 = fetch_variable();
spdlog::debug(fmt, std::forward<Args>(args)...); // somehow add variable3 to `fmt` and `args`
};
} Could you help me in expanding the argument list to put |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
Sounds like you want to do the same thing with the following code Compiler Explorer: https://godbolt.org/z/KM6dWEnzx #include <utility>
#include <iostream>
int add(int lhs, int rhs)
{
return lhs + rhs;
}
template <typename... Args>
int add(Args &&...args)
{
int rhs = 100;
return add(std::forward<Args>(args)..., rhs);
}
int main(void) {
std::cout << add(10);
return 0;
} Isn't this enough? namespace mylogger {
template <typename... Args>
inline void debug(spdlog::format_string_t<Args...> fmt, Args &&...args) {
auto variable3 = fetch_variable();
- spdlog::debug(fmt, std::forward<Args>(args)...); // somehow add variable3 to `fmt` and `args`
+ spdlog::debug(fmt, fmt::runtime(std::forward<Args>(args))..., fmt::runtime(std::move(variable3))); // somehow add variable3 to `fmt` and `args`
};
} |
Beta Was this translation helpful? Give feedback.
The macro allows string literals to be concatenated.
However, it is not an easy way to figure out.
https://godbolt.org/z/v7GP4oMMj