2011-07-18 7 views
3

Andrey SemashevのBoost.logライブラリを使用しようとしていますBoost.log同期の問題

設定ファイルでマルチスレッドログに問題があります。

メインプログラムのコード:

namespace logging = boost::log; 
namespace attrs = boost::log::attributes; 
namespace src = boost::log::sources; 
using boost::shared_ptr; 

// Here we define our application severity levels. 
enum severity_level 
{ 
    normal, 
    notification, 
    warning, 
    error, 
    critical 
}; 

BOOST_LOG_DECLARE_GLOBAL_LOGGER(test_lg, src::severity_logger_mt<>) 

void try_logging(std::string c) 
{ 
    BOOST_LOG_SCOPED_THREAD_TAG("Tag", std::string, c.c_str()); 

    src::severity_logger_mt<>& lg = get_test_lg(); 
    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 
    { 
     // Open the file 
     std::ifstream settings("settings.txt"); 
     if (!settings.is_open()) 
     { 
      std::cout << "Could not open settings.txt file" << std::endl; 
      return 1; 
     } 

     // Read the settings and initialize logging library 
     logging::init_from_stream(settings); 

     shared_ptr<logging::attribute> attr(new attrs::local_clock); 
     logging::core::get()->add_global_attribute("TimeStamp", attr); 

     boost::thread_group g; 
     for (char i = 'a'; i < 'z'; i++) 
      g.create_thread(boost::bind(&try_logging, std::string("aaa") + i)); 

     g.join_all(); 
     system("PAUSE"); 
     return 0; 
    } 
    catch (std::exception& e) 
    { 
     std::cout << "FAILURE: " << e.what() << std::endl; 
     return 1; 
    } 
} 

設定ファイル:

[Core] 

Filter="%Severity% >= 2" 

[Sink:2] 

Destination=TextFile 
FileName=test.log 
AutoFlush=true 
Format="[%TimeStamp%] %Tag% %_%" 
Asynchronous=1 
Filter="%Tag% = aaab" 

[Sink:3] 
Filter="%Tag% = aaaa" 
Asynchronous=1 
Destination=TextFile 
FileName=test.log 
AutoFlush=true 
Format="%Tag% %_%" 

ご覧のように、私は、セットアップのフィルタリング:私はAAAAスレッド、出力にAAABからメッセージだけをしたいです。

しかし、結果ファイルは非常に悪いです:

[2011-Jul-18 11:32:58.078192] aaab This aaaa This is a error severity [2011-Juaaaa This is a critical severity record 
r severity record 
[2011-Jul-18 11:32:58.437581] aaab This is a critical severity record 

1つのメッセージのオーバーライドその他!私が理解しているように、複数のスレッド間で同期はありません。

フィルタリングを削除すると、結果はすべてリジッドになります。

どうすれば修正できますか?可能であれば、コードではなく設定ファイルを使用してください。

ありがとうございます。

答えて

0

私はシングルトンクラスにそれを変更することをお勧めいたしますだろうが、私はあなたのコードに変更をしたくないので、私はあなたが以下をお勧めしますことを見た:

http://boost-log.sourceforge.net/libs/log/doc/html/log/detailed/attributes.html

は「変更可能なの確認定数 "項目を使用すると、問題の解決方法を見つけるのに役立ちます。