未创建日志文件
Log file is not getting created
我需要一个全局记录器,我可以在项目中的多个类中使用。我想在 ini 文件中设置记录器。
我从这里开始遵循示例,并将其与我之前的问题中的一些建议结合起来。
我的记录器如下。
#pragma once
#include <boost/log/common.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/utility/setup/from_stream.hpp>
#include <boost/regex.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)
//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)
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg, boost::log::sources::severity_logger< >)
记录器.cpp如下所示。
#include "stdafx.h"
#include "Logger.h"
#include <fstream>
namespace attrs = boost::log::attributes;
namespace expr = boost::log::expressions;
namespace logging = boost::log;
//Defines a global logger initialization routine
BOOST_LOG_GLOBAL_LOGGER_INIT(my_logger, logger_t)
{
logger_t lg;
logging::add_common_attributes();
std::ifstream settings("settings.ini");
if (!settings.is_open())
{
std::cout << "Could not open settings.txt file" << std::endl;
//return 1;
}
// Read the settings and initialize logging library
logging::init_from_stream(settings);
// Add some attributes
logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
logging::register_simple_filter_factory<logging::trivial::severity_level>("Severity");
return lg;
}
设置.ini如下所示
#
# Copyright Andrey Semashev 2007 - 2014.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
#
[Core]
Filter="%Severity% >= debug"
[Sinks.1]
Destination=TextFile
FileName=test.log
AutoFlush=true
Format="[%TimeStamp%] <%Severity%> (%Channel%): %Message%"
最后,我正在尝试使用记录器,如下所示。
#include "Logger.h"
int wmain(int argc, wchar_t* argv[])
{
INFO << "Program started";
}
但是,不会创建任何日志文件。我现在做错了什么?
在解析设置 ini之前,需要注册过滤器工厂。
// Add some attributes
logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
logging::register_simple_filter_factory<logging::trivial::severity_level>("Severity");
需要在通话前
// Read the settings and initialize logging library
logging::init_from_stream(settings);
如果没有工厂,过滤器不会被解释并最终过滤所有内容。
相关文章:
- 加快在C++中读取/处理日志文件的速度
- 如何将消息时间戳写入日志文件?
- 分析包含 NMEA 句子的日志文件C++
- 如何从日志文件中抓取状态代码?(在 C++ 中)
- 未创建日志文件
- 尝试使用 EvtSetChannelConfigProperty() 函数更新最大事件日志文件大小时插入的错误值
- 提升日志:文件轮换
- POSIX C/C++日志文件(VEX V5 Brain)
- boost日志文件无法创建sample.log文件
- 哪个更适合从C++写入敏感的日志文件,在文件描述符上写()或文件上的fprintf()?
- 如何编写日志文件,以便可以使用记事本实时读取它以进行C++
- 如何在QT中使用DEBUG对齐日志文件中的数据?
- BOOST ::日志是每次运行应用程序时都可能旋转文件
- Qt - 如何在 30 天后删除日志文件,文件名不固定
- Mac OS X:应用程序可以创建日志文件
- 将断言消息写入日志文件
- 独立 exe:磁盘上保存配置文件和日志的位置
- 检查日志文件以找出 vcredist.exe 安装失败的原因
- C++类似cout的函数,将数据写入文件(日志记录)
- 高效的文件日志记录方式