2016-03-01 28 views
0

我有工作提升日志代码,但要将文件,函数和线追加到每个日志调用。这里是我的代码:将文件,函数,行添加到全局提升记录器

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

#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; 
} 

这里是我当前如何称呼它:

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

是否有可能自动执行此全球定义?谢谢。

回答

2

只要定义您的宏作为

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

哎呦,我试过了,但我想我没有与头部正确编译;谢谢 – xinthose