Kylin/Universal/BoostLog.cpp

93 lines
3.2 KiB
C++
Raw Normal View History

2023-07-21 10:47:31 +08:00
#include "BoostLog.h"
#include <boost/log/expressions.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/support/date_time.hpp>
#ifdef ANDROID
#include "AndroidBoostLog.h"
#endif
2023-12-27 10:29:16 +08:00
namespace boost {
namespace log {
2024-01-14 21:20:04 +08:00
static bool enableConsole = true;
static decltype(boost::log::add_console_log()) console;
2023-12-27 10:29:16 +08:00
void initialize(const std::string &filename, const std::string &target, boost::log::trivial::severity_level filter) {
2023-07-21 10:47:31 +08:00
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<AndroidSinkBackend>;
auto sink = boost::make_shared<AndroidSink>();
sink->set_formatter(&androidLogFormatter);
boost::log::core::get()->add_sink(sink);
#else
std::ostringstream oss;
2023-12-25 18:52:51 +08:00
oss << filename << "_%Y-%m-%d_%H.%M.%S_%N.log";
2024-09-15 23:39:23 +08:00
auto fileSink = add_file_log(keywords::file_name = oss.str(), keywords::auto_flush = true, keywords::target = target);
2023-12-27 21:49:33 +08:00
fileSink->set_formatter(&defaultFormatter);
2023-07-21 10:47:31 +08:00
#endif
initialized = true;
}
}
2023-12-27 21:49:33 +08:00
void defaultFormatter(const boost::log::record_view &record, boost::log::formatting_ostream &ostream) {
2023-07-21 10:47:31 +08:00
using namespace boost::log;
std::string level;
boost::log::formatting_ostream oss(level);
oss << "[" << record[boost::log::trivial::severity] << "]";
2024-09-15 23:39:23 +08:00
auto dateTimeFormatter = expressions::stream
<< expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", "[%m-%d %H:%M:%S.%f]");
2023-07-21 10:47:31 +08:00
dateTimeFormatter(record, ostream);
2024-09-15 23:39:23 +08:00
ostream << "[" << boost::log::extract<boost::log::attributes::current_thread_id::value_type>("ThreadID", record) << "]";
2023-07-21 10:47:31 +08:00
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];
}
2023-12-27 21:49:33 +08:00
2024-01-14 21:20:04 +08:00
void removeConsoleLog() {
if (console) {
boost::log::core::get()->remove_sink(console);
console.reset();
}
enableConsole = false;
}
2023-12-27 21:49:33 +08:00
} // namespace log
} // namespace boost
2024-01-14 21:20:04 +08:00
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;
}
2024-09-15 23:39:23 +08:00
namespace std {
ostream &operator<<(ostream &os, const map<int, int> &m) {
os << "{ ";
for (const auto &pair : m) {
os << "{" << pair.first << ", " << pair.second << "} ";
}
os << "}";
return os;
}
} // namespace std