本文介绍了将文件,功能,行添加到全局Boost记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有有效的Boost日志代码,但想将File,Function和Line附加到每个日志调用中.这是我的代码:

I have working boost log code, but want to append File, Function, and Line to every log call. Here is my code:

logging.hpp

logging.hpp

#ifndef LOGGING_HPP
#define LOGGING_HPP

#include <boost/log/expressions.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup.hpp>

#define INFO  BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::info)
#define WARN  BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::warning)
#define ERROR BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::error)

#define SYS_LOGFILE "/var/www/html/logs/pal5.log"

//Narrow-char thread-safe logger.
typedef boost::log::sources::severity_logger_mt<boost::log::trivial::severity_level> logger_t;

//declares a global logger with a custom initialization
BOOST_LOG_GLOBAL_LOGGER(my_logger, logger_t)

#endif

logging.cpp

logging.cpp

#include "../include/logging.hpp"

// CFLAGS += -DBOOST_LOG_DYN_LINK
// LIBS += -lboost_log_setup -lboost_log -lpthread

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

//Defines a global logger initialization routine
BOOST_LOG_GLOBAL_LOGGER_INIT(my_logger, logger_t)
{
    logger_t lg;

    logging::add_common_attributes();

    logging::add_file_log(
            keywords::file_name = SYS_LOGFILE,
            keywords::rotation_size = 1024 * 1024 * 20,    // megabytes
            keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point (0, 0, 0),
            keywords::auto_flush = true,
            keywords::format = (
                    expr::stream << expr::format_date_time <boost::posix_time::ptime> ("TimeStamp", "%y-%m-%d %H:%M:%S.%f")
                    << " [" << expr::attr <boost::log::trivial::severity_level> ("Severity") << "]: "
                    << expr::smessage
            )
    );

    logging::add_console_log(
            std::cout,
            keywords::format = (
                    expr::stream << expr::format_date_time <boost::posix_time::ptime> ("TimeStamp", "%y-%m-%d %H:%M:%S.%f")
                    << " [" << expr::attr <boost::log::trivial::severity_level> ("Severity") << "]: "
                    << expr::smessage
            )
    );

    logging::core::get()->set_filter
    (
        logging::trivial::severity >= logging::trivial::info
    );

    return lg;
}

这是我目前的称呼方式:

Here is how I currently call it:

INFO << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | started";

是否可以使用全局定义自动执行此操作?谢谢你.

Is it possible to automate this with global defines? Thank you.

推荐答案

只需将宏定义为

#define INFO  BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::info) << __FILE__ << "(" ..

这篇关于将文件,功能,行添加到全局Boost记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:03