Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #define BOOST_LOG_DYN_LINK 1
- #pragma GCC system_header
- #include <boost/log/trivial.hpp>
- #include <boost/log/sources/severity_logger.hpp>
- #include <boost/log/utility/setup/console.hpp>
- #include <boost/log/expressions.hpp>
- #include <boost/log/utility/setup/common_attributes.hpp>
- #include <boost/date_time/posix_time/posix_time_types.hpp>
- #include <boost/log/support/date_time.hpp>
- #include <boost/log/attributes/mutable_constant.hpp>
- #include <boost/log/utility/manipulators/add_value.hpp>
- #include <boost/filesystem.hpp>
- #include <sstream>
- using severity = ::boost::log::trivial::severity_level;
- #define CONCAT( A, B ) A ## B
- #define SELECT( NAME, NUM ) CONCAT( NAME ## _, NUM )
- #define GET_COUNT( _1, _2, _3 /* ad nauseam */, COUNT, ... ) COUNT
- #define VA_SIZE( ... ) GET_COUNT( __VA_ARGS__, 3, 2, 1 )
- #define VA_SELECT( NAME, ... ) SELECT( NAME, VA_SIZE(__VA_ARGS__) )(__VA_ARGS__)
- #define LOG( ... ) VA_SELECT( LOG, __VA_ARGS__ )
- #define LOG_2(sev, scope) \
- BOOST_LOG_SEV( logger::Logger::getInstance().getSeverityLogger(), sev) \
- << boost::log::add_value("Line", __LINE__) \
- << boost::log::add_value("File", __FILE__) \
- << boost::log::add_value("Scope", std::string(scope))
- #define LOG_1(sev) LOG_2(sev, "none")
- #define SAFELOG(...) std::cout << logger::getCurrentTime() << ": " << '[' \
- << logger::path_to_filename(__FILE__) << ':' << __LINE__ << "] " << logger::format(__VA_ARGS__)
- namespace logger {
- namespace logging = boost::log;
- namespace attrs = boost::log::attributes;
- namespace expr = boost::log::expressions;
- namespace src = boost::log::sources;
- namespace keywords = boost::log::keywords;
- namespace sinks = boost::log::sinks;
- inline std::string getCurrentTime() {
- std::stringstream ss;
- ss << boost::posix_time::microsec_clock::local_time();
- return ss.str().substr(ss.str().find_last_of(" ")+1);;
- }
- // Convert file path to only the filename
- inline std::string path_to_filename(std::string path) {
- return path.substr(path.find_last_of("/\\")+1);
- }
- inline std::string format(const char *fmt, ...)
- {
- va_list args;
- va_start(args, fmt);
- std::vector<char> v(1024);
- while (true)
- {
- va_list args2;
- va_copy(args2, args);
- int res = vsnprintf(v.data(), v.size(), fmt, args2);
- if ((res >= 0) && (res < static_cast<int>(v.size())))
- {
- va_end(args);
- va_end(args2);
- return std::string(v.data());
- }
- size_t size;
- if (res < 0)
- size = v.size() * 2;
- else
- size = static_cast<size_t>(res) + 1;
- v.clear();
- v.resize(size);
- va_end(args2);
- }
- }
- inline void custom_formatter(logging::record_view const& rec, logging::formatting_ostream& strm)
- {
- std::stringstream ss;
- ss << boost::posix_time::microsec_clock::local_time();
- strm << ss.str().substr(ss.str().find_last_of(" ") + 1);
- strm << expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%H:%M:%S.%f");
- strm << " <" << rec[logging::trivial::severity] << "> ";
- strm << "(" << logging::extract< std::string >("Scope", rec) << ")[";
- logging::value_ref< std::string > fullpath = logging::extract< std::string >("File", rec);
- strm << boost::filesystem::path(fullpath.get()).filename().string() << ":";
- strm << logging::extract< int >("Line", rec) << "] ";
- strm << rec[expr::smessage];
- }
- class Logger
- {
- private:
- Logger() {
- this->init();
- }
- Logger(Logger const&) = delete;
- Logger& operator= (Logger const&) = delete;
- void init() {
- typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
- boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
- sink->locked_backend()->add_stream(
- boost::shared_ptr<std::ostream>(&std::cout, boost::null_deleter()));
- sink->set_formatter(&custom_formatter);
- logging::core::get()->add_sink(sink);
- logging::core::get()->set_filter
- (
- logging::trivial::severity >= logging::trivial::trace //trace,debug,info,warning,error,fatal
- );
- }
- public:
- static Logger& getInstance() {
- static Logger inst;
- return inst;
- }
- static src::severity_logger<logging::trivial::severity_level>& getSeverityLogger() {
- static src::severity_logger<logging::trivial::severity_level> lg;
- return lg;
- }
- };
- } // namespace logger
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement