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% %_%"
正如你看到的,我安裝過濾:我想輸出AAAB只是消息,AAAA線程。
但結果文件是非常糟糕:
[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
一個消息凌駕!據我所知 - 多線程之間沒有同步。
如果我刪除篩選 - 結果是所有rigth。
我該如何解決它?如果可能的話 - 使用配置文件,而不是代碼。
謝謝。