Created
          October 10, 2020 11:41 
        
      - 
      
 - 
        
Save GitBubble/5dd0f93c1a25469d8d8442f591054dc8 to your computer and use it in GitHub Desktop.  
    ordered logging utility
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #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