2015-01-09 67 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")) 
);