2015-01-09 43 views
2

一般來說,如何在boost日誌格式表達式上執行字符串操作?特別是,我該如何截斷以小數秒結尾的TimeStamp表達式,以便記錄毫秒而不是微秒?如何截斷boost日誌格式表達式

鑑於此片段,我如何記錄,例如,13:13:08.440而不是13:13:08.440736?

logging::add_file_log("xyz.log", 
    keywords::format = expr::stream 
     << expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%T.%f") 
); 

我想要做這樣的事情:

<< expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%T.%.3f") 
+0

你有沒有想過這個?我在同一條船上。 – Kurt 2015-03-20 05:40:20

+0

沒有。我只活了幾微秒。 – plong 2015-03-23 14:26:26

回答

1

目前建成Boost.Log沒有這樣的功能。但是,您可以執行的操作是編寫自己的格式化函數,並將其設置爲Boost.Log接收器的格式器。您可以創建一個函數來格式化整個記錄(例如,參見here),或者僅爲特定屬性定義operator<<Here就是一個例子。還要注意,Boost.Log表達式基於Boost.Phoenix,所以在過濾器和格式化程序中使用Boost.Phoenix結構也是可能的。例如:

std::string my_formatter(logging::value_ref<boost::posix_time::ptime> const& timestamp) 
{ 
    if (timestamp) 
    { 
     // Format date/time here and return the result. 
    } 
    else 
    { 
     // The "TimeStamp" attribute value was not found or has unexpected type. 
     // Return a placeholder string. 
    } 
} 

logging::add_file_log("xyz.log", 
    keywords::format = expr::stream 
     << boost::phoenix::bind(&my_formatter, expr::attr<boost::posix_time::ptime>("TimeStamp")) 
);