Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.78 KB | None | 0 0
  1. // c++2.cpp : Defines the entry point for the console application.
  2.  
  3.  
  4. #include "stdafx.h"
  5. #include "program_options_1.h"
  6. #include "utils_logger.h"
  7.  
  8.  
  9. int main(int argc, char* argv[])
  10. {
  11. //1 init program_options and configure it
  12. po::variables_map vm =init_program_options(argc, argv);
  13. //2 init logger and configure it
  14. init_logging();
  15. //3
  16.  
  17. src::channel_logger<std::string> lg_chan_0(keywords::channel = "0");
  18. src::channel_logger<std::string> lg_chan_1(keywords::channel = "1");
  19. src::channel_logger<std::string> lg_chan_2(keywords::channel = "2");
  20. src::channel_logger<std::string> lg_chan_3(keywords::channel = "3");
  21. src::channel_logger<std::string> lg_chan_4(keywords::channel = "4");
  22. src::channel_logger<std::string> lg_chan_5(keywords::channel = "5");
  23. src::channel_logger<std::string> lg_chan_6(keywords::channel = "6");
  24.  
  25. BOOST_LOG(lg_chan_0) << "Hello world0!";
  26. BOOST_LOG(lg_chan_1) << "Hello world1!";
  27. BOOST_LOG(lg_chan_2) << "Hello world2!";
  28. BOOST_LOG(lg_chan_3) << "Hello world3!";
  29. BOOST_LOG(lg_chan_4) << "Hello world4!";
  30. BOOST_LOG(lg_chan_5) << "Hello world5!";
  31. BOOST_LOG(lg_chan_6) << "Hello world6!";
  32. return 0;
  33. }
  34.  
  35. #pragma once
  36. /*
  37. * Copyright Andrey Semashev 2007 - 2015.
  38. * Distributed under the Boost Software License, Version 1.0.
  39. * (See accompanying file LICENSE_1_0.txt or copy at
  40. * http://www.boost.org/LICENSE_1_0.txt)
  41. */
  42.  
  43. #include <stdexcept>
  44. #include <string>
  45. #include <iostream>
  46. #include <fstream>
  47.  
  48. #include <boost/smart_ptr/shared_ptr.hpp>
  49.  
  50. #include <boost/core/null_deleter.hpp>
  51.  
  52. #include <boost/lambda/lambda.hpp>
  53. #include <boost/date_time/posix_time/posix_time.hpp>
  54. #include <boost/log/attributes/current_thread_id.hpp>
  55. #include <boost/log/utility/setup/common_attributes.hpp>
  56. #include <boost/log/expressions/formatters/format.hpp>
  57.  
  58. #include <boost/log/core.hpp>
  59. #include <boost/log/sinks/sync_frontend.hpp>
  60. #include <boost/log/sinks/text_ostream_backend.hpp>
  61. #include <boost/log/sources/severity_channel_logger.hpp>
  62. #include <boost/log/sources/record_ostream.hpp>
  63.  
  64. #include <boost/log/common.hpp>
  65. #include <boost/log/expressions.hpp>
  66. #include <boost/log/attributes.hpp>
  67. #include <boost/log/sinks.hpp>
  68. #include <boost/log/sources/logger.hpp>
  69.  
  70. namespace logging = boost::log;
  71. namespace attrs = boost::log::attributes;
  72. namespace src = boost::log::sources;
  73. namespace sinks = boost::log::sinks;
  74. namespace expr = boost::log::expressions;
  75. namespace keywords = boost::log::keywords;
  76. /*
  77. 1-first date with 404
  78. 2-first date with 200
  79. 3-dates with zero size after first date with 200
  80. 4-dates of saturday and sunday after first date with 200
  81. 5-dates not saturday or sunday after first date with 200 and having zero size
  82. 6-dates not saturday or sunday after first date with 200 and having 400
  83. 7-last date recieved and written to file
  84.  
  85. */
  86. //make attribute argument
  87. BOOST_LOG_ATTRIBUTE_KEYWORD(a_channel, "Channel", std::string)//????????????
  88.  
  89. // Complete sink type
  90. typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink;
  91.  
  92. //[ example_sinks_xml_file_collecting
  93. void init_file_collecting(boost::shared_ptr< file_sink > sink)
  94. {
  95. sink->locked_backend()->set_file_collector(sinks::file::make_collector(
  96. keywords::target = "logs", /*< the target directory >*/
  97. keywords::max_size = 16 * 1024 * 1024, /*< maximum total size of the stored files, in bytes >*/
  98. keywords::min_free_space = 100 * 1024 * 1024, /*< minimum free space on the drive, in bytes >*/
  99. keywords::max_files = 7 /*< maximum number of stored files >*/
  100. ));
  101. }
  102. //]
  103.  
  104. #if 0
  105. //[ example_sinks_xml_file
  106. // Complete file sink type
  107. typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink;
  108.  
  109. void write_header(sinks::text_file_backend::stream_type& file)
  110. {
  111. file << "<?xml version="1.0"?>n<log>n";
  112. }
  113.  
  114. void write_footer(sinks::text_file_backend::stream_type& file)
  115. {
  116. file << "</log>n";
  117. }
  118.  
  119. void init_logging()
  120. {
  121. // Create a text file sink
  122. boost::shared_ptr< file_sink > sink(new file_sink(
  123. keywords::file_name = "%Y%m%d_%H%M%S_%5N.xml", /*< the resulting file name pattern >*/
  124. keywords::rotation_size = 16384 /*< rotation size, in characters >*/
  125. ));
  126.  
  127. sink->set_formatter
  128. (
  129. expr::format("t<record id="%1%" timestamp="%2%">%3%</record>")
  130. % expr::attr< unsigned int >("RecordID")
  131. % expr::attr< boost::posix_time::ptime >("TimeStamp")
  132. % expr::xml_decor[expr::stream << expr::smessage] /*< the log message has to be decorated, if it contains special characters >*/
  133. );
  134.  
  135. // Set header and footer writing functors
  136. sink->locked_backend()->set_open_handler(&write_header);
  137. sink->locked_backend()->set_close_handler(&write_footer);
  138.  
  139. // Add the sink to the core
  140. logging::core::get()->add_sink(sink);
  141. }
  142. //]
  143. #endif
  144.  
  145. //[ example_sinks_xml_file_final
  146. void init_logging()
  147. {
  148. boost::shared_ptr< logging::core > core = logging::core::get();
  149. /*
  150. src::channel_logger<std::string> lg_chan_0(keywords::channel = "0");
  151. src::channel_logger<std::string> lg_chan_1(keywords::channel = "1");
  152. src::channel_logger<std::string> lg_chan_2(keywords::channel = "2");
  153. src::channel_logger<std::string> lg_chan_3(keywords::channel = "3");
  154. src::channel_logger<std::string> lg_chan_4(keywords::channel = "4");
  155. src::channel_logger<std::string> lg_chan_5(keywords::channel = "5");
  156. src::channel_logger<std::string> lg_chan_6(keywords::channel = "6");
  157. */
  158. core->add_global_attribute("ThreadID", boost::log::attributes::current_thread_id());
  159. core->add_global_attribute("TimeStamp",attrs::local_clock());
  160. core->add_global_attribute("RecordID", attrs::counter< unsigned int >(1));
  161. logging::add_common_attributes();
  162.  
  163. //lg_chan_0(keywords::channel = "net");
  164.  
  165. // Create a text file sink
  166. boost::shared_ptr< file_sink > sink0(new file_sink(
  167. keywords::file_name = "first_date_with_404.log",
  168. keywords::rotation_size = 16384
  169. ));
  170.  
  171. // Create a text file sink
  172. boost::shared_ptr< file_sink > sink1(new file_sink(
  173. keywords::file_name = "first_date_with_200.log",
  174. keywords::rotation_size = 16384
  175. ));
  176.  
  177. // Create a text file sink
  178. boost::shared_ptr< file_sink > sink2(new file_sink(
  179. keywords::file_name = "dates_with_zero_size_after_first_date_with_200.log",
  180. keywords::rotation_size = 16384
  181. ));
  182.  
  183. // Create a text file sink
  184. boost::shared_ptr< file_sink > sink3(new file_sink(
  185. keywords::file_name = "dates_of_saturday_and_sunday_after_first_date_with_200.log",
  186. keywords::rotation_size = 16384
  187. ));
  188.  
  189. // Create a text file sink
  190. boost::shared_ptr< file_sink > sink4(new file_sink(
  191. keywords::file_name = "dates_not_saturday_or_sunday_after_first_date_with_200_having_zero_size.log",
  192. keywords::rotation_size = 16384
  193. ));
  194.  
  195. // Create a text file sink
  196. boost::shared_ptr< file_sink > sink5(new file_sink(
  197. keywords::file_name = "dates_not_saturday_or_sunday_after_first_date_with_200_having_400.log",
  198. keywords::rotation_size = 16384
  199. ));
  200.  
  201. // Create a text file sink
  202. boost::shared_ptr< file_sink > sink6(new file_sink(
  203. keywords::file_name = "last_date_recieved_and_written_to_file.log",
  204. keywords::rotation_size = 16384
  205. ));
  206. /*
  207. // Create a text file sink
  208. boost::shared_ptr< file_sink > sink(new file_sink(
  209. keywords::file_name = "%Y%m%d_%H%M%S_%5N.xml",
  210. keywords::rotation_size = 16384
  211. ));
  212. */
  213. sink0->set_filter(expr::attr< std::string >("Channel") == "0");
  214. sink1->set_filter(expr::attr< std::string >("Channel") == "1");
  215. sink2->set_filter(expr::attr< std::string >("Channel") == "2");
  216. sink3->set_filter(expr::attr< std::string >("Channel") == "3");
  217. sink4->set_filter(expr::attr< std::string >("Channel") == "4");
  218. sink5->set_filter(expr::attr< std::string >("Channel") == "5");
  219. sink6->set_filter(expr::attr< std::string >("Channel") == "6");
  220.  
  221.  
  222.  
  223. // Set up where the rotated files will be stored
  224. init_file_collecting(sink0);
  225. init_file_collecting(sink1);
  226. init_file_collecting(sink2);
  227. init_file_collecting(sink3);
  228. init_file_collecting(sink4);
  229. init_file_collecting(sink5);
  230. init_file_collecting(sink6);
  231.  
  232. // Upon restart, scan the directory for files matching the file_name pattern
  233. sink0->locked_backend()->scan_for_files();
  234. sink1->locked_backend()->scan_for_files();
  235. sink2->locked_backend()->scan_for_files();
  236. sink3->locked_backend()->scan_for_files();
  237. sink4->locked_backend()->scan_for_files();
  238. sink5->locked_backend()->scan_for_files();
  239. sink6->locked_backend()->scan_for_files();
  240.  
  241. sink0->set_formatter
  242. (
  243. expr::format("record id="%1%" ["%2%"] thread_id= %3% %4%")
  244. % expr::attr< unsigned int >("RecordID")
  245. % expr::attr< boost::posix_time::ptime >("TimeStamp")
  246. % expr::attr< attrs::current_thread_id::value_type >("ThreadID")
  247. % expr::message
  248. );
  249.  
  250. sink1->set_formatter
  251. (
  252. expr::format("record id="%1%" ["%2%"] thread_id= %3% %4%")
  253. % expr::attr< unsigned int >("RecordID")
  254. % expr::attr< boost::posix_time::ptime >("TimeStamp")
  255. % expr::attr< attrs::current_thread_id::value_type >("ThreadID")
  256. % expr::message
  257. );
  258. sink2->set_formatter
  259. (
  260. expr::format("record id="%1%" ["%2%"] thread_id= %3% %4%")
  261. % expr::attr< unsigned int >("RecordID")
  262. % expr::attr< boost::posix_time::ptime >("TimeStamp")
  263. % expr::attr< attrs::current_thread_id::value_type >("ThreadID")
  264. % expr::message
  265. );
  266. sink3->set_formatter
  267. (
  268. expr::format("record id="%1%" ["%2%"] thread_id= %3% %4%")
  269. % expr::attr< unsigned int >("RecordID")
  270. % expr::attr< boost::posix_time::ptime >("TimeStamp")
  271. % expr::attr< attrs::current_thread_id::value_type >("ThreadID")
  272. % expr::message
  273. );
  274. sink4->set_formatter
  275. (
  276. expr::format("record id="%1%" ["%2%"] thread_id= %3% %4%")
  277. % expr::attr< unsigned int >("RecordID")
  278. % expr::attr< boost::posix_time::ptime >("TimeStamp")
  279. % expr::attr< attrs::current_thread_id::value_type >("ThreadID")
  280. % expr::message
  281. );
  282. sink5->set_formatter
  283. (
  284. expr::format("record id="%1%" ["%2%"] thread_id= %3% %4%")
  285. % expr::attr< unsigned int >("RecordID")
  286. % expr::attr< boost::posix_time::ptime >("TimeStamp")
  287. % expr::attr< attrs::current_thread_id::value_type >("ThreadID")
  288. % expr::message
  289. );
  290. sink6->set_formatter
  291. (
  292. expr::format("record id="%1%" ["%2%"] thread_id= %3% %4%")
  293. % expr::attr< unsigned int >("RecordID")
  294. % expr::attr< boost::posix_time::ptime >("TimeStamp")
  295. % expr::attr< attrs::current_thread_id::value_type >("ThreadID")
  296. % expr::message
  297. );
  298.  
  299. /*
  300. // Set header and footer writing functors
  301. namespace bll = boost::lambda;
  302.  
  303. sink->locked_backend()->set_open_handler
  304. (
  305. bll::_1 << "<?xml version="1.0"?>n<log>n"
  306. );
  307. sink->locked_backend()->set_close_handler
  308. (
  309. bll::_1 << "</log>n"
  310. );
  311. */
  312.  
  313. // Add the sink to the core
  314. //logging::core::get()->add_sink(sink);
  315. core->add_sink(sink0);
  316. core->add_sink(sink1);
  317. core->add_sink(sink2);
  318. core->add_sink(sink3);
  319. core->add_sink(sink4);
  320. core->add_sink(sink5);
  321. core->add_sink(sink6);
  322. }
  323. //]
  324.  
  325. //enum { LOG_RECORDS_TO_WRITE = 2000 };
  326. /*
  327. int main(int argc, char* argv[])
  328. {
  329. try
  330. {
  331. // Initialize logging library
  332. init_logging();
  333.  
  334.  
  335. // And also add some attributes
  336. logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
  337. logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());
  338.  
  339. // Do some logging
  340. src::logger lg;
  341. for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i)
  342. {
  343. BOOST_LOG(lg) << "XML log record " << i;
  344. }
  345.  
  346. // Test that XML character decoration works
  347. BOOST_LOG(lg) << "Special XML characters: &, <, >, ", '";
  348.  
  349. return 0;
  350. }
  351. catch (std::exception& e)
  352. {
  353. std::cout << "FAILURE: " << e.what() << std::endl;
  354. return 1;
  355. }
  356. }
  357. */
  358.  
  359. /*
  360. * Copyright Andrey Semashev 2007 - 2015.
  361. * Distributed under the Boost Software License, Version 1.0.
  362. * (See accompanying file LICENSE_1_0.txt or copy at
  363. * http://www.boost.org/LICENSE_1_0.txt)
  364. */
  365. /*
  366. #include <boost/log/core.hpp>
  367. #include <boost/log/trivial.hpp>
  368. #include <boost/log/expressions.hpp>
  369. #include <boost/log/sinks/text_file_backend.hpp>
  370. #include <boost/log/utility/setup/file.hpp>
  371. #include <boost/log/utility/setup/common_attributes.hpp>
  372. #include <boost/log/sources/severity_logger.hpp>
  373. #include <boost/log/sources/record_ostream.hpp>
  374.  
  375. namespace logging = boost::log;
  376. namespace src = boost::log::sources;
  377. namespace sinks = boost::log::sinks;
  378. namespace keywords = boost::log::keywords;
  379. namespace expr = boost::log::expressions;
  380. namespace attrs = boost::log::attributes;
  381. #if 0
  382.  
  383. //[ example_tutorial_file_simple
  384. void init()
  385. {
  386. logging::add_file_log("sample.log");
  387.  
  388. logging::core::get()->set_filter
  389. (
  390. logging::trivial::severity >= logging::trivial::info
  391. );
  392. }
  393. //]
  394.  
  395. // We need this due to this bug: https://svn.boost.org/trac/boost/ticket/4416
  396. //[ example_tutorial_file_advanced_no_callouts
  397. void init()
  398. {
  399. logging::add_file_log
  400. (
  401. keywords::file_name = "sample_%N.log",
  402. keywords::rotation_size = 10 * 1024 * 1024,
  403. keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
  404. keywords::format = "[%TimeStamp%]: %Message%"
  405. );
  406.  
  407. logging::core::get()->set_filter
  408. (
  409. logging::trivial::severity >= logging::trivial::info
  410. );
  411. }
  412. //]
  413.  
  414. #else
  415.  
  416. //[ example_tutorial_file_advanced
  417. void init()
  418. {
  419. logging::add_file_log
  420. (
  421. keywords::file_name = "sample_%N.log", //*< file name pattern >
  422. keywords::rotation_size = 10 * 1024 * 1024, //*< rotate files every 10 MiB... >
  423. keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), //*< ...or at midnight >
  424. keywords::format = "[%TimeStamp%]: %Message%" //*< log record format >
  425. );
  426.  
  427. logging::core::get()->set_filter
  428. (
  429. logging::trivial::severity >= logging::trivial::info
  430. );
  431. }
  432. //]
  433.  
  434. #endif
  435. void init_logging()
  436. {
  437. // Create a text file sink
  438. boost::shared_ptr< file_sink > sink(new file_sink(
  439. keywords::file_name = "%Y%m%d_%H%M%S_%5N.xml",
  440. keywords::rotation_size = 16384
  441. ));
  442.  
  443. // Set up where the rotated files will be stored
  444. init_file_collecting(sink);
  445.  
  446. // Upon restart, scan the directory for files matching the file_name pattern
  447. sink->locked_backend()->scan_for_files();
  448.  
  449. sink->set_formatter
  450. (
  451. expr::format("t<record id="%1%" timestamp="%2%">%3%</record>")
  452. % expr::attr< unsigned int >("RecordID")
  453. % expr::attr< boost::posix_time::ptime >("TimeStamp")
  454. % expr::xml_decor[expr::stream << expr::smessage]
  455. );
  456.  
  457. // Set header and footer writing functors
  458. namespace bll = boost::lambda;
  459.  
  460. sink->locked_backend()->set_open_handler
  461. (
  462. bll::_1 << "<?xml version="1.0"?>n<log>n"
  463. );
  464. sink->locked_backend()->set_close_handler
  465. (
  466. bll::_1 << "</log>n"
  467. );
  468.  
  469. // Add the sink to the core
  470. logging::core::get()->add_sink(sink);
  471. }
  472. */
  473.  
  474. #pragma once
  475. //#include "stdafx.h"
  476. #include "boost/date_time/posix_time/posix_time.hpp"
  477. #include "boost/date_time/gregorian/gregorian.hpp" //include all types plus i/o
  478. #include <boost/program_options.hpp>
  479. #include <boost/any.hpp>
  480. #include <iostream>
  481. #include <iterator>
  482. #include <string>
  483. #include <vector>
  484. #include "utils.h"
  485.  
  486. namespace po = boost::program_options;
  487. using namespace std;
  488. using namespace boost::posix_time;
  489. using namespace boost::gregorian;
  490.  
  491. po::variables_map init_program_options(int argc, char* argv[])
  492. {
  493. const string VERSION = "0.2.1";
  494. try {
  495. date_duration single_day(1);
  496. auto facet = new date_input_facet();
  497. facet->set_iso_extended_format();
  498. //facet->format("%Y-%m-%d");
  499. std::locale::global(std::locale(std::locale(), facet));
  500.  
  501. std::vector<symbols_enum> symbols_default = { symbols_enum::GBPJPY};
  502.  
  503. string prog = "DUKA";
  504.  
  505. po::options_description desc(prog + " Allowed options");
  506. desc.add_options()
  507. ("help", "produce help message")
  508. ("version,v", "outputs Version of program")
  509. ("day,d", po::value<date>()->default_value(day_clock::universal_day()), "specific day format YYYY-MM-DD (default today)")
  510. ("startdate,s", po::value<date>()->default_value(day_clock::universal_day() - single_day), "start date format YYYY-MM-DD (default today)")
  511. ("enddate,e", po::value<date>()->default_value(day_clock::universal_day()), "end date format YYYY-MM-DD (default today)")
  512. ("thread,t", po::value<int>()->default_value(20), "number of threads (default 20)")
  513. ("folder,f", po::value<string>()->default_value("."), "destination folder (default .)")
  514. ("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")
  515. ("header", po::value<bool>()->default_value(false), "include CSV header (default false)")
  516. ("symbols", po::value<std::vector<symbols_enum>>()->multitoken()->composing()->default_value(symbols_default, "GBPJPY"), "symbol list using format EURUSD EURGBP")
  517. ;
  518. po::positional_options_description pd;
  519. pd.add("symbols", -1);
  520.  
  521. command_line_parser parser{ argc, argv };
  522. parser.options(desc).positional(pd);
  523. parsed_options parsed_options = parser.run();
  524.  
  525. po::variables_map vm;
  526. po::store(parsed_options, vm);
  527. po::notify(vm);
  528.  
  529. if (vm.count("help")) {
  530. cout << desc << "n";
  531. exit(0);
  532. }
  533.  
  534. if (vm.count("version")) {
  535. cout << VERSION << "n";
  536. exit(0);
  537. }
  538.  
  539. if (vm.count("day")) {
  540. cout << "day set to "
  541. << vm["day"].as<date>() << ".n";
  542.  
  543. }
  544. else {
  545. cout << "day was not set.n";
  546. }
  547.  
  548. if (vm.count("startdate")) {
  549. cout << "startdate set to "
  550. << vm["startdate"].as<date>() << ".n";
  551.  
  552. }
  553. else {
  554. cout << "startdate was not set.n";
  555. }
  556.  
  557. if (vm.count("enddate")) {
  558. cout << "enddate set to "
  559. << vm["enddate"].as<date>() << ".n";
  560.  
  561. }
  562. else {
  563. cout << "enddate was not set.n";
  564. }
  565.  
  566. if (vm.count("thread")) {
  567. cout << "thread set to "
  568. << vm["thread"].as<int>() << ".n";
  569.  
  570. }
  571. else {
  572. cout << "thread was not set.n";
  573. }
  574.  
  575. if (vm.count("folder")) {
  576. cout << "folder set to "
  577. << vm["folder"].as<string>() << ".n";
  578.  
  579. }
  580. else {
  581. cout << "folder was not set.n";
  582. }
  583.  
  584. if (vm.count("candle")) {
  585. cout << "candle set to "
  586. << vm["candle"].as<timeframe_enum>() << ".n";
  587.  
  588. }
  589. else {
  590. cout << "candle level was not set.n";
  591. }
  592.  
  593.  
  594.  
  595. if (vm.count("header")) {
  596. cout << "header set to "
  597. << vm["header"].as<bool>() << ".n";
  598.  
  599. }
  600. else {
  601. cout << "header was not set.n";
  602. }
  603.  
  604. if (vm.count("symbols")) {
  605. cout << "symbols set to "
  606. << vm["symbols"].as<std::vector<symbols_enum>>() << ".n";
  607.  
  608. }
  609. else {
  610. cout << "symbols was not set.n";
  611. }
  612. return vm;
  613. }
  614. catch (exception& e) {
  615. cerr << "error: " << e.what() << "n";
  616. exit(1);
  617. }
  618. catch (...) {
  619. cerr << "Exception of unknown type!n";
  620. }
  621. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement