JayBeams  0.1
Another project to have fun coding.
itch5stats.cpp
Go to the documentation of this file.
2 #include <jb/fileio.hpp>
3 #include <jb/log.hpp>
5 
6 #include <iostream>
7 #include <stdexcept>
8 
9 namespace {
10 
11 class config : public jb::config_object {
12 public:
13  config();
15 
16  void validate() const override;
17 
20 };
21 
22 /**
23  * An implementation of jb::message_handler_concept to capture
24  * ITCH-5.0 statistics.
25  */
26 class itch5_stats_handler {
27 public:
28  explicit itch5_stats_handler(config const& cfg)
29  : stats_(cfg.stats()) {
30  }
31 
33 
34  time_point now() const {
35  return std::chrono::steady_clock::now();
36  }
37 
38  template <typename message_type>
39  void handle_message(
40  time_point recv_ts, long msgcnt, std::size_t msgoffset,
41  message_type const& msg) {
42  JB_LOG(trace) << msgcnt << ":" << msgoffset << " " << msg;
43  auto pl = now() - recv_ts;
44  stats_.sample(msg.header.timestamp.ts, pl);
45  }
46 
47  void
48  handle_unknown(time_point recv_ts, jb::itch5::unknown_message const& msg) {
49  char msgtype = *static_cast<char const*>(msg.buf());
50  JB_LOG(error) << "Unknown message type '" << msgtype << "'(" << int(msgtype)
51  << ") in msgcnt=" << msg.count()
52  << ", msgoffset=" << msg.offset();
53  }
54 
55 private:
57 };
58 
59 } // anonymous namespace
60 
61 int main(int argc, char* argv[]) try {
62  config cfg;
63  cfg.load_overrides(argc, argv, std::string("itch5stats.yaml"), "JB_ROOT");
64  jb::log::init();
65 
66  boost::iostreams::filtering_istream in;
67  jb::open_input_file(in, cfg.input_file());
68 
69  itch5_stats_handler handler(cfg);
70  jb::itch5::process_iostream(in, handler);
71 
72  return 0;
73 } catch (jb::usage const& u) {
74  std::cerr << u.what() << std::endl;
75  return u.exit_status();
76 } catch (std::exception const& ex) {
77  std::cerr << "Standard exception raised: " << ex.what() << std::endl;
78  return 1;
79 } catch (...) {
80  std::cerr << "Unknown exception raised" << std::endl;
81  return 1;
82 }
83 
84 namespace {
85 config::config()
86  : input_file(
87  desc("input-file").help("An input file with ITCH-5.0 messages."),
88  this)
89  , stats(desc("stats", "offline-feed-statistics"), this) {
90 }
91 
92 void config::validate() const {
93  if (input_file() == "") {
94  throw jb::usage(
95  "Missing input-file setting."
96  " You must specify an input file.",
97  1);
98  }
99  stats().validate();
100 }
101 
102 } // anonymous namespace
clock_type::time_point time_point
A convenience alias for clock_type::time_point.
Base class for all configuration objects.
virtual void validate() const
Validate the settings.
int exit_status() const
Definition: usage.hpp:21
Keep statistics about a feed and its offline processor.
int main(int argc, char *argv[])
Definition: itch5stats.cpp:61
void process_iostream(std::istream &in, message_handler &handler)
Process an iostream of ITCH-5.0 messages.
void init(config const &cfg)
Initialize the logging functions using the configuration provided.
Definition: log.cpp:190
#define config_object_constructors(NAME)
Helper class to easily define configuration attributes.
A simple class to communicate the result of parsing the options.
Definition: usage.hpp:11
void open_input_file(boost::iostreams::filtering_istream &in, std::string const &filename)
Open a file for reading.
Definition: fileio.cpp:27
#define JB_LOG(lvl)
Definition: log.hpp:70
void const * buf() const
std::uint32_t count() const
std::uint64_t offset() const