Boost C++ Libraries: Ticket #10238: syslog pri part error in custom severity (init_from_stream) https://svn.boost.org/trac10/ticket/10238 <p> Hi all, I'm trying to tune logging from settings to be able to send message to Syslog, <a class="missing wiki">TextFile</a> and Console. I use %Severity% keyword in all these cases. My task is get text value of severity in <a class="missing wiki">TextFile</a> and Console, and to get correct format of message in Syslog. </p> <p> During executing the code below I've got an error pri part in <a class="missing wiki">SyslogViewer</a> [ #<a class="missing wiki">TextFile</a> - OK &lt;DEBUG&gt;[2014-Jul-24 10:27:28.611879] This is a debug severity record &lt;INFO&gt;[2014-Jul-24 10:27:28.627501] This is a info severity record &lt;WARNING&gt;[2014-Jul-24 10:27:28.658746] This is a warning severity record &lt;ERROR&gt;[2014-Jul-24 10:27:28.674369] This is a error severity record &lt;CRITICAL&gt;[2014-Jul-24 10:27:28.705613] This is a critical severity record </p> <p> #Syslog - ERROR PRI PART &lt;14&gt; Jul 24 10:27:28 10.10.10.73 test.exe This is a debug severity record &lt;14&gt; Jul 24 10:27:28 10.10.10.73 test.exe This is a info severity record &lt;14&gt; Jul 24 10:27:28 10.10.10.73 test.exe This is a warning severity record &lt;14&gt; Jul 24 10:27:28 10.10.10.73 test.exe This is a error severity record &lt;14&gt; Jul 24 10:27:28 10.10.10.73 test.exe This is a critical severity record ] Every record has Syslog pri part equals to &lt;14&gt;. That is mistake! </p> <p> P.S. If I use int instead of enum as severity level, everything is ok in Syslog, but in this case I can not get text values of %Severity% in <a class="missing wiki">TextFile</a> and Console [ #<a class="missing wiki">TextFile</a> - I need %Severity% as text here &lt;15&gt;[2014-Jul-24 10:27:28.611879] This is a debug severity record &lt;14&gt;[2014-Jul-24 10:27:28.627501] This is a info severity record &lt;12&gt;[2014-Jul-24 10:27:28.658746] This is a warning severity record &lt;11&gt;[2014-Jul-24 10:27:28.674369] This is a error severity record &lt;10&gt;[2014-Jul-24 10:27:28.705613] This is a critical severity record </p> <p> #Syslog &lt;15&gt; Jul 24 10:44:16 <a class="missing wiki">MaZhorWork</a> fmod_test.exe This is a debug severity record &lt;14&gt; Jul 24 10:44:16 <a class="missing wiki">MaZhorWork</a> fmod_test.exe This is a info severity record &lt;12&gt; Jul 24 10:44:17 <a class="missing wiki">MaZhorWork</a> fmod_test.exe This is a warning severity record &lt;11&gt; Jul 24 10:44:17 <a class="missing wiki">MaZhorWork</a> fmod_test.exe This is a error severity record &lt;10&gt; Jul 24 10:44:17 <a class="missing wiki">MaZhorWork</a> fmod_test.exe This is a critical severity record ] </p> <p> P.S.2. In my opinion problem is in $(BOOST_ROOT_55)\libs\log\src\init_from_settings.cpp, line 453: </p> <pre class="wiki"> // For now we use only the default level mapping. Will add support for configuration later. backend-&gt;set_severity_mapper(sinks::syslog::direct_severity_mapping&lt; &gt;(log::aux::default_attribute_names::severity())); </pre><p> My code that I tried </p> <pre class="wiki">//Here we define our application severity levels. typedef enum TSysLogSeverity { slEmergency = boost::log::sinks::syslog::emergency, slAlert = boost::log::sinks::syslog::alert, slCritical = boost::log::sinks::syslog::critical, slError = boost::log::sinks::syslog::error, slWarning = boost::log::sinks::syslog::warning, slNotice = boost::log::sinks::syslog::notice, slInfo = boost::log::sinks::syslog::info, slDebug = boost::log::sinks::syslog::debug }; // The operator is used for regular stream formatting std::ostream&amp; operator&lt;&lt; (std::ostream&amp; strm, TSysLogSeverity level) { static const char* strings[] = { "EMERGENCY", "ALERT", "CRITICAL", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG" }; if (static_cast&lt; std::size_t &gt;(level) &lt; sizeof(strings) / sizeof(*strings)) strm &lt;&lt; strings[level]; else strm &lt;&lt; level; return strm; } std::istream&amp; operator&gt;&gt;(std::istream&amp; i, TSysLogSeverity &amp;level) { std::string s_tmp; i &gt;&gt; s_tmp; static const char* strings[] = { "EMERGENCY", "ALERT", "CRITICAL", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG" }; bool bFound = false; const std::size_t i_array_size = sizeof(strings) / sizeof(*strings); for (int k = 0; k &lt; i_array_size ; k++) { if (s_tmp == std::string(strings[k])) { level = TSysLogSeverity(k); bFound = true; break; } } if (!bFound) { level = TSysLogSeverity(std::stoul(s_tmp)); } return i; } // Attribute value tag type struct severity_tag; // The operator is used when putting the severity level to log boost::log::formatting_ostream&amp; operator&lt;&lt; ( boost::log::formatting_ostream&amp; strm, boost::log::to_log_manip&lt; TSysLogSeverity, severity_tag &gt; const&amp; manip ) { static const char* strings[] = { "EMERGENCY", "ALERT", "CRITICAL", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG" }; TSysLogSeverity level = manip.get(); if (static_cast&lt; std::size_t &gt;(level) &lt; sizeof(strings) / sizeof(*strings)) strm &lt;&lt; strings[level]; else strm &lt;&lt; static_cast&lt; int &gt;(level); return strm; } // Global logger declaration BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg, src::severity_logger_mt&lt;TSysLogSeverity&gt;) void try_logging() { src::severity_logger_mt&lt;TSysLogSeverity&gt;&amp; lg = test_lg::get(); BOOST_LOG_SEV(lg, slDebug) &lt;&lt; "This is a debug severity record"; BOOST_LOG_SEV(lg, slInfo) &lt;&lt; "This is a info severity record"; BOOST_LOG_SEV(lg, slWarning) &lt;&lt; "This is a warning severity record"; BOOST_LOG_SEV(lg, slError) &lt;&lt; "This is a error severity record"; BOOST_LOG_SEV(lg, slCritical) &lt;&lt; "This is a critical severity record"; } int _tmain(int argc, _TCHAR* argv[]) { try { boost::filesystem::path p(argv[0]); p = p.replace_extension(".ini"); // Open the file std::ifstream settings(p.string()); if (!settings.is_open()) { throw std::runtime_error("Could not open &lt;" + p.string() + "&gt; file"); } boost::log::register_simple_formatter_factory&lt;TSysLogSeverity, char &gt;(boost::log::aux::default_attribute_names::severity()); boost::log::register_simple_filter_factory&lt;TSysLogSeverity&gt;(boost::log::aux::default_attribute_names::severity()); // Read the settings and initialize logging library logging::init_from_stream(settings); src::severity_logger_mt&lt;TSysLogSeverity&gt;&amp; lg = test_lg::get(); // Add some attributes logging::add_common_attributes(); // Try logging try_logging(); } catch (std::exception&amp; e) { std::cout &lt;&lt; "FAILURE: " &lt;&lt; e.what() &lt;&lt; std::endl; } _getch(); return 0; } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/10238 Trac 1.4.3 anonymous Thu, 24 Jul 2014 07:52:08 GMT <link>https://svn.boost.org/trac10/ticket/10238#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10238#comment:1</guid> <description> <p> I'm sorry for bad formatting, because it's my first ticket to boost. Here is detailed output that i've got </p> <p> 1) Using enum as severity level </p> <pre class="wiki">#TextFile - OK &lt;DEBUG&gt;[2014-Jul-24 10:27:28.611879] This is a debug severity record &lt;INFO&gt;[2014-Jul-24 10:27:28.627501] This is a info severity record &lt;WARNING&gt;[2014-Jul-24 10:27:28.658746] This is a warning severity record &lt;ERROR&gt;[2014-Jul-24 10:27:28.674369] This is a error severity record &lt;CRITICAL&gt;[2014-Jul-24 10:27:28.705613] This is a critical severity record #Syslog - ERROR PRI PART &lt;14&gt; Jul 24 10:27:28 10.10.10.73 test.exe This is a debug severity record &lt;14&gt; Jul 24 10:27:28 10.10.10.73 test.exe This is a info severity record &lt;14&gt; Jul 24 10:27:28 10.10.10.73 test.exe This is a warning severity record &lt;14&gt; Jul 24 10:27:28 10.10.10.73 test.exe This is a error severity record &lt;14&gt; Jul 24 10:27:28 10.10.10.73 test.exe This is a critical severity record </pre><p> 2) using int as severity level </p> <pre class="wiki">#TextFile - I need %Severity% as text here &lt;15&gt;[2014-Jul-24 10:27:28.611879] This is a debug severity record &lt;14&gt;[2014-Jul-24 10:27:28.627501] This is a info severity record &lt;12&gt;[2014-Jul-24 10:27:28.658746] This is a warning severity record &lt;11&gt;[2014-Jul-24 10:27:28.674369] This is a error severity record &lt;10&gt;[2014-Jul-24 10:27:28.705613] This is a critical severity record #Syslog &lt;15&gt; Jul 24 10:44:16 MaZhorWork fmod_test.exe This is a debug severity record &lt;14&gt; Jul 24 10:44:16 MaZhorWork fmod_test.exe This is a info severity record &lt;12&gt; Jul 24 10:44:17 MaZhorWork fmod_test.exe This is a warning severity record &lt;11&gt; Jul 24 10:44:17 MaZhorWork fmod_test.exe This is a error severity record &lt;10&gt; Jul 24 10:44:17 MaZhorWork fmod_test.exe This is a critical severity record </pre> </description> <category>Ticket</category> </item> <item> <author>alexander.zhornyak@…</author> <pubDate>Thu, 24 Jul 2014 07:59:34 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10238#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10238#comment:2</guid> <description> <p> Hi all, I'm trying to tune logging from settings to be able to send message to Syslog, TextFile_, Console. I use %Severity% keyword in all these cases. My task is get text value of severity in TextFile_, Console, to get correct format of message in Syslog. </p> <p> During executing the code below I've got an error pri part in Syslog. </p> <pre class="wiki">#TextFile - OK &lt;DEBUG&gt;[2014-Jul-24 10:27:28.611879] This is a debug severity record &lt;INFO&gt;[2014-Jul-24 10:27:28.627501] This is a info severity record &lt;WARNING&gt;[2014-Jul-24 10:27:28.658746] This is a warning severity record &lt;ERROR&gt;[2014-Jul-24 10:27:28.674369] This is a error severity record &lt;CRITICAL&gt;[2014-Jul-24 10:27:28.705613] This is a critical severity record #Syslog - ERROR PRI PART &lt;14&gt; Jul 24 10:27:28 10.10.10.73 test.exe This is a debug severity record &lt;14&gt; Jul 24 10:27:28 10.10.10.73 test.exe This is a info severity record &lt;14&gt; Jul 24 10:27:28 10.10.10.73 test.exe This is a warning severity record &lt;14&gt; Jul 24 10:27:28 10.10.10.73 test.exe This is a error severity record &lt;14&gt; Jul 24 10:27:28 10.10.10.73 test.exe This is a critical severity record </pre><p> Every record has Syslog pri part equals to &lt;14&gt;. That is mistake! </p> <p> P.S. If I use int instead of enum as severity level, everything is ok in Syslog, but in this case I can not get text values of %Severity% in TextFile_ and Console </p> <pre class="wiki">#TextFile - I need %Severity% as text here &lt;15&gt;[2014-Jul-24 10:27:28.611879] This is a debug severity record &lt;14&gt;[2014-Jul-24 10:27:28.627501] This is a info severity record &lt;12&gt;[2014-Jul-24 10:27:28.658746] This is a warning severity record &lt;11&gt;[2014-Jul-24 10:27:28.674369] This is a error severity record &lt;10&gt;[2014-Jul-24 10:27:28.705613] This is a critical severity record #Syslog - OK &lt;15&gt; Jul 24 10:44:16 MaZhorWork fmod_test.exe This is a debug severity record &lt;14&gt; Jul 24 10:44:16 MaZhorWork fmod_test.exe This is a info severity record &lt;12&gt; Jul 24 10:44:17 MaZhorWork fmod_test.exe This is a warning severity record &lt;11&gt; Jul 24 10:44:17 MaZhorWork fmod_test.exe This is a error severity record &lt;10&gt; Jul 24 10:44:17 MaZhorWork fmod_test.exe This is a critical severity record </pre><p> P.S.2. In my opinion problem is in $(BOOST_ROOT_55)\libs\log\src\init_from_settings.cpp, line 453: </p> <pre class="wiki"> // For now we use only the default level mapping. Will add support for configuration later. backend-&gt;set_severity_mapper(sinks::syslog::direct_severity_mapping&lt; &gt;(log::aux::default_attribute_names::severity())); </pre><p> My code that I tried </p> <pre class="wiki">//Here we define our application severity levels. typedef enum TSysLogSeverity { slEmergency = boost::log::sinks::syslog::emergency, slAlert = boost::log::sinks::syslog::alert, slCritical = boost::log::sinks::syslog::critical, slError = boost::log::sinks::syslog::error, slWarning = boost::log::sinks::syslog::warning, slNotice = boost::log::sinks::syslog::notice, slInfo = boost::log::sinks::syslog::info, slDebug = boost::log::sinks::syslog::debug }; // The operator is used for regular stream formatting std::ostream&amp; operator&lt;&lt; (std::ostream&amp; strm, TSysLogSeverity level) { static const char* strings[] = { "EMERGENCY", "ALERT", "CRITICAL", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG" }; if (static_cast&lt; std::size_t &gt;(level) &lt; sizeof(strings) / sizeof(*strings)) strm &lt;&lt; strings[level]; else strm &lt;&lt; level; return strm; } std::istream&amp; operator&gt;&gt;(std::istream&amp; i, TSysLogSeverity &amp;level) { std::string s_tmp; i &gt;&gt; s_tmp; static const char* strings[] = { "EMERGENCY", "ALERT", "CRITICAL", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG" }; bool bFound = false; const std::size_t i_array_size = sizeof(strings) / sizeof(*strings); for (int k = 0; k &lt; i_array_size ; k++) { if (s_tmp == std::string(strings[k])) { level = TSysLogSeverity(k); bFound = true; break; } } if (!bFound) { level = TSysLogSeverity(std::stoul(s_tmp)); } return i; } // Attribute value tag type struct severity_tag; // The operator is used when putting the severity level to log boost::log::formatting_ostream&amp; operator&lt;&lt; ( boost::log::formatting_ostream&amp; strm, boost::log::to_log_manip&lt; TSysLogSeverity, severity_tag &gt; const&amp; manip ) { static const char* strings[] = { "EMERGENCY", "ALERT", "CRITICAL", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG" }; TSysLogSeverity level = manip.get(); if (static_cast&lt; std::size_t &gt;(level) &lt; sizeof(strings) / sizeof(*strings)) strm &lt;&lt; strings[level]; else strm &lt;&lt; static_cast&lt; int &gt;(level); return strm; } // Global logger declaration BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg, src::severity_logger_mt&lt;TSysLogSeverity&gt;) void try_logging() { src::severity_logger_mt&lt;TSysLogSeverity&gt;&amp; lg = test_lg::get(); BOOST_LOG_SEV(lg, slDebug) &lt;&lt; "This is a debug severity record"; BOOST_LOG_SEV(lg, slInfo) &lt;&lt; "This is a info severity record"; BOOST_LOG_SEV(lg, slWarning) &lt;&lt; "This is a warning severity record"; BOOST_LOG_SEV(lg, slError) &lt;&lt; "This is a error severity record"; BOOST_LOG_SEV(lg, slCritical) &lt;&lt; "This is a critical severity record"; } int _tmain(int argc, _TCHAR* argv[]) { try { boost::filesystem::path p(argv[0]); p = p.replace_extension(".ini"); // Open the file std::ifstream settings(p.string()); if (!settings.is_open()) { throw std::runtime_error("Could not open &lt;" + p.string() + "&gt; file"); } boost::log::register_simple_formatter_factory&lt;TSysLogSeverity, char &gt;(boost::log::aux::default_attribute_names::severity()); boost::log::register_simple_filter_factory&lt;TSysLogSeverity&gt;(boost::log::aux::default_attribute_names::severity()); // Read the settings and initialize logging library logging::init_from_stream(settings); src::severity_logger_mt&lt;TSysLogSeverity&gt;&amp; lg = test_lg::get(); // Add some attributes logging::add_common_attributes(); // Try logging try_logging(); } catch (std::exception&amp; e) { std::cout &lt;&lt; "FAILURE: " &lt;&lt; e.what() &lt;&lt; std::endl; } _getch(); return 0; } </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Thu, 24 Jul 2014 08:53:16 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/10238#comment:3 https://svn.boost.org/trac10/ticket/10238#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> This is a known limitation. The library needs a severity mapping to convert from your enum to syslog::level values. When the library is initialized from file or settings it does not know about your enum and currently assumes the severity attribute values to be ints. When you pass your enum as the severity levels the library is not able to extract it from log records and defaults to the 'info' level. </p> <p> To work around the problem you'll have to create the sink yourself and specify the mapping of your enum to syslog severity levels (see the <a href="http://www.boost.org/doc/libs/1_55_0/libs/log/doc/html/log/detailed/sink_backends.html#log.detailed.sink_backends.syslog">example</a>). In order to integrate with the settings parser, your code that creates the sink has to be registered as a sink factory as described <a href="http://www.boost.org/doc/libs/1_55_0/libs/log/doc/html/log/extension/settings.html#log.extension.settings.adding_support_for_user_defined_sinks">here</a>. </p> Ticket alexander.zhornyak@… Thu, 24 Jul 2014 12:08:26 GMT <link>https://svn.boost.org/trac10/ticket/10238#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10238#comment:4</guid> <description> <p> Thank you for your answer. But there is a question why you can not put enum severity value directly to Pri without mapping, simply converting to int? If it is possible, it would be greate to have such flag at syslog backend as "direct to int" mapping. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Thu, 24 Jul 2014 12:15:55 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10238#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10238#comment:5</guid> <description> <p> You cannot extract an attribute value from a log record without knowing its type. Since the library is compiled separately, it has no knowledge of your enum and cannot extract it from log records. That's the same reason why you have to register filters and formatters before parsing. </p> </description> <category>Ticket</category> </item> </channel> </rss>