Update build system, documentation and delete temp files
[wave300.git] / tools / rtlogger / logcnv / logcnv.cpp
blobe2d4efaeb50ff0f876ca383588dc48cf01521b87
1 /******************************************************************************
3 Copyright (c) 2012
4 Lantiq Deutschland GmbH
6 For licensing information, see the file 'LICENSE' in the root folder of
7 this software module.
9 ******************************************************************************/
10 #include "mtlkinc.h"
11 #include <iostream>
13 #ifdef WIN32
14 #include <fcntl.h>
15 #include <io.h>
16 #include <winsock2.h>
17 #endif
19 #include "CmdLine.h"
20 #include "ParamInfo.h"
21 #include "Debug.h"
22 #include "aux_utils.h"
24 #include "LogInfo.h"
25 #include "LogEvt.h"
26 #include "LogFmtDB.h"
28 #include "pcapdefs.h"
29 #include <stdexcept>
30 #include <fstream>
32 using namespace std;
34 #define MTLK_LOGGER_NETWORK 147 /* Corresponds to wtap 45 - WTAP_ENCAP_USER0 */
35 #define MTLK_LOGGER_SNAPLEN 65535
37 #define SCD_FILES_DELIM ","
39 static const pcap_hdr_t log_pcap_hdr =
41 PCAP_MAGIC,
42 PCAP_VERSION_MAJOR,
43 PCAP_VERSION_MINOR,
46 MTLK_LOGGER_SNAPLEN,
47 MTLK_LOGGER_NETWORK
50 #pragma pack(push,1)
51 typedef struct {
52 uint8 oid;
53 uint8 gid;
54 uint16 fid;
55 uint16 lid;
56 uint8 wlanif;
57 /* Followed by:
58 uint16 src_len;
59 char src_str[];
60 uint16 dst_len;
61 char dst_str[];
62 uint16 msg_len;
63 char msg_str[];
65 } mtlklog_hdr_t;
66 #pragma pack(pop)
68 #if 1
69 #define LOCAL_DBG_TRACE(x) DBG_TRACE("logcnv: " << x)
70 #else
71 #define LOCAL_DBG_TRACE(x)
72 #endif
74 static const ParamInfo paramShowHelp(CCmdLine::ParamName("h", "help"),
75 "Show this help.");
76 static const ParamInfo paramDebugLevel(CCmdLine::ParamName("d", "debug"),
77 "Application debug level (for debugging purposes only)",
78 "dlevel");
79 static const ParamInfo paramStringFiles(CCmdLine::ParamName("s", "str-file"),
80 "Compilation extracted string-file (.scd)",
81 "file1[,file2[,...]]");
82 static const ParamInfo paramPCapOut(CCmdLine::ParamName("p", "pcap"),
83 "Output in PCAP format");
84 static const ParamInfo paramInFile(CCmdLine::ParamName("i", "input"),
85 "Log file to read (default: stdin)",
86 "file");
87 static const ParamInfo paramOutFile(CCmdLine::ParamName("o", "output"),
88 "Output file (default: stdout)",
89 "file");
91 #define stream_enable_exceptions(s) (s).exceptions(ios::badbit | ios::failbit)
93 static void
94 ShowUsage (void)
96 static const ParamInfo* AllParams[] = {&paramShowHelp,
97 &paramDebugLevel,
98 &paramStringFiles,
99 &paramPCapOut,
100 &paramInFile,
101 &paramOutFile};
103 CHelpScreen HelpScreen;
105 cerr << "Metalink Log Convertor Utility v." MTLK_SOURCE_VERSION
106 << endl << endl
107 << "Usage:" << endl << "\tlogcnv ";
109 for (size_t i = 0 ; i < ARRAY_SIZE(AllParams); i++) {
110 HelpScreen.AddParam(*AllParams[i]);
113 cerr << HelpScreen.GetHelp();
116 static void
117 WriteString (string& str, ostream &out_s)
119 uint16 size = str.length() + 1;
120 uint16 sz_h = HOST_TO_NET16(size);
121 out_s.write((const char *)&sz_h, sizeof(sz_h));
122 out_s.write(str.c_str(), size);
125 static void
126 ProcessLog (istream &in_s, ostream &out_s, vector<string> &scd_files, bool pcap_out)
128 CLogInfo log_info;
129 CLogFmtDB fmt_db;
130 pcaprec_hdr_t pcaprec_hdr;
131 mtlklog_hdr_t mtlklog_hdr;
133 in_s >> log_info;
135 for (vector<string>::iterator it = scd_files.begin(); it != scd_files.end(); ++it) {
136 fmt_db.Read(*it);
139 /* Put the PCAP file header */
140 if (pcap_out) {
141 out_s.write((const char *)&log_pcap_hdr, sizeof(log_pcap_hdr));
144 while (!in_s.eof()) {
145 CLogEvt log_evt;
147 in_s >> log_evt;
148 if (log_evt.HasData()) {
149 if (!pcap_out) {
150 out_s << "[" << setw(10) << setfill('0') << log_evt.GetTS() << setfill(' ') << setw(0) << "] " << log_evt.GetMsgString(fmt_db) << "'" << endl;
152 else {
153 string src = log_evt.GetSrcString(fmt_db);
154 string dst = log_evt.GetDstString(fmt_db);
155 string msg = log_evt.GetMsgString(fmt_db);
157 pcaprec_hdr.ts_sec = log_evt.GetTS()/1000;
158 pcaprec_hdr.ts_usec = (log_evt.GetTS()%1000) * 1000;
159 pcaprec_hdr.incl_len =
160 pcaprec_hdr.orig_len = sizeof(mtlklog_hdr_t) +
161 sizeof(uint16) + src.length() + 1 +
162 sizeof(uint16) + dst.length() + 1 +
163 sizeof(uint16) + msg.length() + 1;
164 out_s.write((const char *)&pcaprec_hdr, sizeof(pcaprec_hdr_t));
165 mtlklog_hdr.oid = log_evt.GetOID();
166 mtlklog_hdr.gid = log_evt.GetGID();
167 mtlklog_hdr.fid = HOST_TO_NET16(log_evt.GetFID());
168 mtlklog_hdr.lid = HOST_TO_NET16(log_evt.GetLID());
169 mtlklog_hdr.wlanif = log_evt.GetWLANIF();
170 out_s.write((const char *)&mtlklog_hdr, sizeof(mtlklog_hdr));
171 WriteString(src, out_s);
172 WriteString(dst, out_s);
173 WriteString(msg, out_s);
179 enum MAIN_RETVALS
181 MAIN_SUCCESS = 0,
182 MAIN_KNOWN_ERROR = -1,
183 MAIN_UNKNOWN_ERROR = -2
186 #ifdef WIN32
187 int __cdecl main(int argc, char* argv[])
188 #else
189 int main(int argc, char* argv[])
190 #endif
192 try {
193 CCmdLine cmdLine(argc, argv);
194 string strStringFiles = cmdLine.getParamValue(paramStringFiles);
196 if (cmdLine.isCmdLineParam(paramShowHelp)) {
197 LOCAL_DBG_TRACE("Entering help mode...");
198 ShowUsage();
201 vector<string> scd_files;
202 CStrTokenizer tok(strStringFiles);
203 for(CStrTokenizer::iterator t = tok.begin(SCD_FILES_DELIM); t; ++t) {
204 LOCAL_DBG_TRACE("\tTokenizer returned path \"" << t.get() << "\"");
206 if(t.get().empty())
207 continue;
209 scd_files.push_back(t.get());
212 if (scd_files.empty()) {
213 ShowUsage();
214 throw logic_error("At least one string-file (.scd) must be specified!");
217 istream *in_s = &cin;
218 ostream *out_s = &cout;
219 ifstream in_f;
220 ofstream out_f;
221 string fName;
222 bool pcap_out = cmdLine.isCmdLineParam(paramPCapOut);
224 fName = cmdLine.getParamValue(paramInFile);
225 if (!fName.empty()) {
226 stream_enable_exceptions(in_f);
227 in_f.open(fName.c_str(), ios::in | ios::binary);
228 in_s = &in_f;
230 else {
231 stream_enable_exceptions(cin);
232 #ifdef WIN32
233 /* Without _setmode() Windows replaces end-of-lines and breaks the data */
234 if (_setmode(_fileno(stdin), _O_BINARY) == -1) {
235 throw logic_error("Can't set STDIN to BINARY mode!");
237 #endif
240 fName = cmdLine.getParamValue(paramOutFile);
241 if (!fName.empty()) {
242 stream_enable_exceptions(out_f);
243 out_f.open(fName.c_str(), pcap_out?(ios::out | ios::binary):ios::out);
244 out_s = &out_f;
246 else {
247 stream_enable_exceptions(cout);
248 #ifdef WIN32
249 /* Without _setmode() Windows replaces end-of-lines and breaks the data */
250 if (_setmode(_fileno(stdout), _O_BINARY) == -1) {
251 throw logic_error("Can't set STDOUT to BINARY mode!");
253 #endif
256 ProcessLog(*in_s, *out_s, scd_files, pcap_out);
258 catch (const exception& ex) {
259 cerr << "Error occurred:" << endl << "\t"
260 << ex.what() << endl;
261 return MAIN_KNOWN_ERROR;
263 catch(...) {
264 cerr << "Unknown fatal error occurred." << endl;
265 return MAIN_UNKNOWN_ERROR;
268 return MAIN_SUCCESS;