shit_adam

utility.hpp

Aug 3rd, 2013
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.15 KB | None | 0 0
  1. #pragma once
  2.  
  3. // STL
  4. #include <iostream>
  5. #include <sstream>
  6. #include <fstream>
  7. #include <functional>
  8. #include <utility>
  9. #include <vector>
  10. #include <memory>
  11. #include <chrono>
  12. // C-STL
  13. #include <ctime>
  14. // Boost
  15. #include <boost/format.hpp>
  16.  
  17. namespace Robotics {
  18. namespace utility {
  19.  
  20.     /**
  21.      * @brief Levels of message
  22.      *
  23.      * From the most commmon to the most important are:
  24.      * trace -- The TRACE Level designates finer-grained informational events than the DEBUG
  25.      * debug -- The DEBUG Level designates fine-grained informational events that are most useful to debug an application
  26.      * info -- The INFO level designates informational messages that highlight the progress of the application at coarse-grained level
  27.      * warning -- The WARNING level designates potentially harmful situations
  28.      * error -- The ERROR level designates error events that might still allow the application to continue running
  29.      * fatal -- The FATAL level designates very severe error events that will presumably lead the application to abort
  30.      */
  31.     enum class level : int { trace, debug, info, warning, error, fatal };
  32.  
  33. }//namespace utility
  34. }//namespace Robotics
  35.  
  36. namespace {
  37.  
  38. /**
  39.  * @brief RLogMaster class used for Logging.
  40.  */
  41. class RLogMaster {
  42. public: // functions
  43.     RLogMaster() : console_lv_([](const typename ::Robotics::utility::level lv) { return true; }) {}
  44.  
  45.     RLogMaster(const RLogMaster & rhs) = delete;
  46.     RLogMaster(RLogMaster && rhs) = delete;
  47.  
  48.     RLogMaster & operator=(const RLogMaster & rhs) = delete;
  49.     RLogMaster & operator=(RLogMaster && rhs) = delete;
  50.  
  51.     /**
  52.      * @brief destructor, close all files opened
  53.      */
  54.     ~RLogMaster() {
  55.         for(auto i : this->files_) {
  56.             i.first->close();
  57.         }//for
  58.     }//destructor
  59.  
  60.     /**
  61.      * @brief output messages into files
  62.      */
  63.     template <typename ... Args>
  64.     inline void
  65.     print(const typename ::Robotics::utility::level lv, Args ... args) const {
  66.         print(lv,
  67.                 std::ostringstream(create_time_message(lv), std::ios_base::ate),
  68.                 std::forward<Args>(args)...);
  69.     }//print(level, ...)
  70.  
  71.     template <typename ... Args>
  72.     inline void
  73.     format(const typename ::Robotics::utility::level lv,
  74.             const std::string & fmt, Args ... args) const
  75.     {
  76.         std::string time_msg(create_time_message(lv));
  77.  
  78.         std::cout << time_msg;
  79.         for(auto i : this->files_) {
  80.             if(i.second(lv))
  81.                 *(i.first) << time_msg;
  82.         }//for
  83.  
  84.         boost::format fmter(fmt);
  85.         format(lv, fmter, std::forward<Args>(args)...);
  86.     }//format(level, string, ...)
  87.  
  88.     inline void
  89.     add_file(const std::string & file_name,
  90.             const std::function<bool(const typename ::Robotics::utility::level lv)> & func)
  91.     {
  92.         this->files_.push_back(
  93.                 std::make_pair(
  94.                     std::make_shared<std::ofstream>(file_name),
  95.                     func));
  96.     }//add_file(file_name, func)
  97.  
  98.     inline void
  99.     set_console_level(const std::function<bool(const typename ::Robotics::utility::level lv)> & func)
  100.     { this->console_lv_ = func; }
  101.  
  102. private: // functions
  103.     inline std::string create_time_message(const typename ::Robotics::utility::level lv) const {
  104.         // Get the current time by format year-month-day.hour-minute-second.microseconds_from_epoch
  105.         std::time_t now_time = std::time(nullptr);
  106.         auto tp = std::chrono::high_resolution_clock::now();
  107.  
  108.         const std::size_t buf_len = 30;
  109.         static char buf[buf_len];
  110.  
  111.         std::strftime(buf, buf_len, "[%Y-%m-%d.%H:%M:%S.", std::localtime(&now_time));
  112.  
  113.         std::string time_msg(buf);
  114.         time_msg += std::to_string(
  115.                 std::chrono::duration_cast<std::chrono::microseconds>(tp.time_since_epoch()).count());
  116.         time_msg += "] ";
  117.         time_msg += this->msg_lv_[static_cast<int>(lv)];
  118.  
  119.         return time_msg;
  120.     }//create_time_message(level)
  121.  
  122.     std::string
  123.     get_color(const typename ::Robotics::utility::level lv) const {
  124.         switch(lv) {
  125.         case ::Robotics::utility::level::trace:
  126.             return "\033[37;40;1;2m"; // white on black, bold, darkened
  127.         case ::Robotics::utility::level::debug:
  128.             return "\033[34;40;1;4m"; // blue on black, bold, underlined
  129.         case ::Robotics::utility::level::info:
  130.             return "\033[33;40;1;4m"; // yellow on black, bold, underline
  131.         case ::Robotics::utility::level::warning:
  132.             return "\033[35;40;1m"; // magenta on black, bold
  133.         case ::Robotics::utility::level::error:
  134.             return "\033[31;40;1m"; // red on black, bold
  135.         case ::Robotics::utility::level::fatal:
  136.             return "\033[37;41;1m"; // white on read, bold
  137.         };//switch-case
  138.  
  139.         return "\033[0m"; //normal text
  140.     }//get_color(level)
  141.  
  142.     template <typename T, typename ... Args>
  143.     inline void
  144.     format(const typename ::Robotics::utility::level lv,
  145.             boost::format & fmt, const T & value, Args ... args) const
  146.     {
  147.         fmt % value;
  148.         format(lv, fmt, std::forward<Args>(args)...);
  149.     }//format(level, format, value, ...)
  150.  
  151.     void
  152.     format(const typename ::Robotics::utility::level lv, boost::format & fmt) const {
  153.         if(this->console_lv_(lv))
  154.             std::cout << this->get_color(lv) << fmt.str() << "\033[0m" << std::endl;
  155.         for(auto i : this->files_) {
  156.             if(i.second(lv))
  157.                 *(i.first) << fmt.str() << std::endl;
  158.         }//for
  159.     }//format(level, format)
  160.  
  161.     template <typename T, typename ... Args>
  162.     inline void
  163.     print(const typename ::Robotics::utility::level lv,
  164.             std::ostringstream && oss,
  165.             T && message,
  166.             Args ... args) const
  167.     {
  168.         oss << std::forward<T>(message);
  169.         print(lv, std::move(oss), std::forward<Args>(args)...);
  170.     }//print(level, ostringstream, message, ...)
  171.  
  172.     void
  173.     print(const typename ::Robotics::utility::level lv,
  174.             std::ostringstream && oss) const
  175.     {
  176.         if(this->console_lv_(lv))
  177.             std::cout << this->get_color(lv) << oss.str() << "\033[0m" << std::endl;
  178.         for(auto i : this->files_) {
  179.             if(i.second(lv))
  180.                 *(i.first) << oss.str() << std::endl;
  181.         }//for
  182.     }//print(level, ostringstream)
  183.  
  184. private: // members
  185.     std::function<bool(const typename ::Robotics::utility::level lv)> console_lv_;
  186.  
  187.     std::vector<
  188.         std::pair<
  189.             std::shared_ptr<std::ofstream>,
  190.             std::function<bool(const typename ::Robotics::utility::level lv)>
  191.         >
  192.     > files_;
  193.  
  194.     const char * msg_lv_[6] = {
  195.         "[trace] ", "[debug] ", "[info ] ", "[warn ] ", "[error] ", "[fatal] "};
  196. };//class RLogMaster
  197.  
  198. }//empty namespace
  199.  
  200. namespace Robotics {
  201. namespace utility {
  202.  
  203.     RLogMaster rLog;
  204.  
  205. }//namespace utility
  206. }//namespace Robotics
Add Comment
Please, Sign In to add comment