fix8  version 1.4.0
Open Source C++ FIX Framework
hfprint.cpp File Reference
#include <iostream>
#include <memory>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <iterator>
#include <algorithm>
#include <typeinfo>
#include <sys/ioctl.h>
#include <signal.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#include <fix8/f8includes.hpp>
#include <fix8/usage.hpp>
#include <fix8/consolemenu.hpp>
#include "Perf_types.hpp"
#include "Perf_router.hpp"
#include "Perf_classes.hpp"
#include "hftest.hpp"

Go to the source code of this file.

Typedefs

using MessageCount = map< string, unsigned >
 

Functions

void print_usage ()
 
const string GETARGLIST ("hsvo:c")
 
bool term_received (false)
 
bool summary (false)
 
void sig_handler (int sig)
 
int main (int argc, char **argv)
 

Detailed Description


This is a simple logfile/logstream printer using the metadata generated for hftest.cpp.

hfprint – f8 protocol log printer

Usage: hfprint [-hosv] <fix protocol file, use '-' for stdin>
-h,–help help, this screen
-o,–offset bytes to skip on each line before parsing FIX message
-s,–summary summary, generate message summary
-v,–version print version then exit
e.g.
hfprint myfix_server_protocol.log
cat myfix_client_protocol.log | hfprint -

Definition in file hfprint.cpp.

Typedef Documentation

using MessageCount = map<string, unsigned>

Definition at line 102 of file hfprint.cpp.

Function Documentation

const string GETARGLIST ( "hsvo:c"  )

Referenced by main(), and print_usage().

int main ( int  argc,
char **  argv 
)

Definition at line 118 of file hfprint.cpp.

References FIX8::F8MetaCntx::_beginStr, FIX8::BaseMsgEntry::_name, FIX8::TEX::ctx(), FIX8_PACKAGE, FIX8_VERSION, GETARGLIST(), inputFile, print_usage(), sig_handler(), summary(), term_received(), FIX8::F8MetaCntx::version(), and FIX8::f8Exception::what().

119 {
120  int val, offset(0);
121 
122 #ifdef FIX8_HAVE_GETOPT_LONG
123  option long_options[]
124  {
125  { "help", 0, 0, 'h' },
126  { "offset", 1, 0, 'o' },
127  { "version", 0, 0, 'v' },
128  { "summary", 0, 0, 's' },
129  { "context", 0, 0, 'c' },
130  { 0 },
131  };
132 
133  while ((val = getopt_long (argc, argv, GETARGLIST.c_str(), long_options, 0)) != -1)
134 #else
135  while ((val = getopt (argc, argv, GETARGLIST.c_str())) != -1)
136 #endif
137  {
138  switch (val)
139  {
140  case 'v':
141  cout << argv[0] << " for " FIX8_PACKAGE " version " FIX8_VERSION << endl;
142  cout << "Released under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3. See <http://fsf.org/> for details." << endl;
143  return 0;
144  case ':': case '?': return 1;
145  case 'h': print_usage(); return 0;
146  case 'o': offset = stoi(optarg); break;
147  case 's': summary = true; break;
148  case 'c':
149  cout << "Context FIX beginstring:" << TEX::ctx()._beginStr << endl;
150  cout << "Context FIX version:" << TEX::ctx().version() << endl;
151  return 0;
152  default: break;
153  }
154  }
155 
156  signal(SIGTERM, sig_handler);
157  signal(SIGINT, sig_handler);
158 
159  string inputFile;
160  if (optind < argc)
161  inputFile = argv[optind];
162  if (inputFile.empty())
163  {
164  print_usage();
165  return 1;
166  }
167 
168  bool usestdin(inputFile == "-");
169  filestdin ifs(usestdin ? &cin : new ifstream(inputFile.c_str()), usestdin);
170  if (!ifs())
171  {
172  cerr << "Could not open " << inputFile << endl;
173  return 1;
174  }
175 
176  unsigned msgs(0);
177  MessageCount *mc(summary ? new MessageCount : 0);
178 
179  const int bufsz(4096);
180  char buffer[bufsz];
181 
182  try
183  {
184  while (!ifs().eof() && !term_received)
185  {
186  ifs().getline(buffer, bufsz);
187  if (buffer[0])
188  {
189  unique_ptr<Message> msg(Message::factory(TEX::ctx(), buffer + offset));
190  if (summary)
191  {
192  MessageCount::iterator mitr(mc->find(msg->get_msgtype()));
193  if (mitr == mc->end())
194  mc->insert({msg->get_msgtype(), 1});
195  else
196  mitr->second++;
197  }
198  cout << *msg << endl;
199  ++msgs;
200  }
201  }
202 
203  if (term_received)
204  cerr << "interrupted" << endl;
205  }
206  catch (f8Exception& e)
207  {
208  cerr << "exception: " << e.what() << endl;
209  }
210  catch (exception& e) // also catches Poco::Net::NetException
211  {
212  cerr << "exception: " << e.what() << endl;
213  }
214 
215  cout << msgs << " messages decoded." << endl;
216  if (summary)
217  {
218  for (MessageCount::const_iterator mitr(mc->begin()); mitr != mc->end(); ++mitr)
219  {
220  const BaseMsgEntry *bme(TEX::ctx()._bme.find_ptr(mitr->first.c_str()));
221  cout << setw(20) << left << bme->_name << " (\"" << mitr->first << "\")" << '\t' << mitr->second << endl;
222  }
223  }
224 
225  return 0;
226 }
bool summary(false)
bool term_received(false)
const string GETARGLIST("hsvo:c")
void sig_handler(int sig)
Definition: hfprint.cpp:105
string inputFile
Definition: f8c.cpp:96
map< string, unsigned > MessageCount
Definition: hfprint.cpp:102
Message instantiation table entry.
Definition: message.hpp:192
#define FIX8_PACKAGE
Definition: f8config.h:601
void print_usage()
Definition: hfprint.cpp:229
const f8String _beginStr
Fix header beginstring.
Definition: message.hpp:228
const F8MetaCntx & ctx()
Compiler generated metadata object, accessed through this function.
Base exception class.
Definition: f8exception.hpp:49
unsigned version() const
Definition: message.hpp:352
#define FIX8_VERSION
Definition: f8config.h:742
Abstract file or stdin input.
Definition: f8utils.hpp:1250
const char * what() const
Definition: f8exception.hpp:85
void print_usage ( )

Definition at line 229 of file hfprint.cpp.

References UsageMan::add(), GETARGLIST(), UsageMan::print(), and UsageMan::setdesc().

Referenced by main().

230 {
231  UsageMan um("hfprint", GETARGLIST, "<fix protocol file, use '-' for stdin>");
232  um.setdesc("hfprint -- f8 protocol log printer");
233  um.add('h', "help", "help, this screen");
234  um.add('v', "version", "print version then exit");
235  um.add('o', "offset", "bytes to skip on each line before parsing FIX message");
236  um.add('s', "summary", "summary, generate message summary");
237  um.add("e.g.");
238  um.add("@hfprint myfix_server_protocol.log");
239  um.add("@hfprint hfprint -s -o 12 myfix_client_protocol.log");
240  um.add("@cat myfix_client_protocol.log | hfprint -");
241  um.print(cerr);
242 }
const string GETARGLIST("hsvo:c")
Convenient program help/usage wrapper. Generates a standardised usage message.
Definition: usage.hpp:42
void sig_handler ( int  sig)

Definition at line 105 of file hfprint.cpp.

References term_received().

Referenced by main().

106 {
107  switch (sig)
108  {
109  case SIGTERM:
110  case SIGINT:
111  term_received = true;
112  signal(sig, sig_handler);
113  break;
114  }
115 }
bool term_received(false)
void sig_handler(int sig)
Definition: hfprint.cpp:105
bool summary ( false  )

Referenced by main().

bool term_received ( false  )

Referenced by main(), and sig_handler().