fix8  version 1.4.0
Open Source C++ FIX Framework
hfprint.cpp
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------------------
2 /*
3 
4 Fix8 is released under the GNU LESSER GENERAL PUBLIC LICENSE Version 3.
5 
6 Fix8 Open Source FIX Engine.
7 Copyright (C) 2010-16 David L. Dight <fix@fix8.org>
8 
9 Fix8 is free software: you can redistribute it and / or modify it under the terms of the
10 GNU Lesser General Public License as published by the Free Software Foundation, either
11 version 3 of the License, or (at your option) any later version.
12 
13 Fix8 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
14 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 
16 You should have received a copy of the GNU Lesser General Public License along with Fix8.
17 If not, see <http://www.gnu.org/licenses/>.
18 
19 BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO
20 THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE
21 COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY
22 KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO
24 THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE,
25 YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
26 
27 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT
28 HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED
29 ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
30 CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
31 NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
32 THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH
33 HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
34 
35 */
36 //-----------------------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------------------
55 #include <iostream>
56 #include <memory>
57 #include <fstream>
58 #include <iomanip>
59 #include <sstream>
60 #include <vector>
61 #include <map>
62 #include <list>
63 #include <set>
64 #include <iterator>
65 #include <algorithm>
66 #include <typeinfo>
67 
68 #ifdef _MSC_VER
69 #include <signal.h>
70 #else
71 #include <sys/ioctl.h>
72 #include <signal.h>
73 #include <termios.h>
74 #endif
75 
76 #include <errno.h>
77 #include <string.h>
78 
79 // f8 headers
80 #include <fix8/f8includes.hpp>
81 
82 #ifdef FIX8_HAVE_GETOPT_H
83 #include <getopt.h>
84 #endif
85 
86 #include <fix8/usage.hpp>
87 #include <fix8/consolemenu.hpp>
88 #include "Perf_types.hpp"
89 #include "Perf_router.hpp"
90 #include "Perf_classes.hpp"
91 #include "hftest.hpp"
92 
93 //-----------------------------------------------------------------------------------------
94 using namespace std;
95 using namespace FIX8;
96 
97 //-----------------------------------------------------------------------------------------
98 void print_usage();
99 const string GETARGLIST("hsvo:c");
100 bool term_received(false), summary(false);
101 
102 using MessageCount = map<string, unsigned>;
103 
104 //-----------------------------------------------------------------------------------------
105 void sig_handler(int sig)
106 {
107  switch (sig)
108  {
109  case SIGTERM:
110  case SIGINT:
111  term_received = true;
112  signal(sig, sig_handler);
113  break;
114  }
115 }
116 
117 //-----------------------------------------------------------------------------------------
118 int main(int argc, char **argv)
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 }
227 
228 //-----------------------------------------------------------------------------------------
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 }
243 
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
bool add(const char sw, const std::string &lsw, const std::string &help)
Definition: usage.hpp:73
#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 char * _name
Definition: message.hpp:195
const F8MetaCntx & ctx()
Compiler generated metadata object, accessed through this function.
Base exception class.
Definition: f8exception.hpp:49
void setdesc(const std::string &desc)
Definition: usage.hpp:91
void print(std::ostream &os) const
Definition: usage.hpp:95
unsigned version() const
Definition: message.hpp:352
int main(int argc, char **argv)
Definition: hfprint.cpp:118
#define FIX8_VERSION
Definition: f8config.h:742
Abstract file or stdin input.
Definition: f8utils.hpp:1250
const char * what() const
Definition: f8exception.hpp:85
Convenient program help/usage wrapper. Generates a standardised usage message.
Definition: usage.hpp:42