Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- // STL
- #include <iostream>
- #include <sstream>
- #include <fstream>
- #include <functional>
- #include <utility>
- #include <vector>
- #include <memory>
- #include <chrono>
- // C-STL
- #include <ctime>
- // Boost
- #include <boost/format.hpp>
- namespace Robotics {
- namespace utility {
- /**
- * @brief Levels of message
- *
- * From the most commmon to the most important are:
- * trace -- The TRACE Level designates finer-grained informational events than the DEBUG
- * debug -- The DEBUG Level designates fine-grained informational events that are most useful to debug an application
- * info -- The INFO level designates informational messages that highlight the progress of the application at coarse-grained level
- * warning -- The WARNING level designates potentially harmful situations
- * error -- The ERROR level designates error events that might still allow the application to continue running
- * fatal -- The FATAL level designates very severe error events that will presumably lead the application to abort
- */
- enum class level : int { trace, debug, info, warning, error, fatal };
- }//namespace utility
- }//namespace Robotics
- namespace {
- /**
- * @brief RLogMaster class used for Logging.
- */
- class RLogMaster {
- public: // functions
- RLogMaster() : console_lv_([](const typename ::Robotics::utility::level lv) { return true; }) {}
- RLogMaster(const RLogMaster & rhs) = delete;
- RLogMaster(RLogMaster && rhs) = delete;
- RLogMaster & operator=(const RLogMaster & rhs) = delete;
- RLogMaster & operator=(RLogMaster && rhs) = delete;
- /**
- * @brief destructor, close all files opened
- */
- ~RLogMaster() {
- for(auto i : this->files_) {
- i.first->close();
- }//for
- }//destructor
- /**
- * @brief output messages into files
- */
- template <typename ... Args>
- inline void
- print(const typename ::Robotics::utility::level lv, Args ... args) const {
- print(lv,
- std::ostringstream(create_time_message(lv), std::ios_base::ate),
- std::forward<Args>(args)...);
- }//print(level, ...)
- template <typename ... Args>
- inline void
- format(const typename ::Robotics::utility::level lv,
- const std::string & fmt, Args ... args) const
- {
- std::string time_msg(create_time_message(lv));
- std::cout << time_msg;
- for(auto i : this->files_) {
- if(i.second(lv))
- *(i.first) << time_msg;
- }//for
- boost::format fmter(fmt);
- format(lv, fmter, std::forward<Args>(args)...);
- }//format(level, string, ...)
- inline void
- add_file(const std::string & file_name,
- const std::function<bool(const typename ::Robotics::utility::level lv)> & func)
- {
- this->files_.push_back(
- std::make_pair(
- std::make_shared<std::ofstream>(file_name),
- func));
- }//add_file(file_name, func)
- inline void
- set_console_level(const std::function<bool(const typename ::Robotics::utility::level lv)> & func)
- { this->console_lv_ = func; }
- private: // functions
- inline std::string create_time_message(const typename ::Robotics::utility::level lv) const {
- // Get the current time by format year-month-day.hour-minute-second.microseconds_from_epoch
- std::time_t now_time = std::time(nullptr);
- auto tp = std::chrono::high_resolution_clock::now();
- const std::size_t buf_len = 30;
- static char buf[buf_len];
- std::strftime(buf, buf_len, "[%Y-%m-%d.%H:%M:%S.", std::localtime(&now_time));
- std::string time_msg(buf);
- time_msg += std::to_string(
- std::chrono::duration_cast<std::chrono::microseconds>(tp.time_since_epoch()).count());
- time_msg += "] ";
- time_msg += this->msg_lv_[static_cast<int>(lv)];
- return time_msg;
- }//create_time_message(level)
- std::string
- get_color(const typename ::Robotics::utility::level lv) const {
- switch(lv) {
- case ::Robotics::utility::level::trace:
- return "\033[37;40;1;2m"; // white on black, bold, darkened
- case ::Robotics::utility::level::debug:
- return "\033[34;40;1;4m"; // blue on black, bold, underlined
- case ::Robotics::utility::level::info:
- return "\033[33;40;1;4m"; // yellow on black, bold, underline
- case ::Robotics::utility::level::warning:
- return "\033[35;40;1m"; // magenta on black, bold
- case ::Robotics::utility::level::error:
- return "\033[31;40;1m"; // red on black, bold
- case ::Robotics::utility::level::fatal:
- return "\033[37;41;1m"; // white on read, bold
- };//switch-case
- return "\033[0m"; //normal text
- }//get_color(level)
- template <typename T, typename ... Args>
- inline void
- format(const typename ::Robotics::utility::level lv,
- boost::format & fmt, const T & value, Args ... args) const
- {
- fmt % value;
- format(lv, fmt, std::forward<Args>(args)...);
- }//format(level, format, value, ...)
- void
- format(const typename ::Robotics::utility::level lv, boost::format & fmt) const {
- if(this->console_lv_(lv))
- std::cout << this->get_color(lv) << fmt.str() << "\033[0m" << std::endl;
- for(auto i : this->files_) {
- if(i.second(lv))
- *(i.first) << fmt.str() << std::endl;
- }//for
- }//format(level, format)
- template <typename T, typename ... Args>
- inline void
- print(const typename ::Robotics::utility::level lv,
- std::ostringstream && oss,
- T && message,
- Args ... args) const
- {
- oss << std::forward<T>(message);
- print(lv, std::move(oss), std::forward<Args>(args)...);
- }//print(level, ostringstream, message, ...)
- void
- print(const typename ::Robotics::utility::level lv,
- std::ostringstream && oss) const
- {
- if(this->console_lv_(lv))
- std::cout << this->get_color(lv) << oss.str() << "\033[0m" << std::endl;
- for(auto i : this->files_) {
- if(i.second(lv))
- *(i.first) << oss.str() << std::endl;
- }//for
- }//print(level, ostringstream)
- private: // members
- std::function<bool(const typename ::Robotics::utility::level lv)> console_lv_;
- std::vector<
- std::pair<
- std::shared_ptr<std::ofstream>,
- std::function<bool(const typename ::Robotics::utility::level lv)>
- >
- > files_;
- const char * msg_lv_[6] = {
- "[trace] ", "[debug] ", "[info ] ", "[warn ] ", "[error] ", "[fatal] "};
- };//class RLogMaster
- }//empty namespace
- namespace Robotics {
- namespace utility {
- RLogMaster rLog;
- }//namespace utility
- }//namespace Robotics
Add Comment
Please, Sign In to add comment