fix8  version 1.4.0
Open Source C++ FIX Framework
myprint.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 "Myfix_types.hpp"
#include "Myfix_router.hpp"
#include "Myfix_classes.hpp"
#include "myfix.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 myfix.cpp.

f8print – f8 protocol log printer

Usage: f8print [-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.
f8print myfix_server_protocol.log
cat myfix_client_protocol.log | f8print -

Definition in file myprint.cpp.

Typedef Documentation

using MessageCount = map<string, unsigned>

Definition at line 102 of file myprint.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 myprint.cpp.

References FIX8::F8MetaCntx::_beginStr, FIX8::BaseMsgEntry::_name, FIX8::TEX::ctx(), FIX8_MAX_MSG_LENGTH, 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  char buffer[FIX8_MAX_MSG_LENGTH];
180 
181  try
182  {
183  while (!ifs().eof() && !term_received)
184  {
185  ifs().getline(buffer, FIX8_MAX_MSG_LENGTH);
186  if (buffer[0])
187  {
188  unique_ptr<Message> msg(Message::factory(TEX::ctx(), buffer + offset));
189  if (summary)
190  {
191  MessageCount::iterator mitr(mc->find(msg->get_msgtype()));
192  if (mitr == mc->end())
193  mc->insert({msg->get_msgtype(), 1});
194  else
195  mitr->second++;
196  }
197  cout << *msg << endl;
198  ++msgs;
199  }
200  }
201 
202  if (term_received)
203  cerr << "interrupted" << endl;
204  }
205  catch (f8Exception& e)
206  {
207  cerr << "exception: " << e.what() << endl;
208  }
209  catch (exception& e) // also catches Poco::Net::NetException
210  {
211  cerr << "exception: " << e.what() << endl;
212  }
213 
214  cout << msgs << " messages decoded." << endl;
215  if (summary)
216  {
217  for (MessageCount::const_iterator mitr(mc->begin()); mitr != mc->end(); ++mitr)
218  {
219  const BaseMsgEntry *bme(TEX::ctx()._bme.find_ptr(mitr->first.c_str()));
220  cout << setw(20) << left << bme->_name << " (\"" << mitr->first << "\")" << '\t' << mitr->second << endl;
221  }
222  }
223 
224  return 0;
225 }
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 sig_handler(int sig)
Definition: myprint.cpp:105
bool term_received(false)
bool summary(false)
const string GETARGLIST("hsvo:c")
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
#define FIX8_MAX_MSG_LENGTH
Definition: f8config.h:576
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: myprint.cpp:228
void print_usage ( )

Definition at line 228 of file myprint.cpp.

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

Referenced by main().

229 {
230  UsageMan um("f8print", GETARGLIST, "<fix protocol file, use '-' for stdin>");
231  um.setdesc("f8print -- f8 protocol log printer");
232  um.add('h', "help", "help, this screen");
233  um.add('v', "version", "print version then exit");
234  um.add('o', "offset", "bytes to skip on each line before parsing FIX message");
235  um.add('s', "summary", "summary, generate message summary");
236  um.add('c', "context", "print the f8c generated beginstring and version. Use this to check which FIX version this build will work with.");
237  um.add("e.g.");
238  um.add("@f8print myfix_server_protocol.log");
239  um.add("@f8print f8print -s -o 12 myfix_client_protocol.log");
240  um.add("@cat myfix_client_protocol.log | f8print -");
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 myprint.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 }
void sig_handler(int sig)
Definition: myprint.cpp:105
bool term_received(false)
bool summary ( false  )

Referenced by main().

bool term_received ( false  )

Referenced by client_process(), main(), and sig_handler().