-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogman.cpp
More file actions
511 lines (422 loc) · 15.6 KB
/
logman.cpp
File metadata and controls
511 lines (422 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <unordered_map>
#include <vector>
#include <deque>
#include <fstream>
#include <set>
using namespace std;
// Create strcasecmp() ONLY for people using Visual Studio.
// Unix, Linux (CAEN) and MacOS already have it.
#ifdef _MSC_VER
inline int strcasecmp(const char *s1, const char *s2) {
return _stricmp(s1, s2);
}
#endif // _MSC_VER
//helper func
int64_t ts_conv(string ts) {
int64_t num = ts[ts.length() - 1] - '0';
int64_t zrs = 10;
for (size_t i = 1; i < ts.length(); ++i) {
if (!isdigit(ts[ts.length() - 1 - i]))
continue;
num += (ts[ts.length() - 1 - i] - '0') * zrs;
zrs *= 10;
}
return num;
}
void parse_str(string str, std::vector<string> &kw_vec) {
kw_vec.clear();
string kw;
for (size_t i = 0; i < str.length(); ++i) {
kw.clear();
while (isalnum(str[i])) {
kw.push_back(str[i]);
++i;
}
if (!kw.empty())
kw_vec.push_back(kw);
}
}
class LogEnt {
public:
LogEnt(string ts, string cat, string msg, int32_t entrid)
: time_stamps{ ts }, category{ cat },
message{ msg }, entID{ entrid } {}
string rtn_ts() {
return time_stamps;
}
string rtn_cat() {
return category;
}
string rtn_msg() {
return message;
}
int32_t rtn_entID() {
return entID;
}
int64_t rtn_ts_int() {
return ts_int;
}
friend class cmpEnt;
class cmpEnt {
public:
bool operator () (LogEnt *a, LogEnt *b) {
if (a->ts_int == b->ts_int) {
if (strcasecmp(a->category.c_str(), b->category.c_str()) == 0)
return a->entID < b->entID;
return strcasecmp(a->category.c_str(), b->category.c_str()) < 0;
}
return a->ts_int < b->ts_int;
}
};
private:
string time_stamps;
string category;
string message;
int64_t ts_int = ts_conv(time_stamps);
int32_t entID;
};
struct cmpTimeStamp {
bool operator() (LogEnt* a, LogEnt*b) {
return a->rtn_ts_int() < b->rtn_ts_int();
}
bool operator()(LogEnt *a, int64_t num) {
return a->rtn_ts_int() < num;
}
bool operator()(int64_t num, LogEnt *a) {
return a->rtn_ts_int() > num;
}
};
void print_LogEnt(LogEnt *a) {
std::cout << a->rtn_entID() << "|"
<< a->rtn_ts() << "|"
<< a->rtn_cat() << "|"
<< a->rtn_msg() << "\n";
}
struct LastResult {
// store last search
char last_s;
// c
string last_c;
bool found_c;
// t / m
std::vector<LogEnt*>::iterator itr_b;
std::vector<LogEnt*>::iterator itr_e;
// k
std::vector<int> last_k;
};
int main(int argc, char *argv[])
{
if (argc != 2) {
std::cout << "./logman <LOGFILE.txt>\n";
exit(1);
}
if (!strcmp(argv[1], "h") || !strcmp(argv[1], "help")) {
std::cout << "./logman <LOGFILE.txt>\n";
exit(0);
}
ios_base::sync_with_stdio(false);
std::vector<LogEnt*> master_log;
std::ifstream infile(argv[1]);
string ts, cat, msg;
int entid = 0;
while (std::getline(infile, ts, '|')) {
std::getline(infile, cat, '|');
std::getline(infile, msg);
LogEnt *entr = new LogEnt(ts, cat, msg, entid);
master_log.push_back(entr);
++entid;
} // finished puttting in master log
// sort master log
std::sort(master_log.begin(), master_log.end(), LogEnt::cmpEnt());
// put in relevant details (in sorted order)
std::unordered_map<string, vector<int32_t>> kw_log;
std::unordered_map<string, vector<int32_t>> ct_log;
std::vector<string> kw_dq;
std::vector<int32_t> entID_vc;
entID_vc.resize(master_log.size());
for (int i = 0; size_t(i) < master_log.size(); ++i) {
// entID index in masterlog
entID_vc[master_log[i]->rtn_entID()] = i;
// categories (SORTED ml order)
string cat = master_log[i]->rtn_cat();
string msg = master_log[i]->rtn_msg();
std::transform(cat.begin(), cat.end(), cat.begin(), ::tolower);
std::transform(msg.begin(), msg.end(), msg.begin(), ::tolower);
// add in category
ct_log[cat].push_back(i);
// add keywords in SORTED ml order
// keyword == categories
parse_str(cat, kw_dq);
while (!kw_dq.empty()) {
if (kw_log[kw_dq.back()].empty() ||
(!kw_log[kw_dq.back()].empty() &&
kw_log[kw_dq.back()].back() != i))
kw_log[kw_dq.back()].push_back(i);
kw_dq.pop_back();
}
// keyword == messages
parse_str(msg, kw_dq);
while (!kw_dq.empty()) {
if (kw_log[kw_dq.back()].empty() ||
(!kw_log[kw_dq.back()].empty() &&
kw_log[kw_dq.back()].back() != i))
kw_log[kw_dq.back()].push_back(i);
kw_dq.pop_back();
}
} // finished putting in relevant containers in SORTED ml order
std::cout << master_log.size() << " entries read\n";
std::vector<int32_t> expt_log;
LastResult last_search;
last_search.last_s = 'n';
char cmd;
do {
std::cout << "% ";
std::cin >> cmd;
switch (cmd) {
case 'a': {
size_t entid;
std::cin >> entid;
if (entid >= master_log.size()) {
std::cerr << "not valid entry ID\n";
break;
}
expt_log.push_back(entID_vc[entid]);
std::cout << "log entry " << entid << " appended\n";
break;
}
case 't': {
last_search.last_s = cmd;
string ts1, ts2;
std::getline(cin >> std::ws, ts1, '|');
std::getline(cin >> std::ws, ts2);
int64_t num1 = ts_conv(ts1);
int64_t num2 = ts_conv(ts2);
last_search.itr_b = std::lower_bound(master_log.begin(), master_log.end(), num1, cmpTimeStamp());
last_search.itr_e = std::upper_bound(master_log.begin(), master_log.end(), num2, cmpTimeStamp());
std::cout << "Timestamps search: " << (last_search.itr_e - last_search.itr_b) << " entries found\n";
break;
}
case 'm': {
last_search.last_s = cmd;
string ts;
std::cin >> ts;
int64_t num = ts_conv(ts);
last_search.itr_b = std::lower_bound(master_log.begin(), master_log.end(), num, cmpTimeStamp());
last_search.itr_e = std::upper_bound(master_log.begin(), master_log.end(), num, cmpTimeStamp());
std::cout << "Timestamp search: " << (last_search.itr_e - last_search.itr_b) << " entries found\n";
break;
}
case 'c': {
last_search.last_s = cmd;
string cat;
std::getline(cin >> std::ws, cat);
std::transform(cat.begin(), cat.end(), cat.begin(), ::tolower);
if (ct_log.find(cat) == ct_log.end()) {
last_search.found_c = false;
std::cout << "Category search: 0 entries found\n";
break;
}
last_search.found_c = true;
last_search.last_c = cat;
std::cout << "Category search: " << ct_log[cat].size() << " entries found\n";
break;
}
case 'p': {
// print nothing if no search
if (expt_log.empty())
break;
for (size_t i = 0; i < expt_log.size(); ++i) {
std::cout << i << "|";
print_LogEnt(master_log[expt_log[i]]);
}
break;
}
case 'g': {
// print nothing if no search
if (last_search.last_s == 'n')
break;
// for t, m, c, k
switch (last_search.last_s) {
case 't':
case 'm': {
auto beg = last_search.itr_b;
auto end = last_search.itr_e;
int ml_b = int(beg - master_log.begin());
int ml_e = int(end - master_log.begin());
for (int i = ml_b; i < ml_e; ++i)
print_LogEnt(master_log[i]);
break;
}
case 'c': {
if (!last_search.found_c)
break;
for (auto x : ct_log[last_search.last_c])
print_LogEnt(master_log[x]);
break;
}
case 'k': {
for (auto x : last_search.last_k)
print_LogEnt(master_log[x]);
break;
}
}
break;
}
case 'r': {
// check for null or nothing
if (last_search.last_s == 'n')
break;
// for t, m, c, k
switch (last_search.last_s) {
case 't':
case 'm': {
auto beg = last_search.itr_b;
auto end = last_search.itr_e;
int ml_b = int(beg - master_log.begin());
int ml_e = int(end - master_log.begin());
for (int i = ml_b; i < ml_e; ++i)
expt_log.push_back(i);
std::cout << (ml_e - ml_b) << " log entries appended\n";
break;
}
case 'c': {
if (!last_search.found_c) {
std::cout << "0 log entries appended\n";
break;
}
for (auto x : ct_log[last_search.last_c])
expt_log.push_back(x);
std::cout << ct_log[last_search.last_c].size() << " log entries appended\n";
break;
}
case 'k': {
for (auto x : last_search.last_k)
expt_log.push_back(x);
std::cout << last_search.last_k.size() << " log entries appended\n";
break;
}
}
break;
}
case 'd': {
size_t pos_delete;
std::cin >> pos_delete;
if (pos_delete >= expt_log.size())
break;
expt_log.erase(expt_log.begin() + pos_delete);
std::cout << "Deleted excerpt list entry " << pos_delete << "\n";
break;
}
case 'b': {
size_t pos_beg;
std::cin >> pos_beg;
if (pos_beg >= expt_log.size())
break;
int tmp = expt_log[pos_beg];
expt_log.erase(expt_log.begin() + pos_beg);
expt_log.insert(expt_log.begin(), tmp);
std::cout << "Moved excerpt list entry " << pos_beg << "\n";
break;
}
case 'e': {
size_t pos_end;
std::cin >> pos_end;
if (pos_end >= expt_log.size())
break;
int tmp = expt_log[pos_end];
expt_log.erase(expt_log.begin() + pos_end);
expt_log.push_back(tmp);
std::cout << "Moved excerpt list entry " << pos_end << "\n";
break;
}
case 's': {
std::cout << "excerpt list sorted\n";
if (expt_log.empty()) {
std::cout << "(previously empty)\n";
break;
}
// prev ordering
std::cout << "previous ordering:\n";
std::cout << "0|";
print_LogEnt(master_log[expt_log[0]]);
std::cout << "...\n";
std::cout << expt_log.size() - 1 << "|";
print_LogEnt(master_log[expt_log[expt_log.size() - 1]]);
// sort
std::sort(expt_log.begin(), expt_log.end());
// new ordering
std::cout << "new ordering:\n";
std::cout << "0|";
print_LogEnt(master_log[expt_log[0]]);
std::cout << "...\n";
std::cout << expt_log.size() - 1 << "|";
print_LogEnt(master_log[expt_log[expt_log.size() - 1]]);
break;
}
case 'l': {
std::cout << "excerpt list cleared\n";
if (expt_log.empty()) {
std::cout << "(previously empty)\n";
break;
}
// prev contents
std::cout << "previous contents:\n";
std::cout << "0|";
print_LogEnt(master_log[expt_log[0]]);
std::cout << "...\n";
std::cout << expt_log.size() - 1 << "|";
print_LogEnt(master_log[expt_log[expt_log.size() - 1]]);
//clear excerpt deque
expt_log.clear();
break;
}
case 'k': {
last_search.last_s = cmd;
string keyword;
std::getline(cin >> std::ws, keyword);
std::transform(keyword.begin(), keyword.end(), keyword.begin(), ::tolower);
std::vector<string> key_vec;
parse_str(keyword, key_vec);
// first check if value is valid
if (key_vec.empty()) {
last_search.last_k.clear();
std::cout << "Keyword search: 0 entries found\n";
break;
}
// then see if its in there
if (kw_log.find(key_vec[0]) == kw_log.end()) {
last_search.last_k.clear();
std::cout << "Keyword search: 0 entries found\n";
break;
}
last_search.last_k = kw_log[key_vec[0]];
std::vector<int32_t> tmp;
for (size_t i = 1; i < key_vec.size(); ++i) {
if (kw_log.find(key_vec[i]) == kw_log.end()) {
last_search.last_k.clear();
break;
}
tmp = last_search.last_k;
last_search.last_k.clear();
std::set_intersection(tmp.begin(), tmp.end(),
kw_log[key_vec[i]].begin(), kw_log[key_vec[i]].end(),
std::back_inserter(last_search.last_k));
}
std::cout << "Keyword search: " << last_search.last_k.size() << " entries found\n";
break;
}
case '#': {
string junk;
getline(cin, junk);
break;
}
}
} while (cmd != 'q');
for (auto x : master_log)
delete x;
return 0;
}