Last active
August 29, 2015 14:11
-
-
Save akira-okumura/c27a7f8bcf7f18c33a81 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Logger.h" | |
namespace MyProject { | |
// Suppress a warning of "warning: missing initializer..." | |
// http://stackoverflow.com/questions/27354654/how-to-suppress-warning-missing-initializer-caused-by-boost-log-and-gcc-4-4 | |
#pragma GCC diagnostic ignored "-Wmissing-field-initializers" // Disable | |
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", Logger::ESeverityLevel) | |
#pragma GCC diagnostic warning "-Wmissing-field-initializers" // Re-enable | |
Logger gLogger; | |
Logger::Logger(const std::string& fileName) | |
{ | |
fSink = boost::make_shared<text_sink>(); | |
fSink->locked_backend()->add_stream(boost::make_shared<std::ofstream>(fileName)); | |
logging::core::get()->add_sink(fSink); | |
logging::add_common_attributes(); | |
InitFormat(); | |
SetFilterLevel(kWarning); | |
} | |
void Logger::InitFormat() | |
{ | |
static attrs::utc_clock timeStamp; | |
static attrs::counter<uint64_t> recordID(1); | |
logging::core::get()->add_global_attribute("UTCTimeStamp", timeStamp); | |
logging::core::get()->add_global_attribute("RecordID", recordID); | |
fSink->set_formatter(expr::stream | |
<< expr::attr<uint64_t>("RecordID") << " " | |
<< expr::format_date_time<boost::posix_time::ptime>("UTCTimeStamp", "%Y/%m/%d %H:%M:%S.%f (UTC)") | |
<< " [" << expr::attr<ESeverityLevel>("Severity") << "] " | |
<< expr::smessage); | |
} | |
void Logger::SetFilterLevel(ESeverityLevel level) | |
{ | |
fSink->set_filter(severity >= level); | |
} | |
} // MyProject |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment