OnixS C++ ICE Binary Order Entry Handler 1.1.1
API Documentation
Loading...
Searching...
No Matches
SbeLogDecoder.cpp
Go to the documentation of this file.
1/*
2 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3 *
4 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5 * and international copyright treaties.
6 *
7 * Access to and use of the software is governed by the terms of the applicable ONIXS Software
8 * Services Agreement (the Agreement) and Customer end user license agreements granting
9 * a non-assignable, non-transferable and non-exclusive license to use the software
10 * for it's own data processing purposes under the terms defined in the Agreement.
11 *
12 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13 * of this source code or associated reference material to any other location for further reproduction
14 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15 *
16 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17 * the terms of the Agreement is a violation of copyright law.
18 */
22
23#include <string>
24#include <iostream>
25#include <cstdio>
26#include <fstream>
27
28using namespace ONIXS_ICEBOE_NAMESPACE;
30using namespace ONIXS_ICEBOE_NAMESPACE::Encoding;
31
32#include <Common/Options.h>
33
34namespace
35{
36 struct MessagePrinter
37 {
38 MessagePrinter(std::ofstream& outFile, StrRef prefix)
39 : outFile_(outFile)
40 , prefix_(prefix)
41 {}
42
43 template <typename MessageType>
44 void operator()(const MessageType& msg) const
45 {
46 outFile_ << prefix_ << msg << std::endl << std::endl;
47
48 std::cout << prefix_ << msg << std::endl << std::endl;
49 }
50
51 std::ofstream& outFile_;
52 const StrRef prefix_;
53 };
54
55 std::string getOutputFileName(const std::string& entryName)
56 {
57 const char * const DecodedSummaryFileExtension = ".txt";
58 return entryName.substr(0, entryName.find_last_of(".")) + DecodedSummaryFileExtension;
59 }
60
61 size_t decodeSbeLogFile(const std::string& summaryFileName)
62 {
63 const std::string outputFileName = getOutputFileName(summaryFileName);
64 std::ofstream decoded(outputFileName.c_str());
65
66 if (!decoded)
67 throw std::runtime_error("Cannot create file: '" + outputFileName + "'");
68
69 std::ifstream file(summaryFileName.c_str());
70
71 if (!file)
72 throw std::runtime_error("Cannot open " + summaryFileName + " log file.");
73
74 const size_t LogLinePrefixSize = 32;
75
76 std::string line;
77 ByteArray buffer;
78 size_t messageCounter = 0;
79
80 while (getline(file, line))
81 {
82 try
83 {
84 const StrRef Prefix(line.data(), LogLinePrefixSize);
85 const StrRef Base64SbeMessage(line.data() + LogLinePrefixSize, line.size() - LogLinePrefixSize);
86
87 Base64Encoding::decode(buffer, Base64SbeMessage);
88
89 if (buffer.empty())
90 throw std::runtime_error("Cannot decode an empty message");
91
92 const auto message = NetworkMessage(&buffer[0], static_cast<MessageSize>(buffer.size())).message();
93
94 assert(message.valid());
95
96 if (!processTypified(message, MessagePrinter(decoded, Prefix)))
97 throw std::runtime_error("Unknown message type");
98
99 ++messageCounter;
100 }
101 catch (...)
102 {
103 std::cerr << "Cannot decode '" << line << "'" << std::endl;
104 throw;
105 }
106 }
107
108 return messageCounter;
109 }
110
111 void gatherFiles(std::vector<std::string>& gatheredFiles, const std::string& root)
112 {
114 Filesystem::gatherFiles(&gatheredFiles, root, ".summary");
115 else
116 gatheredFiles.push_back(root);
117 }
118
119 class FileDecoder
120 {
121 public:
122 FileDecoder()
123 : messagesCount_(0)
124 , filesCount_(0)
125 {
126 }
127
128 void operator()(const std::string& name)
129 {
130 messagesCount_ += decodeSbeLogFile(name);
131 ++filesCount_;
132 }
133
134 size_t messagesCount() const
135 {
136 return messagesCount_;
137 }
138
139 size_t filesCount() const
140 {
141 return filesCount_;
142 }
143
144 private:
145 size_t messagesCount_;
146 size_t filesCount_;
147 };
148}
149
150int main(int argc, char* argv[])
151{
152 // `--help` to show options.
153 const Samples::AppConfiguration<Samples::InputLogFileConfiguration> cfg{"SbeLogDecoder", argc, argv};
154
155 const auto location = cfg.location();
156
157 if(!Filesystem::exist(location))
158 {
159 std::cerr << "'" + location + "'" + " does not exist.";
160 return 1;
161 }
162
163 std::vector<std::string> inputFiles;
164 gatherFiles(inputFiles, location);
165
166 const FileDecoder decoder =
167 std::for_each(inputFiles.begin(), inputFiles.end(), FileDecoder());
168
169 std::clog << decoder.messagesCount() << " messages in " << decoder.filesCount() << " file(s) are decoded." << std::endl << std::endl;
170
171 return 0;
172}
#define ONIXS_ICEBOE_NAMESPACE
Definition ABI.h:113
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE
Definition ABI.h:114
int main(int argc, char *argv[])
static void decode(ByteArray &decoded, const std::string &encoded)
Reconstructs the binary data from a BASE64-encoded string.
SbeMessage message() const noexcept
Retrieves the underlying SBE message.
std::vector< unsigned char > ByteArray
Byte sequence.
bool exist(const std::string &entry)
void gatherFiles(std::vector< std::string > *gatheredFiles, const std::string &root, const std::string &extension)
Gathers files which are stored in a given folder.
bool isDirectory(const std::string &path)
bool processTypified(SbeMessage binary, Processor &&processor)
Casts a given binary message according to template/type information and processes the cast messages b...
UInt16 MessageSize
Message length type.
Definition Aliases.h:29
std::basic_string_view< Char > StrRef
Definition StrRef.h:46