#include "BoostLog.h" #include #include #include #include #ifdef ANDROID #include "AndroidBoostLog.h" #endif 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; if (!initialized) { boost::log::core::get()->set_filter(boost::log::trivial::severity >= filter); add_common_attributes(); #ifdef ANDROID using AndroidSink = boost::log::sinks::synchronous_sink; auto sink = boost::make_shared(); sink->set_formatter(&androidLogFormatter); boost::log::core::get()->add_sink(sink); #else std::ostringstream oss; oss << filename << "_%Y-%m-%d_%H.%M.%S_%N.log"; auto fileSink = add_file_log(keywords::file_name = oss.str(), keywords::auto_flush = true, keywords::target = target); fileSink->set_formatter(&defaultFormatter); #endif initialized = true; } } void defaultFormatter(const boost::log::record_view &record, boost::log::formatting_ostream &ostream) { using namespace boost::log; std::string level; boost::log::formatting_ostream oss(level); oss << "[" << record[boost::log::trivial::severity] << "]"; auto dateTimeFormatter = expressions::stream << expressions::format_date_time( "TimeStamp", "[%m-%d %H:%M:%S.%f]"); dateTimeFormatter(record, ostream); ostream << "[" << boost::log::extract("ThreadID", record) << "]"; ostream << std::setw(8) << std::left << level; auto &&category = record[AmassKeywords::category]; if (!category.empty()) ostream << " [" << category << "]"; const auto &filename = record[AmassKeywords::filename]; if (!filename.empty()) { #ifdef QT_CREATOR_CONSOLE ostream << " [file:///" << filename << ":" << record[AmassKeywords::line] << "] "; #else ostream << " [" << filename << ":" << record[AmassKeywords::line] << "] "; #endif } // Finally, put the record message to the stream 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; }