我正在编写将python应用程序转换为c ++的代码 我现在正在进行伐木 我使用升压日志..但我发现一些事情很难理解 我试图为我的代码的每个部分制作单独的文件,以便主要功能保持清洁。
当我使用提升日志执行此操作时,每件事情都运行良好,但定义了
"或声明或初始化......这三个单词很容易用内置类型理解,但很难知道在使用库类时它意味着什么...或者可能是我的大脑不适合学习编程和#34;
在主文件&#34中没有看到记录器文件中定义记录器;虽然当我在主文件中定义记录器但是其他组件(如接收器和主文件外部的核心)一切顺利时
我想知道为什么我必须在main中定义记录器,尽管每个其他组件都是在main之外定义的 这有什么事情与新的"我真的试图避免记忆安排,因为我非常害怕被困在那里"
我正在编辑问题以更清楚地显示问题
在c ++ 2.cpp中我使函数init_logger()返回void
在这个函数中我定义了接收器,核心,属性,并且它们在从函数返回后仍然存在"为什么它们仍然存在?"
/*
src::channel_logger<std::string> lg_chan_0(keywords::channel = "0");
src::channel_logger<std::string> lg_chan_1(keywords::channel = "1");
src::channel_logger<std::string> lg_chan_2(keywords::channel = "2");
src::channel_logger<std::string> lg_chan_3(keywords::channel = "3");
src::channel_logger<std::string> lg_chan_4(keywords::channel = "4");
src::channel_logger<std::string> lg_chan_5(keywords::channel = "5");
src::channel_logger<std::string> lg_chan_6(keywords::channel = "6");
*/
但是当我在init_function()中定义记录器时,它们在返回后不会持久存在并且编译器告诉我它们是未定义的 为什么核心和接收器在返回后仍然存在,并且可以被c ++ 2.cpp中定义的logmacros使用,但是记录器不会持久存在?
// c++2.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "program_options_1.h"
#include "utils_logger.h"
int main(int argc, char* argv[])
{
//1 init program_options and configure it
po::variables_map vm =init_program_options(argc, argv);
//2 init logger and configure it
init_logging();
//3
src::channel_logger<std::string> lg_chan_0(keywords::channel = "0");
src::channel_logger<std::string> lg_chan_1(keywords::channel = "1");
src::channel_logger<std::string> lg_chan_2(keywords::channel = "2");
src::channel_logger<std::string> lg_chan_3(keywords::channel = "3");
src::channel_logger<std::string> lg_chan_4(keywords::channel = "4");
src::channel_logger<std::string> lg_chan_5(keywords::channel = "5");
src::channel_logger<std::string> lg_chan_6(keywords::channel = "6");
BOOST_LOG(lg_chan_0) << "Hello world0!";
BOOST_LOG(lg_chan_1) << "Hello world1!";
BOOST_LOG(lg_chan_2) << "Hello world2!";
BOOST_LOG(lg_chan_3) << "Hello world3!";
BOOST_LOG(lg_chan_4) << "Hello world4!";
BOOST_LOG(lg_chan_5) << "Hello world5!";
BOOST_LOG(lg_chan_6) << "Hello world6!";
return 0;
}
这是代码:
#pragma once
/*
* Copyright Andrey Semashev 2007 - 2015.
* 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)
*/
#include <stdexcept>
#include <string>
#include <iostream>
#include <fstream>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/core/null_deleter.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/log/attributes/current_thread_id.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/expressions/formatters/format.hpp>
#include <boost/log/core.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/common.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/sources/logger.hpp>
namespace logging = boost::log;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;
/*
1-first date with 404
2-first date with 200
3-dates with zero size after first date with 200
4-dates of saturday and sunday after first date with 200
5-dates not saturday or sunday after first date with 200 and having zero size
6-dates not saturday or sunday after first date with 200 and having 400
7-last date recieved and written to file
*/
//make attribute argument
BOOST_LOG_ATTRIBUTE_KEYWORD(a_channel, "Channel", std::string)//????????????
// Complete sink type
typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink;
//[ example_sinks_xml_file_collecting
void init_file_collecting(boost::shared_ptr< file_sink > sink)
{
sink->locked_backend()->set_file_collector(sinks::file::make_collector(
keywords::target = "logs", /*< the target directory >*/
keywords::max_size = 16 * 1024 * 1024, /*< maximum total size of the stored files, in bytes >*/
keywords::min_free_space = 100 * 1024 * 1024, /*< minimum free space on the drive, in bytes >*/
keywords::max_files = 7 /*< maximum number of stored files >*/
));
}
//]
#if 0
//[ example_sinks_xml_file
// Complete file sink type
typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink;
void write_header(sinks::text_file_backend::stream_type& file)
{
file << "<?xml version=\"1.0\"?>\n<log>\n";
}
void write_footer(sinks::text_file_backend::stream_type& file)
{
file << "</log>\n";
}
void init_logging()
{
// Create a text file sink
boost::shared_ptr< file_sink > sink(new file_sink(
keywords::file_name = "%Y%m%d_%H%M%S_%5N.xml", /*< the resulting file name pattern >*/
keywords::rotation_size = 16384 /*< rotation size, in characters >*/
));
sink->set_formatter
(
expr::format("\t<record id=\"%1%\" timestamp=\"%2%\">%3%</record>")
% expr::attr< unsigned int >("RecordID")
% expr::attr< boost::posix_time::ptime >("TimeStamp")
% expr::xml_decor[expr::stream << expr::smessage] /*< the log message has to be decorated, if it contains special characters >*/
);
// Set header and footer writing functors
sink->locked_backend()->set_open_handler(&write_header);
sink->locked_backend()->set_close_handler(&write_footer);
// Add the sink to the core
logging::core::get()->add_sink(sink);
}
//]
#endif
//[ example_sinks_xml_file_final
void init_logging()
{
boost::shared_ptr< logging::core > core = logging::core::get();
/*
src::channel_logger<std::string> lg_chan_0(keywords::channel = "0");
src::channel_logger<std::string> lg_chan_1(keywords::channel = "1");
src::channel_logger<std::string> lg_chan_2(keywords::channel = "2");
src::channel_logger<std::string> lg_chan_3(keywords::channel = "3");
src::channel_logger<std::string> lg_chan_4(keywords::channel = "4");
src::channel_logger<std::string> lg_chan_5(keywords::channel = "5");
src::channel_logger<std::string> lg_chan_6(keywords::channel = "6");
*/
core->add_global_attribute("ThreadID", boost::log::attributes::current_thread_id());
core->add_global_attribute("TimeStamp",attrs::local_clock());
core->add_global_attribute("RecordID", attrs::counter< unsigned int >(1));
logging::add_common_attributes();
//lg_chan_0(keywords::channel = "net");
// Create a text file sink
boost::shared_ptr< file_sink > sink0(new file_sink(
keywords::file_name = "first_date_with_404.log",
keywords::rotation_size = 16384
));
// Create a text file sink
boost::shared_ptr< file_sink > sink1(new file_sink(
keywords::file_name = "first_date_with_200.log",
keywords::rotation_size = 16384
));
// Create a text file sink
boost::shared_ptr< file_sink > sink2(new file_sink(
keywords::file_name = "dates_with_zero_size_after_first_date_with_200.log",
keywords::rotation_size = 16384
));
// Create a text file sink
boost::shared_ptr< file_sink > sink3(new file_sink(
keywords::file_name = "dates_of_saturday_and_sunday_after_first_date_with_200.log",
keywords::rotation_size = 16384
));
// Create a text file sink
boost::shared_ptr< file_sink > sink4(new file_sink(
keywords::file_name = "dates_not_saturday_or_sunday_after_first_date_with_200_having_zero_size.log",
keywords::rotation_size = 16384
));
// Create a text file sink
boost::shared_ptr< file_sink > sink5(new file_sink(
keywords::file_name = "dates_not_saturday_or_sunday_after_first_date_with_200_having_400.log",
keywords::rotation_size = 16384
));
// Create a text file sink
boost::shared_ptr< file_sink > sink6(new file_sink(
keywords::file_name = "last_date_recieved_and_written_to_file.log",
keywords::rotation_size = 16384
));
/*
// Create a text file sink
boost::shared_ptr< file_sink > sink(new file_sink(
keywords::file_name = "%Y%m%d_%H%M%S_%5N.xml",
keywords::rotation_size = 16384
));
*/
sink0->set_filter(expr::attr< std::string >("Channel") == "0");
sink1->set_filter(expr::attr< std::string >("Channel") == "1");
sink2->set_filter(expr::attr< std::string >("Channel") == "2");
sink3->set_filter(expr::attr< std::string >("Channel") == "3");
sink4->set_filter(expr::attr< std::string >("Channel") == "4");
sink5->set_filter(expr::attr< std::string >("Channel") == "5");
sink6->set_filter(expr::attr< std::string >("Channel") == "6");
// Set up where the rotated files will be stored
init_file_collecting(sink0);
init_file_collecting(sink1);
init_file_collecting(sink2);
init_file_collecting(sink3);
init_file_collecting(sink4);
init_file_collecting(sink5);
init_file_collecting(sink6);
// Upon restart, scan the directory for files matching the file_name pattern
sink0->locked_backend()->scan_for_files();
sink1->locked_backend()->scan_for_files();
sink2->locked_backend()->scan_for_files();
sink3->locked_backend()->scan_for_files();
sink4->locked_backend()->scan_for_files();
sink5->locked_backend()->scan_for_files();
sink6->locked_backend()->scan_for_files();
sink0->set_formatter
(
expr::format("record id=\"%1%\" [\"%2%\"] thread_id= %3% %4%")
% expr::attr< unsigned int >("RecordID")
% expr::attr< boost::posix_time::ptime >("TimeStamp")
% expr::attr< attrs::current_thread_id::value_type >("ThreadID")
% expr::message
);
sink1->set_formatter
(
expr::format("record id=\"%1%\" [\"%2%\"] thread_id= %3% %4%")
% expr::attr< unsigned int >("RecordID")
% expr::attr< boost::posix_time::ptime >("TimeStamp")
% expr::attr< attrs::current_thread_id::value_type >("ThreadID")
% expr::message
);
sink2->set_formatter
(
expr::format("record id=\"%1%\" [\"%2%\"] thread_id= %3% %4%")
% expr::attr< unsigned int >("RecordID")
% expr::attr< boost::posix_time::ptime >("TimeStamp")
% expr::attr< attrs::current_thread_id::value_type >("ThreadID")
% expr::message
);
sink3->set_formatter
(
expr::format("record id=\"%1%\" [\"%2%\"] thread_id= %3% %4%")
% expr::attr< unsigned int >("RecordID")
% expr::attr< boost::posix_time::ptime >("TimeStamp")
% expr::attr< attrs::current_thread_id::value_type >("ThreadID")
% expr::message
);
sink4->set_formatter
(
expr::format("record id=\"%1%\" [\"%2%\"] thread_id= %3% %4%")
% expr::attr< unsigned int >("RecordID")
% expr::attr< boost::posix_time::ptime >("TimeStamp")
% expr::attr< attrs::current_thread_id::value_type >("ThreadID")
% expr::message
);
sink5->set_formatter
(
expr::format("record id=\"%1%\" [\"%2%\"] thread_id= %3% %4%")
% expr::attr< unsigned int >("RecordID")
% expr::attr< boost::posix_time::ptime >("TimeStamp")
% expr::attr< attrs::current_thread_id::value_type >("ThreadID")
% expr::message
);
sink6->set_formatter
(
expr::format("record id=\"%1%\" [\"%2%\"] thread_id= %3% %4%")
% expr::attr< unsigned int >("RecordID")
% expr::attr< boost::posix_time::ptime >("TimeStamp")
% expr::attr< attrs::current_thread_id::value_type >("ThreadID")
% expr::message
);
/*
// Set header and footer writing functors
namespace bll = boost::lambda;
sink->locked_backend()->set_open_handler
(
bll::_1 << "<?xml version=\"1.0\"?>\n<log>\n"
);
sink->locked_backend()->set_close_handler
(
bll::_1 << "</log>\n"
);
*/
// Add the sink to the core
//logging::core::get()->add_sink(sink);
core->add_sink(sink0);
core->add_sink(sink1);
core->add_sink(sink2);
core->add_sink(sink3);
core->add_sink(sink4);
core->add_sink(sink5);
core->add_sink(sink6);
}
//]
//enum { LOG_RECORDS_TO_WRITE = 2000 };
/*
int main(int argc, char* argv[])
{
try
{
// Initialize logging library
init_logging();
// And also add some attributes
logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());
// Do some logging
src::logger lg;
for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i)
{
BOOST_LOG(lg) << "XML log record " << i;
}
// Test that XML character decoration works
BOOST_LOG(lg) << "Special XML characters: &, <, >, \", '";
return 0;
}
catch (std::exception& e)
{
std::cout << "FAILURE: " << e.what() << std::endl;
return 1;
}
}
*/
/*
* Copyright Andrey Semashev 2007 - 2015.
* 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)
*/
/*
#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>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
namespace expr = boost::log::expressions;
namespace attrs = boost::log::attributes;
#if 0
//[ example_tutorial_file_simple
void init()
{
logging::add_file_log("sample.log");
logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);
}
//]
// We need this due to this bug: https://svn.boost.org/trac/boost/ticket/4416
//[ example_tutorial_file_advanced_no_callouts
void init()
{
logging::add_file_log
(
keywords::file_name = "sample_%N.log",
keywords::rotation_size = 10 * 1024 * 1024,
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
keywords::format = "[%TimeStamp%]: %Message%"
);
logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);
}
//]
#else
//[ example_tutorial_file_advanced
void init()
{
logging::add_file_log
(
keywords::file_name = "sample_%N.log", //*< file name pattern >
keywords::rotation_size = 10 * 1024 * 1024, //*< rotate files every 10 MiB... >
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), //*< ...or at midnight >
keywords::format = "[%TimeStamp%]: %Message%" //*< log record format >
);
logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);
}
//]
#endif
void init_logging()
{
// Create a text file sink
boost::shared_ptr< file_sink > sink(new file_sink(
keywords::file_name = "%Y%m%d_%H%M%S_%5N.xml",
keywords::rotation_size = 16384
));
// Set up where the rotated files will be stored
init_file_collecting(sink);
// Upon restart, scan the directory for files matching the file_name pattern
sink->locked_backend()->scan_for_files();
sink->set_formatter
(
expr::format("\t<record id=\"%1%\" timestamp=\"%2%\">%3%</record>")
% expr::attr< unsigned int >("RecordID")
% expr::attr< boost::posix_time::ptime >("TimeStamp")
% expr::xml_decor[expr::stream << expr::smessage]
);
// Set header and footer writing functors
namespace bll = boost::lambda;
sink->locked_backend()->set_open_handler
(
bll::_1 << "<?xml version=\"1.0\"?>\n<log>\n"
);
sink->locked_backend()->set_close_handler
(
bll::_1 << "</log>\n"
);
// Add the sink to the core
logging::core::get()->add_sink(sink);
}
*/
#pragma once
//#include "stdafx.h"
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/date_time/gregorian/gregorian.hpp" //include all types plus i/o
#include <boost/program_options.hpp>
#include <boost/any.hpp>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include "utils.h"
namespace po = boost::program_options;
using namespace std;
using namespace boost::posix_time;
using namespace boost::gregorian;
po::variables_map init_program_options(int argc, char* argv[])
{
const string VERSION = "0.2.1";
try {
date_duration single_day(1);
auto facet = new date_input_facet();
facet->set_iso_extended_format();
//facet->format("%Y-%m-%d");
std::locale::global(std::locale(std::locale(), facet));
std::vector<symbols_enum> symbols_default = { symbols_enum::GBPJPY};
string prog = "DUKA";
po::options_description desc(prog + " Allowed options");
desc.add_options()
("help", "produce help message")
("version,v", "outputs Version of program")
("day,d", po::value<date>()->default_value(day_clock::universal_day()), "specific day format YYYY-MM-DD (default today)")
("startdate,s", po::value<date>()->default_value(day_clock::universal_day() - single_day), "start date format YYYY-MM-DD (default today)")
("enddate,e", po::value<date>()->default_value(day_clock::universal_day()), "end date format YYYY-MM-DD (default today)")
("thread,t", po::value<int>()->default_value(20), "number of threads (default 20)")
("folder,f", po::value<string>()->default_value("."), "destination folder (default .)")
("candle,c", po::value<timeframe_enum>()->default_value(timeframe_enum::TICK, "TICK"), "use candles instead of ticks. Accepted values TICK M1 M2 M5 M10 M15 M30 H1 H4")
("header", po::value<bool>()->default_value(false), "include CSV header (default false)")
("symbols", po::value<std::vector<symbols_enum>>()->multitoken()->composing()->default_value(symbols_default, "GBPJPY"), "symbol list using format EURUSD EURGBP")
;
po::positional_options_description pd;
pd.add("symbols", -1);
command_line_parser parser{ argc, argv };
parser.options(desc).positional(pd);
parsed_options parsed_options = parser.run();
po::variables_map vm;
po::store(parsed_options, vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
exit(0);
}
if (vm.count("version")) {
cout << VERSION << "\n";
exit(0);
}
if (vm.count("day")) {
cout << "day set to "
<< vm["day"].as<date>() << ".\n";
}
else {
cout << "day was not set.\n";
}
if (vm.count("startdate")) {
cout << "startdate set to "
<< vm["startdate"].as<date>() << ".\n";
}
else {
cout << "startdate was not set.\n";
}
if (vm.count("enddate")) {
cout << "enddate set to "
<< vm["enddate"].as<date>() << ".\n";
}
else {
cout << "enddate was not set.\n";
}
if (vm.count("thread")) {
cout << "thread set to "
<< vm["thread"].as<int>() << ".\n";
}
else {
cout << "thread was not set.\n";
}
if (vm.count("folder")) {
cout << "folder set to "
<< vm["folder"].as<string>() << ".\n";
}
else {
cout << "folder was not set.\n";
}
if (vm.count("candle")) {
cout << "candle set to "
<< vm["candle"].as<timeframe_enum>() << ".\n";
}
else {
cout << "candle level was not set.\n";
}
if (vm.count("header")) {
cout << "header set to "
<< vm["header"].as<bool>() << ".\n";
}
else {
cout << "header was not set.\n";
}
if (vm.count("symbols")) {
cout << "symbols set to "
<< vm["symbols"].as<std::vector<symbols_enum>>() << ".\n";
}
else {
cout << "symbols was not set.\n";
}
return vm;
}
catch (exception& e) {
cerr << "error: " << e.what() << "\n";
exit(1);
}
catch (...) {
cerr << "Exception of unknown type!\n";
}
}
{{1}}
答案 0 :(得分:1)
为什么核心和接收器在返回后仍然存在,并且可以被c ++ 2.cpp中定义的logmacros使用但是记录器不会持续存在?
记录核心是由Boost.Log在内部创建和维护的单例。当您第一次调用logging::core::get()
时,库会创建核心对象并返回指向它的指针。后续调用返回指向同一对象的指针。在应用程序终止之前,单例不会被销毁。全局和特定于线程的属性由核心管理。
接收器已在核心中注册,因此它们也会存在,直到应用程序终止或从核心移除它们为止。
记录器未连接到核心,它们是应用程序根据需要创建的单独对象。记录器使用日志记录核心来生成日志记录。
记录宏不直接使用核心或接收器。所有日志记录都通过记录器发出,记录器提供特定于源的属性。
您可以从docs了解有关Boost.Log如何运作的更多信息。