Skip to content

Instantly share code, notes, and snippets.

@GitBubble
Created October 10, 2020 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GitBubble/5dd0f93c1a25469d8d8442f591054dc8 to your computer and use it in GitHub Desktop.
Save GitBubble/5dd0f93c1a25469d8d8442f591054dc8 to your computer and use it in GitHub Desktop.
ordered logging utility
#include <iostream>
#include <string>
#include <initializer_list>
class Log: public std::ostringstream
{
public:
Log() = default;
~Log()
{
std::lock_guard<std::mutex> guard(_mutexPrint);
std::cout << this->str();
}
private:
static std::mutex _mutexPrint;
};
std::mutex Log::_mutexPrint{};
template <typename T>
void func(T t)
{
Log{} << t ;
}
#ifdef LOGGING
template<typename T, typename... Args>
void func(T t, Args... args) // recursive variadic function
{
Log{} << t ;
func(args...) ;
}
#else
template<typename T, typename... Args>
void func(T t, Args... args) // recursive variadic function
{
return;
}
#endif
#ifdef LOGGING
template <class T>
void func2( std::initializer_list<T> list )
{
for( auto elem : list )
{
Log{} << elem;
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment