Hi Igor, Thanks for your help!
Is that to say i MUST do other initialization when i use
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT ?
Why does it initialized by boost::log::sources::severity_logger_mt ?
Best Regards,
Bill
On 1/14/15, Igor Mironchik wrote:
Hi. You missed this:
BOOST_LOG_ATTRIBUTE_KEYWORD( severity, "Severity", severity_level )
BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "TimeStamp",
boost::posix_time::ptime)
using namespace boost::log;
void init_logging()
{
auto sink = boost::log::add_file_log
(
"sample.log",
keywords::format = expressions::stream
<< expressions::format_date_time( timestamp, "%Y-%m-%d, %H:%M:%S.%f" )
<< " <" << severity.or_default(normal)
<< "> " << expressions::message
);
// The sink will perform character code conversion as needed, according
to the locale set with imbue()
std::locale loc = boost::locale::generator()("en_US.UTF-8");
sink->imbue(loc);
// Let's add some commonly used attributes, like timestamp and record
counter.
boost::log::add_common_attributes();
}
On Wed, 14 Jan 2015 14:02:49 +0300, Bill Tian
wrote:
Hi guys,
I want to define a global logger that can be used in other place.
The severity level always be "info".
I init the log like this:
#include <cstdlib>
#include
#include
#include
#include
#include
#include
// 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_caststd::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)
void try_logging()
{
boost::log::sources::severity_logger_mt& 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;
}
In the log i got this:
[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
What do i miss?
Thank you!
Bill
--
Best Regards,
Igor Mironchik.