助推日志打印源$ C ​​$ C文件名和行号 [英] boost log to print source code file name and line number

查看:369
本文介绍了助推日志打印源$ C ​​$ C文件名和行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的C ++应用程序加速(1.55.0)记录。
我已经能够产生这种格式的日志

  [2014年07月15 10:47:26.137959]:其中,调试和GT;定期消息

我希望能够增加源文件名和行号
被生成的日志。

  [2014年07月15 10:47:26.137959]:其中,调试和GT; [文件名:LINE_NO]正则消息

例如:

  [2014年07月15 10:47:26.137959]:其中,调试和GT; [helloworld.cpp:12]常规消息

来源$ C ​​$ C:

 的#include<升压/日志/ core.hpp>
#包括LT&;升压/日志/ trivial.hpp>
#包括LT&;升压/日志/ EX pressions.hpp>
#包括LT&;升压/日志/片/ text_file_backend.hpp>
#包括LT&;升压/日志/工具/设置/ file.hpp>
#包括LT&;升压/日志/工具/设置/ common_attributes.hpp>
#包括LT&;升压/日志/来源/ severity_logger.hpp>
#包括LT&;升压/日志/来源/ record_ostream.hpp>
#包括LT&;升压/日志/ EX pressions.hpp>
#包括LT&;升压/日志/支持/ date_time.hpp>
#包括LT&;升压/日志/属性/ attribute.hpp>
#包括LT&;升压/日志/属性/ attribute_cast.hpp>
#包括LT&;升压/日志/属性/ attribute_value.hpp>
#包括LT&;升压/ make_shared.hpp>
#包括LT&;升压/ property_tree / ptree.hpp>空间日志=提振::登录;
命名空间SRC =的boost ::登录::来源;
命名空间=下沉的boost ::登录::汇;
命名空间关键字=提振::登录::关键词;无效的init()
{
    记录:: add_file_log
    (
        关键词:FILE_NAME =sample_%N.log/ * LT;文件名模式> * /
        关键词:rotation_size = 10 * 1024 * 1204,/ *<旋转文件每10 MIB ...> * /
        关键词:time_based_rotation =下沉::文件:: rotation_at_time_point(0,0,0)/ *< ...或者在午夜> * /
        关键词:格式=
        (
            提高::登录::前pressions ::流
                <<提高::登录::前pressions :: format_date_time<升压::了posix_time ::的ptime&GT(时间戳,%Y-%间 - %D_%H:%M:%S。%F)
                << :其中, <<提高::登录::琐碎::严重性和LT;< >中
                <<提高::登录::前pressions :: smessage
        )
    );
}INT主(INT,CHAR * [])
{
    在里面();
    测井:: add_common_attributes();    使用命名空间的日志记录::琐碎;
    SRC :: severity_logger< severity_level> LG;    BOOST_LOG_SEV(LG,调试)LT;< 定期信息;
    返回0;
}


解决方案

荷鲁斯的指出的那样,你可以使用属性日志文件和行号。然而,它是最好避免使用多语句宏以避免与前pressions这样的问题:

 如果(东西)
   LOG_FILE_LINE(调试)LT;< 这是真的; //只有第一条语句是有条件的!

您可以做的更好创建充分利用了Boost库日志的基础行为的宏。例如, BOOST_LOG_SEV 的是:

 的#define BOOST_LOG_SEV(记录仪,LVL)BOOST_LOG_STREAM_SEV(记录仪,LVL)
#定义BOOST_LOG_STREAM_SEV(记录仪,LVL)\\
    BOOST_LOG_STREAM_WITH_PARAMS((记录),(::的boost ::登录::关键词:严重=(LVL)))

使用的 BOOST_LOG_STREAM_WITH_PARAMS 的,你可以设置和获取更多的属性,如:

  //新宏包括严重性,文件名和行号
#定义CUSTOM_LOG(记录仪,SEV)\\
   BOOST_LOG_STREAM_WITH_PARAMS(\\
      (记录),\\
         (set_get_attrib(文件,path_to_filename(__ FILE__)))\\
         (set_get_attrib(线,__LINE__))\\
         (::的boost ::登录::关键词:严重=(升压::登录::琐碎:: SEV))\\
   )//设置属性,并返回新值
模板< typename的值类型>
值类型set_get_attrib(为const char *名称,值类型的值){
   汽车ATTR = logging::attribute_cast<attrs::mutable_constant<ValueType>>(logging::core::get()->get_global_attributes()[name]);
   attr.set(值);
   返回attr.get();
}//转换文件路径只有文件名
标准::字符串path_to_filename(标准::字符串路径){
   返回path.substr(path.find_last_of(/ \\\\)+ 1);
}

完整的源$ C ​​$ c是:

 的#include&LT;升压/日志/ trivial.hpp&GT;
#包括LT&;升压/日志/来源/ severity_logger.hpp&GT;
#包括LT&;升压/日志/工具/设置/ file.hpp&GT;
#包括LT&;升压/日志/工具/设置/ console.hpp&GT;
#包括LT&;升压/日志/ EX pressions.hpp&GT;
#包括LT&;升压/日志/工具/设置/ common_attributes.hpp&GT;
#包括LT&;升压/日志/属性/ mutable_constant.hpp&GT;
#包括LT&;升压/ DATE_TIME /了posix_time / posix_time_types.hpp&GT;
#包括LT&;升压/日志/支持/ date_time.hpp&GT;
#包括LT&;升压/日志/属性/ mutable_constant.hpp&GT;空间日志=提振::登录;
命名空间的attrs =提振::登录::属性;
命名空间EXPR =提振::登录::前pressions;
命名空间SRC =的boost ::登录::来源;
命名空间关键字=提振::登录::关键词;//新宏包括严重性,文件名和行号
#定义CUSTOM_LOG(记录仪,SEV)\\
   BOOST_LOG_STREAM_WITH_PARAMS(\\
      (记录),\\
         (set_get_attrib(文件,path_to_filename(__ FILE__)))\\
         (set_get_attrib(线,__LINE__))\\
         (::的boost ::登录::关键词:严重=(升压::登录::琐碎:: SEV))\\
   )//设置属性,并返回新值
模板&LT; typename的值类型&GT;
值类型set_get_attrib(为const char *名称,值类型的值){
   汽车ATTR = logging::attribute_cast<attrs::mutable_constant<ValueType>>(logging::core::get()->get_global_attributes()[name]);
   attr.set(值);
   返回attr.get();
}//转换文件路径只有文件名
标准::字符串path_to_filename(标准::字符串路径){
   返回path.substr(path.find_last_of(/ \\\\)+ 1);
}无效的init(){
   //新的属性是保持文件名和行号
   记录::核心::得到() - GT; add_global_attribute(文件,ATTRS :: mutable_constant&LT;标准::字符串&GT;());
   记录::核心::得到() - GT; add_global_attribute(线,ATTRS :: mutable_constant&LT; INT&GT;(0));   记录:: add_file_log(
    关键词:FILE_NAME =sample.log
    关键词:格式=(
     EXPR ::流
      &LT;&LT; EXPR :: format_date_time&LT;提高::了posix_time ::分组时间&GT;(时间戳,%Y-%M-%D_%H:%M:%S.%F)
      &LT;&LT; :其中, &LT;&LT;提高::登录::琐碎::严重性和LT;&LT; &gt;中
      &LT;&LT; '['&LT;&LT; EXPR :: ATTR&LT;标准::字符串&GT;(文件)
               &LT;&LT; ':'&LT;&LT; EXPR :: ATTR&LT; INT&GT;(线)LT;&LT; ]
      &LT;&LT; EXPR :: smessage
    )
   );
   测井:: add_common_attributes();
}INT主(INT ARGC,CHAR *的argv []){
   在里面();
   SRC :: severity_logger&LT;记录::琐碎:: severity_level&GT; LG;   CUSTOM_LOG(LG,调试)LT;&LT; 定期信息;
   返回0;
}

这生成一个日志是这样的:

  2015-10-15_15:25:12.743153:其中,调试和GT; [main.cpp中:61]正则消息

I'm using Boost(1.55.0) Logging in my C++ application. I have been able to generate log of this format

[2014-Jul-15 10:47:26.137959]: <debug>  A regular message

I want to be able to add source file name and line number where the log is generated.

[2014-Jul-15 10:47:26.137959]: <debug> [filename:line_no] A regular message

example:

[2014-Jul-15 10:47:26.137959]: <debug> [helloworld.cpp : 12] A regular message

Source Code:

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/attributes/attribute.hpp>
#include <boost/log/attributes/attribute_cast.hpp>
#include <boost/log/attributes/attribute_value.hpp>
#include <boost/make_shared.hpp>
#include <boost/property_tree/ptree.hpp>

namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;

void init()
{
    logging::add_file_log
    (
        keywords::file_name = "sample_%N.log",                                        /*< file name pattern >*/
        keywords::rotation_size = 10*1024*1204,                                 /*< rotate files every 10 MiB... >*/
        keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), /*< ...or at midnight >*/
        keywords::format =
        (
            boost::log::expressions::stream
                << boost::log::expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d_%H:%M:%S.%f")
                << ": <" << boost::log::trivial::severity << "> "
                << boost::log::expressions::smessage
        )
    );
}

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

    using namespace logging::trivial;
    src::severity_logger< severity_level > lg;

    BOOST_LOG_SEV(lg, debug) << "A regular message";
    return 0;
}

解决方案

As Horus pointed out, you can use attributes to log file and line numbers. However it is best to avoid using multi-statements macros to avoid problems with expressions like this:

if (something)
   LOG_FILE_LINE(debug) << "It's true"; // Only the first statement is conditional!

You can do better creating a macro that leverages the underlying behavior of the Boost Log library. For example, BOOST_LOG_SEV is:

#define BOOST_LOG_SEV(logger, lvl) BOOST_LOG_STREAM_SEV(logger, lvl)
#define BOOST_LOG_STREAM_SEV(logger, lvl)\
    BOOST_LOG_STREAM_WITH_PARAMS((logger), (::boost::log::keywords::severity = (lvl)))

Using BOOST_LOG_STREAM_WITH_PARAMS you can set and get more attributes, like this:

// New macro that includes severity, filename and line number
#define CUSTOM_LOG(logger, sev) \
   BOOST_LOG_STREAM_WITH_PARAMS( \
      (logger), \
         (set_get_attrib("File", path_to_filename(__FILE__))) \
         (set_get_attrib("Line", __LINE__)) \
         (::boost::log::keywords::severity = (boost::log::trivial::sev)) \
   )

// Set attribute and return the new value
template<typename ValueType>
ValueType set_get_attrib(const char* name, ValueType value) {
   auto attr = logging::attribute_cast<attrs::mutable_constant<ValueType>>(logging::core::get()->get_global_attributes()[name]);
   attr.set(value);
   return attr.get();
}

// Convert file path to only the filename
std::string path_to_filename(std::string path) {
   return path.substr(path.find_last_of("/\\")+1);
}

The complete source code is:

#include <boost/log/trivial.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/attributes/mutable_constant.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/attributes/mutable_constant.hpp>

namespace logging  = boost::log;
namespace attrs    = boost::log::attributes;
namespace expr     = boost::log::expressions;
namespace src      = boost::log::sources;
namespace keywords = boost::log::keywords;

// New macro that includes severity, filename and line number
#define CUSTOM_LOG(logger, sev) \
   BOOST_LOG_STREAM_WITH_PARAMS( \
      (logger), \
         (set_get_attrib("File", path_to_filename(__FILE__))) \
         (set_get_attrib("Line", __LINE__)) \
         (::boost::log::keywords::severity = (boost::log::trivial::sev)) \
   )

// Set attribute and return the new value
template<typename ValueType>
ValueType set_get_attrib(const char* name, ValueType value) {
   auto attr = logging::attribute_cast<attrs::mutable_constant<ValueType>>(logging::core::get()->get_global_attributes()[name]);
   attr.set(value);
   return attr.get();
}

// Convert file path to only the filename
std::string path_to_filename(std::string path) {
   return path.substr(path.find_last_of("/\\")+1);
}

void init() {
   // New attributes that hold filename and line number
   logging::core::get()->add_global_attribute("File", attrs::mutable_constant<std::string>(""));
   logging::core::get()->add_global_attribute("Line", attrs::mutable_constant<int>(0));

   logging::add_file_log (
    keywords::file_name = "sample.log",
    keywords::format = (
     expr::stream
      << expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d_%H:%M:%S.%f")
      << ": <" << boost::log::trivial::severity << "> "
      << '['   << expr::attr<std::string>("File")
               << ':' << expr::attr<int>("Line") << "] "
      << expr::smessage
    )
   );
   logging::add_common_attributes();
}

int main(int argc, char* argv[]) {
   init();
   src::severity_logger<logging::trivial::severity_level> lg;

   CUSTOM_LOG(lg, debug) << "A regular message";
   return 0;
}

This generate a log like this:

2015-10-15_15:25:12.743153: <debug> [main.cpp:61] A regular message

这篇关于助推日志打印源$ C ​​$ C文件名和行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆