I want to define a global logger that can be used in other place.
The severity level always be "info".
#include <cstdlib>
#include <boost/log/trivial.hpp>
#include <boost/log/core.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sources/severity_feature.hpp>
// Here we define our application severity levels.
enum severity_level
{
normal,
notification,
warning,
error,
critical
};
// The formatting logic for the severity level
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
{
static const char* const str[] =
{
"normal",
"notification",
"warning",
"error",
"critical"
};
if (static_cast<std::size_t>(lvl) < (sizeof(str) / sizeof(*str)))
strm << str[lvl];
else
strm << static_cast<int>(lvl);
return strm;
}
// Global logger declaration
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg,
boost::log::sources::severity_logger_mt<severity_level>)
void try_logging()
{
boost::log::sources::severity_logger_mt<severity_level>& lg = test_lg::get();
// TODO:
// always write "info" record, Unknown reason!
BOOST_LOG_SEV(lg, normal) << "This is a normal severity record";
BOOST_LOG_SEV(lg, notification) << "This is a notification severity record";
BOOST_LOG_SEV(lg, warning) << "This is a warning severity record";
BOOST_LOG_SEV(lg, error) << "This is a error severity record";
BOOST_LOG_SEV(lg, critical) << "This is a critical severity record";
}
int main(int argc, char* argv[])
{
try_logging();
return EXIT_SUCCESS;
}
[2015-01-14 19:00:34.800935] [0x40000300] [info] This is a normal severity re
cord
[2015-01-14 19:00:34.805935] [0x40000300] [info] This is a notification sever
ity record
[2015-01-14 19:00:34.806935] [0x40000300] [info] This is a warning severity r
ecord
[2015-01-14 19:00:34.806935] [0x40000300] [info] This is a error severity rec
ord
[2015-01-14 19:00:34.806935] [0x40000300] [info] This is a critical severity
record