// Copyright: All Rights Reserved // Author: egametang@gmail.com (tanghai) #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "Log/Log.h" DEFINE_bool(logtoconsole, false, "log messages go to stderr instead of logfiles"); using namespace boost::log; namespace Egametang { std::string FileName(const char* s) { boost::filesystem::path path(s); return path.filename().string(); } bool ELog::isInit = false; sources::severity_logger ELog::slog; void ELog::Init(const char* fileName) { if (isInit) { return; } isInit = true; auto core = core::get(); typedef sinks::synchronous_sink text_sink; auto pSink = std::make_shared(); std::string logFileName = FileName(fileName) + ".log"; auto logStream = std::make_shared(logFileName.c_str()); if (!logStream->good()) { throw std::runtime_error("Failed to open a log file"); } pSink->locked_backend()->add_stream(logStream); // 是否输出到标准错误 if (FLAGS_logtoconsole) { pSink->locked_backend()->add_stream( std::shared_ptr(&std::clog, boost::log::empty_deleter())); } pSink->locked_backend()->set_formatter( formatters::format("[%1%][%2%][%3%]%4%") % formatters::attr("Line #", keywords::format = "%08x") % formatters::date_time("TimeStamp") % formatters::attr("ThreadID", keywords::format = "%05d") % formatters::message() ); pSink->set_filter(boost::log::filters::attr("Severity", std::nothrow) >= INFO); core->add_global_attribute("Line #", std::make_shared>()); core->add_global_attribute("TimeStamp", std::make_shared()); core->add_global_attribute("ThreadID", std::make_shared()); core->add_sink(pSink); } boost::log::sources::severity_logger& ELog::GetSLog() { return slog; } } // Egametang