-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_logsGenerator.cpp
159 lines (127 loc) · 3.25 KB
/
test_logsGenerator.cpp
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
/******************************************************************************
* test_logsGenerator.cpp
*
* Test application to generate random logs.
* The number of logs can be specified.
* If no parameter is passed, it generates an 'infinite' number of logs.
*
* Copyright (C) 2012-2016 Pietro Mele
* Released under a GPL 3 license.
*
* pietrom16@gmail.com
*
*****************************************************************************/
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
std::string LogTime();
std::string LogDate();
int main(int argc, char* argv[])
{
bool endless = true;
int gen = 0, nGen = 4;
int nLogs = 10;
int pauseSec = 0;
if(argc > 1) {
const std::string arg(argv[1]);
if(arg == "-h" || arg == "--help") {
std::cout << argv[0] << " [nLogs] [distribution] [pause]\n"
<< "distribution values:\n"
<< " 0. Random"
<< " 1. Increasing"
<< " 2. Spikes"
<< " 3. Triangle wave"
<< std::endl;
exit(0);
}
nLogs = atoi(argv[1]);
endless = false;
}
if(argc > 2) {
gen = atoi(argv[2]);
if(gen >= nGen)
gen = 0;
}
if(argc > 3) {
pauseSec = atoi(argv[3]);
}
std::string log;
std::string logFile = "test.log";
std::ofstream ofs(logFile);
// Log levels (a custom levels enum can be used)
static const int nLogLevels = 8;
const char logLevelTags[nLogLevels][9] { " ---- ", "VeRbOsE ", "DETAIL ", "Info ", "warninG ", "error ", "CriticaL", "fATAl " };
int level = 0;
std::chrono::milliseconds pause(1000*pauseSec);
std::cout << "Writing test logs in file: " << logFile << " ..." << std::endl;
int i = 0;
while (endless || i < nLogs)
{
log = std::string("Test log message - ");
log += std::to_string(rand() % 1000);
log += std::string(" - Log id = ");
log += std::to_string(i);
if(gen == 0) { // Random
level = rand() % nLogLevels;
}
else if(gen == 1) { // Increasing
if(level >= nLogLevels)
level = 0;
else
++level;
}
else if(gen == 2) { // Spikes
if(i % 8 == 0)
level = 6;
else
level = 1;
}
else if(gen == 3) { // Triangle wave
level = abs((i % 14) - 7);
}
int format = rand() % 3;
switch (format) {
case 0:
ofs << LogTime() << LogDate() << logLevelTags[level] << " " << log << std::endl;
break;
case 1:
ofs << LogDate() << logLevelTags[level] << LogTime() << " " << log << std::endl;
break;
case 2:
ofs << logLevelTags[level] << LogTime() << LogDate() << " " << log << std::endl;
break;
}
std::cout << "." << std::flush;
std::this_thread::sleep_for(pause);
++i;
}
std::cout << "\n" << i << " test logs generated in file: " << logFile << std::endl;
return 0;
}
std::string LogTime()
{
float t = float(std::clock())/CLOCKS_PER_SEC;
const size_t sz = 16;
char ct[sz];
#ifndef _WIN32
std::snprintf(ct, sz, "% 7.3f ", t);
#else
_snprintf_s(ct, sz, "% 7.3f ", t);
#endif
return std::string(ct);
}
std::string LogDate() {
std::time_t t = std::time(nullptr);
char mbstr[32];
#ifndef _WIN32 //+B
std::strftime(mbstr, sizeof(mbstr), "%F %T ", std::localtime(&t));
#else
mbstr[0] = '\0';
#endif
return std::string(mbstr);
}