-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.cpp
211 lines (169 loc) · 4.96 KB
/
main.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
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
/*
* cpu-stat - a simple program to get CPU stats on Linux
*
* by Davide Coppola - http://www.davidecoppola.com
*
* 0.00.01 19-Dec-2016 First basic version.
* 0.00.02 21-Dec-2016 New code design.
* 0.00.03 22-Dec-2016 Introduced options.
*
*/
#include "CPUSnapshot.h"
#include "CPUStatsPrinter.h"
#include <chrono>
#include <iostream>
#include <string>
#include <stdexcept>
#include <thread>
// -- PROGRAM DATA --
const char * STR_APP_NAME = "cpp-stat";
const char * STR_APP_VERSION = "0.00.03";
// -- SHORT OPTIONS --
const char * STR_OPT_ALL = "-a";
const char * STR_OPT_CPU = "-c";
const char * STR_OPT_DELAY = "-d";
const char * STR_OPT_HELP = "-h";
const char * STR_OPT_PRECISION = "-p";
const char * STR_OPT_VERBOSE = "-v";
// -- LONG OPTIONS --
const char * STR_LONGOPT_ALL = "--all";
const char * STR_LONGOPT_HELP = "--help";
const char * STR_LONGOPT_VERSION = "--version";
// -- ERRORS --
const char * STR_ERR = "ERROR - ";
const char * STR_ERR_PARAM = "wrong/missing parameter";
const char * STR_ERR_UNKNOWN_OPT = "unknown option.";
void PrintHelp();
void PrintUsage();
void PrintVersion();
int main(int argc, char * argv[])
{
// -- PARSE OPTIONS --
bool optPrintAll = false;
int optDelay = 100;
const int MIN_DELAY = 33;
unsigned int optPrecision = 2;
bool optVerbose = false;
int optCPU = -1;
// skip program name
int index = 1;
while(index < argc)
{
std::string arg(argv[index]);
if(STR_OPT_ALL == arg || arg == STR_LONGOPT_ALL)
optPrintAll = true;
else if(STR_OPT_DELAY == arg)
{
std::string param(argv[++index]);
try
{
optDelay = std::stoi(param);
if(optDelay < MIN_DELAY)
optDelay = MIN_DELAY;
}
catch(std::logic_error e)
{
std::cout << STR_ERR << STR_ERR_PARAM << std::endl;
PrintUsage();
return 1;
}
}
else if(STR_OPT_PRECISION == arg)
{
std::string param(argv[++index]);
try
{
optPrecision = std::stoi(param);
}
catch(std::logic_error e)
{
std::cout << STR_ERR << STR_ERR_PARAM << std::endl;
PrintUsage();
return 1;
}
}
else if(STR_OPT_CPU == arg)
{
std::string param(argv[++index]);
try
{
optCPU = std::stoi(param);
}
catch(std::logic_error e)
{
std::cout << STR_ERR << STR_ERR_PARAM << std::endl;
PrintUsage();
return 1;
}
}
else if(STR_OPT_VERBOSE == arg)
optVerbose = true;
else if(STR_OPT_HELP == arg || STR_LONGOPT_HELP == arg)
{
PrintHelp();
return 0;
}
else if(STR_LONGOPT_VERSION == arg)
{
PrintVersion();
return 0;
}
else
{
std::cout << STR_ERR << STR_ERR_UNKNOWN_OPT << std::endl;
PrintUsage();
return 1;
}
++index;
}
// -- GET SNAPSHOTS --
// snapshot 1
CPUSnapshot s1;
// 100ms pause
std::this_thread::sleep_for(std::chrono::milliseconds(optDelay));
// snapshot 2
CPUSnapshot s2;
// -- PRINT STATS --
CPUStatsPrinter printer(s1, s2);
printer.SetPrecision(optPrecision);
printer.SetVerbose(optVerbose);
if(optPrintAll)
printer.PrintActivePercentageAll();
else if(optCPU >= 0)
printer.PrintActivePercentageCPU(optCPU);
else
printer.PrintActivePercentageTotal();
return 0;
}
void PrintHelp()
{
PrintUsage();
const char * STR_LMARGIN = " ";
std::cout << std::endl;
std::cout << "launching the program with no option will print the active time percentage of the total CPU" << std::endl;
std::cout << std::endl;
std::cout << "PRINT OPTIONS" << std::endl;
std::cout << STR_LMARGIN << STR_OPT_ALL << " | " << STR_LONGOPT_ALL << "\t\t" << "print active time percentage for all CPUs, starting with total. " << std::endl;
std::cout << STR_LMARGIN << STR_OPT_CPU << " <cpu>" << "\t\t" << "print active time percentage only for selected CPU." << std::endl;
std::cout << STR_LMARGIN << STR_OPT_PRECISION << " <precision>" << "\t" << "set the deciaml precision of printed numbers. Default is 2." << std::endl;
std::cout << STR_LMARGIN << STR_OPT_VERBOSE << "\t\t\t" << "enable verbose mode." << std::endl;
std::cout << std::endl;
std::cout << "OTHER OPTIONS" << std::endl;
std::cout << STR_LMARGIN << STR_OPT_HELP << " | " << STR_LONGOPT_HELP << "\t\t" << "print this help." << std::endl;
std::cout << STR_LMARGIN << STR_LONGOPT_VERSION << "\t\t" << "print the version number" << std::endl;
std::cout << STR_LMARGIN << STR_OPT_DELAY << " <time>" << "\t\t" << "set delay time (in ms) between 2 snapshots of CPU data. Default is 100 ms." << std::endl;
std::cout << std::endl;
}
void PrintUsage()
{
const char * STR_LMARGIN = " ";
std::cout << "usage: " << STR_APP_NAME << " [" << STR_LONGOPT_VERSION << "] [" << STR_OPT_HELP << " | " << STR_LONGOPT_HELP
<< "] [" << STR_OPT_ALL << " | " << STR_LONGOPT_ALL << "] [" << STR_OPT_DELAY << " <time>] ["
<< STR_OPT_PRECISION << " <precision>]" << std::endl
<< STR_LMARGIN << "[" << STR_OPT_CPU << " <cpu>] [" << STR_OPT_VERBOSE << "]"
<< std::endl;
}
void PrintVersion()
{
std::cout << STR_APP_NAME << " - version " << STR_APP_VERSION << std::endl;
}