Advertisement
Guest User

Untitled

a guest
Jul 15th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.50 KB | None | 0 0
  1. #pragma once
  2. #define BOOST_LOG_DYN_LINK 1
  3. #pragma GCC system_header
  4.  
  5. #include <boost/log/trivial.hpp>
  6. #include <boost/log/sources/severity_logger.hpp>
  7. #include <boost/log/utility/setup/console.hpp>
  8. #include <boost/log/expressions.hpp>
  9. #include <boost/log/utility/setup/common_attributes.hpp>
  10. #include <boost/date_time/posix_time/posix_time_types.hpp>
  11. #include <boost/log/support/date_time.hpp>
  12. #include <boost/log/attributes/mutable_constant.hpp>
  13. #include <boost/log/utility/manipulators/add_value.hpp>
  14. #include <boost/filesystem.hpp>
  15. #include <sstream>
  16.  
  17. using severity = ::boost::log::trivial::severity_level;
  18.  
  19. #define CONCAT( A, B ) A ## B
  20. #define SELECT( NAME, NUM ) CONCAT( NAME ## _, NUM )
  21.  
  22. #define GET_COUNT( _1, _2, _3 /* ad nauseam */, COUNT, ... ) COUNT
  23. #define VA_SIZE( ... ) GET_COUNT( __VA_ARGS__, 3, 2, 1 )
  24.  
  25. #define VA_SELECT( NAME, ... ) SELECT( NAME, VA_SIZE(__VA_ARGS__) )(__VA_ARGS__)
  26.  
  27. #define LOG( ... ) VA_SELECT( LOG, __VA_ARGS__ )
  28.  
  29. #define LOG_2(sev, scope) \
  30.     BOOST_LOG_SEV( logger::Logger::getInstance().getSeverityLogger(), sev) \
  31.     << boost::log::add_value("Line", __LINE__)      \
  32.     << boost::log::add_value("File", __FILE__)       \
  33.     << boost::log::add_value("Scope", std::string(scope))
  34.  
  35. #define LOG_1(sev) LOG_2(sev, "none")
  36.  
  37. #define SAFELOG(...) std::cout << logger::getCurrentTime() << ": " << '[' \
  38.     << logger::path_to_filename(__FILE__) << ':' << __LINE__ << "] " << logger::format(__VA_ARGS__)
  39.  
  40. namespace logger {
  41.  
  42. namespace logging  = boost::log;
  43. namespace attrs    = boost::log::attributes;
  44. namespace expr     = boost::log::expressions;
  45. namespace src      = boost::log::sources;
  46. namespace keywords = boost::log::keywords;
  47. namespace sinks    = boost::log::sinks;
  48.  
  49. inline std::string getCurrentTime() {
  50.     std::stringstream ss;
  51.     ss << boost::posix_time::microsec_clock::local_time();
  52.     return ss.str().substr(ss.str().find_last_of(" ")+1);;
  53. }
  54.  
  55. // Convert file path to only the filename
  56. inline std::string path_to_filename(std::string path) {
  57.     return path.substr(path.find_last_of("/\\")+1);
  58. }
  59.  
  60. inline std::string format(const char *fmt, ...)
  61. {
  62.     va_list args;
  63.     va_start(args, fmt);
  64.     std::vector<char> v(1024);
  65.     while (true)
  66.     {
  67.         va_list args2;
  68.         va_copy(args2, args);
  69.         int res = vsnprintf(v.data(), v.size(), fmt, args2);
  70.         if ((res >= 0) && (res < static_cast<int>(v.size())))
  71.         {
  72.             va_end(args);
  73.             va_end(args2);
  74.             return std::string(v.data());
  75.         }
  76.         size_t size;
  77.         if (res < 0)
  78.             size = v.size() * 2;
  79.         else
  80.             size = static_cast<size_t>(res) + 1;
  81.         v.clear();
  82.         v.resize(size);
  83.         va_end(args2);
  84.     }
  85. }
  86.  
  87. inline void custom_formatter(logging::record_view const& rec, logging::formatting_ostream& strm)
  88. {
  89.     std::stringstream ss;
  90.     ss << boost::posix_time::microsec_clock::local_time();
  91.     strm << ss.str().substr(ss.str().find_last_of(" ") + 1);
  92.  
  93.     strm << expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%H:%M:%S.%f");
  94.     strm << " <" << rec[logging::trivial::severity] << "> ";
  95.     strm << "(" << logging::extract< std::string >("Scope", rec) << ")[";
  96.     logging::value_ref< std::string > fullpath = logging::extract< std::string >("File", rec);
  97.     strm << boost::filesystem::path(fullpath.get()).filename().string() << ":";
  98.     strm << logging::extract< int >("Line", rec) << "] ";
  99.     strm << rec[expr::smessage];
  100. }
  101.  
  102. class Logger
  103. {
  104. private:
  105.     Logger() {
  106.         this->init();
  107.     }
  108.     Logger(Logger const&) = delete;
  109.     Logger& operator= (Logger const&) = delete;
  110.    
  111.     void init() {
  112.         typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
  113.         boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
  114.         sink->locked_backend()->add_stream(
  115.             boost::shared_ptr<std::ostream>(&std::cout, boost::null_deleter()));
  116.  
  117.         sink->set_formatter(&custom_formatter);
  118.         logging::core::get()->add_sink(sink);
  119.         logging::core::get()->set_filter
  120.         (
  121.             logging::trivial::severity >= logging::trivial::trace //trace,debug,info,warning,error,fatal
  122.         );
  123.     }
  124.  
  125. public:
  126.     static Logger& getInstance() {
  127.         static Logger inst;
  128.         return inst;
  129.     }
  130.  
  131.     static src::severity_logger<logging::trivial::severity_level>&  getSeverityLogger() {
  132.         static src::severity_logger<logging::trivial::severity_level> lg;
  133.         return lg;
  134.     }
  135. };
  136. } // namespace logger
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement