// Copyright: All Rights Reserved // Author: egametang@gmail.com (tanghai) #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "Log/Log.h" #include "Base/Exception.h" using namespace boost::log; namespace Egametang { std::string FileName(std::string s) { boost::filesystem::path path(s); return path.filename().string(); } boost::scoped_ptr< boost::log::sources::severity_logger > Log::slog; void Log::Init(std::string fileName) { slog.reset(new boost::log::sources::severity_logger()); auto core = core::get(); typedef sinks::synchronous_sink text_sink; auto pSink = boost::make_shared(); std::string logFileName = FileName(fileName) + ".log"; auto logStream = boost::make_shared(logFileName.c_str()); if (!logStream->good()) { throw std::runtime_error("Failed to open a log file"); } pSink->locked_backend()->add_stream(logStream); pSink->locked_backend()->add_stream( boost::shared_ptr(&std::clog, boost::log::empty_deleter())); pSink->set_formatter( expressions::format("[%1%][%2%][%3%]%4%") % expressions::attr< unsigned int >("RecordID") % expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f") % expressions::attr("ThreadID") % expressions::smessage ); core->add_global_attribute("RecordID", attributes::counter(1)); core->add_global_attribute("TimeStamp", attributes::local_clock()); core->add_global_attribute("ThreadID", attributes::current_thread_id()); core->add_sink(pSink); } boost::log::sources::severity_logger& Log::GetSLog() { if (!slog.get()) { throw Exception() << ErrInfo("use log please Init in main function"); } return *slog; } } // Egametang