1.log添加函数打印。

This commit is contained in:
amass 2024-01-14 21:20:04 +08:00
parent df14b67695
commit edeb230c46
3 changed files with 80 additions and 10 deletions

View File

@ -7,16 +7,11 @@
#include "AndroidBoostLog.h"
#endif
BOOST_LOG_GLOBAL_LOGGER_INIT(location_logger, LocationLogger<boost::log::trivial::severity_level>) {
LocationLogger<boost::log::trivial::severity_level> 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<boost::log::trivial::severity_level>) {
LocationLogger<boost::log::trivial::severity_level> 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;
}

View File

@ -66,16 +66,19 @@ void initialize(const std::string &filename = "logs/app", const std::string &tar
trivial::severity_level filter = static_cast<trivial::severity_level>(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<FilenameTaggerFeature> {};
template <typename BaseT>
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 <typename ArgsT>
FunctionTaggerFeature(const ArgsT &args);
typedef typename boost::log::strictest_lock<boost::lock_guard<threading_model>, typename BaseT::open_record_lock,
typename BaseT::add_attribute_lock,
typename BaseT::remove_attribute_lock>::type open_record_lock;
protected:
template <typename ArgsT>
boost::log::record open_record_unlocked(const ArgsT &args);
};
struct FunctionTagger : public boost::mpl::quote1<FunctionTaggerFeature> {};
template <typename BaseT>
class LineTaggerFeature : public BaseT {
public:
@ -164,8 +189,8 @@ template <typename LevelT = int>
class LocationLogger
: public boost::log::sources::basic_composite_logger<
char, LocationLogger<LevelT>, boost::log::sources::multi_thread_model<boost::log::aux::light_rw_mutex>,
boost::log::sources::features<boost::log::sources::severity<LevelT>, FilenameTagger, LineTagger,
CategoryTagger>> {
boost::log::sources::features<boost::log::sources::severity<LevelT>, FilenameTagger, FunctionTagger,
LineTagger, CategoryTagger>> {
typedef typename LocationLogger::logger_base base_type;
public:
@ -180,12 +205,14 @@ BOOST_LOG_GLOBAL_LOGGER(location_logger, LocationLogger<boost::log::trivial::sev
BOOST_LOG_STREAM_WITH_PARAMS(::location_logger::get(),\
(::boost::log::keywords::severity = ::boost::log::trivial::lvl) \
(AmassKeywords::FilenameTag = (AmassKeywords::fileName(__FILE__))) \
(AmassKeywords::FunctionTag = __func__) \
(AmassKeywords::LineTag = __LINE__))
#define LOG_CAT( lvl, cat) \
BOOST_LOG_STREAM_WITH_PARAMS(::location_logger::get(),\
(::boost::log::keywords::severity = ::boost::log::trivial::lvl) \
(AmassKeywords::FilenameTag = (AmassKeywords::fileName(__FILE__))) \
(AmassKeywords::FunctionTag = __func__) \
(AmassKeywords::LineTag = __LINE__) \
(AmassKeywords::CategoryTag = #cat))

View File

@ -125,6 +125,36 @@ boost::log::record FilenameTaggerFeature<BaseT>::open_record_unlocked(const Args
return BaseT::open_record_unlocked(args);
}
template <typename BaseT>
FunctionTaggerFeature<BaseT>::FunctionTaggerFeature(const FunctionTaggerFeature &obj)
: BaseT(static_cast<const BaseT &>(obj)) {
}
template <typename BaseT>
template <typename ArgsT>
FunctionTaggerFeature<BaseT>::FunctionTaggerFeature(const ArgsT &args) : BaseT(args) {}
template <typename BaseT>
template <typename ArgsT>
boost::log::record FunctionTaggerFeature<BaseT>::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<boost::log::attribute_set::iterator, bool> res =
BaseT::add_attribute_unlocked("Function", boost::log::attributes::constant<std::string>(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 <typename BaseT>
template <typename ArgsT>
LineTaggerFeature<BaseT>::LineTaggerFeature(const ArgsT &args) : BaseT(args) {}