我需要一个Boost记录器,该记录器使用以下参数登录控制台(并稍后登录到文件): “ [[%TimeStamp%] [%Severity%] [%File%(%Line%)]%Message%”。 我已经阅读了Boost.Log v2文档,并从其他地方获得了一些“灵感”,但是我真的无法使它正常工作。
'.InlineShapes.AddPicture(filePath, Type.Missing, Type.Missing, Type.Missing)
'Dim objRng As Word.Range = .Range()
'objRng.InlineShapes.AddPicture(filePath)
'Dim objInlineShape As Word.InlineShape = objWordApp.Selection.InlineShapes.AddPicture(
' FileName:=filePath,
' LinkToFile:=False,
' SaveWithDocument:=True
')
'objInlineShape.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue
'objInlineShape.Width = imgX
'objInlineShape.Height = imgY
'.Application.Selection.InlineShapes.AddPicture(filePath)
'Dim objRng As Word.Range = .Sections(1).Range()
'objRng.InlineShapes.AddPicture(filePath)
'
'Dim objInlineShape As Word.InlineShape = .InlineShapes.AddPicture(filePath)
'Dim objShape As Word.Shape = objInlineShape.ConvertToShape()
'objDoc.Bookmarks.Item("\startofdoc").Range.InlineShapes.AddPicture(filePath)
和源文件:
// cswlogger.h
#pragma once
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/attributes/mutable_constant.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/utility/manipulators/add_value.hpp>
BOOST_LOG_GLOBAL_LOGGER(sysLogger,
boost::log::sources::severity_logger_mt<boost::log::trivial::severity_level>);
class CswLogger
{
public:
/// Init with default info level logging
static void init(boost::log::trivial::severity_level level = boost::log::trivial::info);
/// Disable logging
static void disable();
};
#define LOG_LOG_LOCATION(LOGGER, LEVEL, ARG) \
BOOST_LOG_SEV(LOGGER, boost::log::trivial::LEVEL) \
<< boost::log::add_value("Line", __LINE__) \
<< boost::log::add_value("File", __FILE__) << ARG
/// System Log macros.
/// TRACE < DEBUG < INFO < WARN < ERROR < FATAL
#define LOG_TRACE(ARG) LOG_LOG_LOCATION(sysLogger::get(), trace, ARG);
#define LOG_DEBUG(ARG) LOG_LOG_LOCATION(sysLogger::get(), debug, ARG);
#define LOG_INFO(ARG) LOG_LOG_LOCATION(sysLogger::get(), info, ARG);
#define LOG_WARN(ARG) LOG_LOG_LOCATION(sysLogger::get(), warning, ARG);
#define LOG_ERROR(ARG) LOG_LOG_LOCATION(sysLogger::get(), error, ARG);
#define LOG_FATAL(ARG) LOG_LOG_LOCATION(sysLogger::get(), fatal, ARG);
main.cpp
// cswlogger.cpp
#include "cswlogger.h"
#include <boost/log/core.hpp>
#include <boost/log/common.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/settings.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
BOOST_LOG_GLOBAL_LOGGER_DEFAULT(sysLogger,
boost::log::sources::severity_channel_logger_mt<boost::log::trivial::severity_level>);
void CswLogger::init(boost::log::trivial::severity_level level)
{
boost::log::add_console_log
(
std::clog,
boost::log::keywords::format = "[%TimeStamp%] [%Severity%] [%File%(%Line%)] %Message%"
);
boost::log::core::get()->set_filter
(
boost::log::trivial::severity >= level
);
// Indicate start of logging
LOG_INFO("Log Start");
}
void CswLogger::disable()
{
boost::log::core::get()->set_logging_enabled(false);
}
其输出将是:“ [] [] main.cpp(3)这是一条信息跟踪”
缺少时间戳和严重性参数。 是否可以使用“ BOOST_LOG_SEV”日志宏并添加自定义日志参数,还是需要使用其他方法?
答案 0 :(得分:1)
首先,输出中缺少TimeStamp
属性,因为您尚未将其添加到日志记录核心。您可以按照here的说明添加它:
boost::log::core::get()->add_global_attribute(
"TimeStamp",
boost::log::attributes::local_clock());
或者您可以通过调用add_common_attributes
将其添加为常用属性之一。
接下来,Severity
属性存在,由记录器提供。但是,默认情况下,从字符串解析的过滤器和格式化程序不支持该属性的值类型(即boost::log::trivial::severity_level
)。您可以通过两种方式解决此问题。
首先,您可以切换到日志接收器的手动设置,这将允许您将接收器的过滤器和格式化程序设置为lambda表达式。这样,您将能够向格式化程序提供属性类型。例如:
BOOST_LOG_ATTRIBUTE_KEYWORD(a_timestamp, "TimeStamp", boost::log::attributes::local_clock::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(a_severity, "Severity", boost::log::trivial::severity_level)
BOOST_LOG_ATTRIBUTE_KEYWORD(a_file, "File", std::string)
BOOST_LOG_ATTRIBUTE_KEYWORD(a_line, "Line", int)
typedef boost::log::sinks::synchronous_sink< boost::log::sinks::text_ostream_backend > sink_t;
auto sink = boost::make_shared< sink_t >();
sink->set_formatter(boost::log::expressions::ostream
<< "[" << a_timestamp << "] "
<< "[" << a_severity << "] "
<< "[" << a_file << "(" << a_line << ")] "
<< boost::log::expressions::message);
boost::log::core::get()->add_sink(sink);
或者,您可以为Severity
属性注册格式化程序和过滤器工厂,以便格式化程序和过滤器解析器能够将该属性与boost::log::trivial::severity_level
类型相关联。 here对此进行了详细说明。就您而言,您可以在解析所有格式器之前(即在add_console_log
调用之前)添加此调用:
boost::log::register_simple_formatter_factory<
boost::log::trivial::severity_level, char >("Severity");