Ich lerne die Boost.Log-Bibliothek. Ich möchte Nachrichten an eine Datei senden und std::clog
. Ich habe die folgende Klasse:Boost.Log: Warum wird die Nachricht doppelt angezeigt?
class logger
{
public:
explicit logger(
const std::string& tag,
const std::string& file,
bool console
)
{
boost::log::register_simple_formatter_factory< boost::log::trivial::severity_level, char >("Severity");
std::string the_format = "[%TimeStamp%] (%LineID%) [%Severity%]";
if(!tag.empty()) {
m_log_.add_attribute(
"Tag",
boost::log::attributes::constant<std::string>(tag)
);
the_format += " [%Tag%]";
}
the_format += ": %Message%";
if(console) {
boost::log::add_console_log(
std::clog,
boost::log::keywords::auto_flush = true,
boost::log::keywords::format = the_format
);
}
if(!file.empty()) {
boost::log::add_file_log(
boost::log::keywords::file_name = file,
boost::log::keywords::auto_flush = true,
boost::log::keywords::open_mode = (std::ios::out | std::ios::app),
boost::log::keywords::format = the_format
);
}
}
~logger(void)
{ }
void log(
const std::string& msg
)
{
BOOST_LOG_SEV (m_log_, boost::log::trivial::info) << msg;
}
private:
boost::log::sources::severity_logger<boost::log::trivial::severity_level> m_log_;
}; // logger
Ich habe folgende main()
Funktion:
void x()
{
logger lg("omega", "", true);
lg.log("Goodbye");
}
int main(int argc, char** argv)
{
logger lg("alfa", "", true);
lg.log("Hello world!!!");
x();
return 0;
}
Ich verstehe nicht, warum die doppelte Nachricht angezeigt: "Hallo Welt !!!":
[2016-Aug-23 17:51:36.852912] (1) [info] [alfa]: Hello world!!!
[2016-Aug-23 17:51:36.853359] (2) [info] [omega]: Goodbye
[2016-Aug-23 17:51:36.853359] (2) [info] [omega]: Goodbye
UPDATE: Entschuldigung, das Beispiel war unvollständig.
Welche Boost-Version verwenden Sie? Ich kann das nicht mit 1.58 reproduzieren. Welche Plattform? – isanae
@isanae, es tut mir leid, das Beispiel war unvollständig. Ich benutze die Version 1.60 und das Betriebssystem ist GNU/Linux. –