diff --git a/Universal/BoostLog.cpp b/Universal/BoostLog.cpp index 82da5fc..0200be6 100644 --- a/Universal/BoostLog.cpp +++ b/Universal/BoostLog.cpp @@ -7,16 +7,11 @@ #include "AndroidBoostLog.h" #endif -BOOST_LOG_GLOBAL_LOGGER_INIT(location_logger, LocationLogger) { - LocationLogger lg; - auto consoleSink = boost::log::add_console_log(); - consoleSink->set_formatter(&boost::log::defaultFormatter); - boost::log::add_common_attributes(); - return lg; -} - namespace boost { namespace log { +static bool enableConsole = true; +static decltype(boost::log::add_console_log()) console; + void initialize(const std::string &filename, const std::string &target, boost::log::trivial::severity_level filter) { static bool initialized = false; using namespace boost::log; @@ -67,5 +62,23 @@ void defaultFormatter(const boost::log::record_view &record, boost::log::formatt ostream << record[expressions::smessage]; } +void removeConsoleLog() { + if (console) { + boost::log::core::get()->remove_sink(console); + console.reset(); + } + enableConsole = false; +} + } // namespace log } // namespace boost + +BOOST_LOG_GLOBAL_LOGGER_INIT(location_logger, LocationLogger) { + LocationLogger lg; + boost::log::add_common_attributes(); + if (boost::log::enableConsole) { + boost::log::console = boost::log::add_console_log(); + boost::log::console->set_formatter(&boost::log::defaultFormatter); + } + return lg; +} diff --git a/Universal/BoostLog.h b/Universal/BoostLog.h index 3e28f37..195fcf7 100644 --- a/Universal/BoostLog.h +++ b/Universal/BoostLog.h @@ -66,16 +66,19 @@ void initialize(const std::string &filename = "logs/app", const std::string &tar trivial::severity_level filter = static_cast(LOG_FILTER_LEVEL)); void defaultFormatter(boost::log::record_view const &record, boost::log::formatting_ostream &ostream); +void removeConsoleLog(); } // namespace log } // namespace boost namespace AmassKeywords { BOOST_PARAMETER_KEYWORD(FilenameNS, FilenameTag) +BOOST_PARAMETER_KEYWORD(FunctionNS, FunctionTag) BOOST_PARAMETER_KEYWORD(LineNS, LineTag) BOOST_PARAMETER_KEYWORD(CategoryNS, CategoryTag) BOOST_LOG_ATTRIBUTE_KEYWORD(filename, "Filename", std::string) +BOOST_LOG_ATTRIBUTE_KEYWORD(function, "Function", std::string) BOOST_LOG_ATTRIBUTE_KEYWORD(line, "Line", size_t) BOOST_LOG_ATTRIBUTE_KEYWORD(category, "Category", std::string) @@ -138,6 +141,28 @@ protected: struct FilenameTagger : public boost::mpl::quote1 {}; +template +class FunctionTaggerFeature : public BaseT { +public: + typedef typename BaseT::char_type char_type; + typedef typename BaseT::threading_model threading_model; + FunctionTaggerFeature() = default; + FunctionTaggerFeature(const FunctionTaggerFeature &obj); + + template + FunctionTaggerFeature(const ArgsT &args); + + typedef typename boost::log::strictest_lock, typename BaseT::open_record_lock, + typename BaseT::add_attribute_lock, + typename BaseT::remove_attribute_lock>::type open_record_lock; + +protected: + template + boost::log::record open_record_unlocked(const ArgsT &args); +}; + +struct FunctionTagger : public boost::mpl::quote1 {}; + template class LineTaggerFeature : public BaseT { public: @@ -164,8 +189,8 @@ template class LocationLogger : public boost::log::sources::basic_composite_logger< char, LocationLogger, boost::log::sources::multi_thread_model, - boost::log::sources::features, FilenameTagger, LineTagger, - CategoryTagger>> { + boost::log::sources::features, FilenameTagger, FunctionTagger, + LineTagger, CategoryTagger>> { typedef typename LocationLogger::logger_base base_type; public: @@ -180,12 +205,14 @@ BOOST_LOG_GLOBAL_LOGGER(location_logger, LocationLogger::open_record_unlocked(const Args return BaseT::open_record_unlocked(args); } +template +FunctionTaggerFeature::FunctionTaggerFeature(const FunctionTaggerFeature &obj) + : BaseT(static_cast(obj)) { +} + +template +template +FunctionTaggerFeature::FunctionTaggerFeature(const ArgsT &args) : BaseT(args) {} + +template +template +boost::log::record FunctionTaggerFeature::open_record_unlocked(const ArgsT &args) { + std::string tag_value = args[AmassKeywords::FunctionTag | std::string()]; + boost::log::attribute_set &attrs = BaseT::attributes(); + boost::log::attribute_set::iterator tag = attrs.end(); + if (!tag_value.empty()) { + // Add the tag as a new attribute + std::pair res = + BaseT::add_attribute_unlocked("Function", boost::log::attributes::constant(tag_value)); + if (res.second) tag = res.first; + } + + BOOST_SCOPE_EXIT_TPL((&tag)(&attrs)) { + if (tag != attrs.end()) attrs.erase(tag); + } + BOOST_SCOPE_EXIT_END + + return BaseT::open_record_unlocked(args); +} + template template LineTaggerFeature::LineTaggerFeature(const ArgsT &args) : BaseT(args) {}