What I am trying to achieve?
How can I find if a stream chain is ended? Look at the function below (all these functions are inside a LogRouter class in this question):
template<typename First, typename... Rest>
void log(const LogLevel &level_, First first_, Rest... rest_) {
sstream << first_ << " ";
log(level_, rest_...);
}
void log(const LogLevel &level_) {
for(auto &route : routes)
route->stream() << sstream.str() << std::endl;
sstream.clear();
sstream.str("");
}
I want to achieve the exact same functionality in the above but using streams. So, when I reach an end of a stream it needs to send the final data to the routes and instead of using
router.log(LogLevel::Alert, "test stream", 15);
I want to be able to use
router.log(LogLevel::Alert) << "test stream " << 15;
What I have tried:
std::ostream
operator overloading does not accept packed variables.going through every single passed value one by one. Like below:
struct LogEnd {}; static LogEnd end() { return LogEnd; } template<typename T> LogRouter &operator<<(const T &value) { sstream << value; return *this; } LogRouter &log(const LogLevel &level_) { currentLogLevel = level_; //had to add another variable return *this; } void operator<<(const LogEnd &end) { for(auto &route : routes) route.stream() << sstream.str() << std::endl; currentLogLevel = LogLevel::None; }
This gives me what I want syntax wise but I need to call the additional
LogRouter::end()
at the end of every:router.log(LogLevel::Alert) << "test stream " << 15 << LogRouter::end();
I have a syntax for
std::endl
also but it would be best if I can call it without anything in the end.
Question
Is there a way to know an end of a stream chain. Something similar to what you can do when using recursive variadic template function.
See Question&Answers more detail:os