2013-04-19 7 views
11

Ich möchte eine einfache Protokolldatei in einer parallelen Anwendung. Ich habe Boost.Log v2.0 herunterladen und mit Boost 1.53.0 kompiliert.Wie Boost.Log in Datei umleiten

Das Problem ist, dass Boost.Log Ausgabe auf der Konsole. Ich verwende BOOST_LOG_TRIVIAL(trace).

Gibt es eine nette Möglichkeit, BOOST_LOG_TRIVIAL in eine Datei umzuleiten?

Antwort

15

können Sie BOOST_LOG_TRIVIAL eine Datei verwenden, machen mit (unter der Annahme, dass namespace logging = boost::log;:

#include <boost/log/core.hpp> 
#include <boost/log/trivial.hpp> 
#include <boost/log/expressions.hpp> 
#include <boost/log/utility/setup/file.hpp> 

void init() 
{ 
    logging::add_file_log("sample.log"); 

    logging::core::get()->set_filter 
    (
     logging::trivial::severity >= logging::trivial::info 
    ); 
} 

Und in Haupt:

int main(int, char*[]) 
{ 
    init(); 

    BOOST_LOG_TRIVIAL(trace) << "A trace severity message"; 
// other types of severity 
    BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message"; 

    return 0; 
} 
+0

welche Header habe ich für die Protokollierung :: add_file_log enthalten ("sample.log ")? –

+2

@ elvis.dukaj:' #include ' – m0nhawk