-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
83 lines (75 loc) · 3.04 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
#include "src/helpers/helper_functions.h"
#include "src/helpers/logger.h"
#include "src/input_handler.h"
#include "src/spa.h"
#include <iostream>
int VerifyArguments(const bool incorrectArgs, const int argc, const CInputHandler::SUserArgs& userArgs);
/**
* @brief Prints info when a wrong command is inserted or with parameter -h or --help
*/
void print_info()
{
std::cout << "Execute this application using ./spa -c [config file]\n\n"
<< "Usage: ./spa [OPTION [ARG]] ...\n"
<< " -h, --help\t\t show this help statement\n"
<< " -c, --config [config file]\t\t use the [config file] for the "
"measurements (must be in the JSON format, read documentation for the "
"exact format or use the example)\n"
<< " -s --sensors [sensorconfig]\t\t use the [sensorconfig file] for the "
"path and sensor information (must be in the JSON format, read "
"documentation for the exact format or use the example, by default "
"\"sensor_config.json\")\n"
<< " -l --log [loglevel]\t\t enables logging for the tool, [loglevel]: "
"0: only error and warnings, 1: info logging, 2: debug logging, 3: "
"all loggings\n\n"
<< "NOTE: OPTION --config (or -c) is required for the correct JSON "
"configuration!\n"
<< std::endl;
}
/**
* @brief The main function of the application
*/
int main(int argc, char* argv[])
{
// Input checks using the CInputHandler
CInputHandler inputHandler;
bool incorrectArgs = inputHandler.Parse(argc, argv);
auto userArgs = inputHandler.GetUserArguments();
if (VerifyArguments(incorrectArgs, argc, userArgs) == -1)
return -1; // Terminate application, arguments are incorrect
// Enabling/disabling logger components (can be enabled using terminal arguments)
CLogger::Enable(userArgs.enableInfoLog, userArgs.enableDebugLog);
// Parsing user configuration
auto config = Core::ConfigParser::Parse(userArgs.configFile);
CLogger::Log(CLogger::Types::INFO, "Started application");
CSystemPerformanceAnalyzer spa{ userArgs.configFile, userArgs.sensorInfoFile };
// Start the actual execution of the configured tasks and measurements
spa.StartExecution();
}
/**
* @brief Verifies the arguments
*
* @return int(-1): incorrect arguments
* int(0): correct arguments
*/
int VerifyArguments(const bool incorrectArgs, const int argc, const CInputHandler::SUserArgs& userArgs)
{
if (incorrectArgs || argc < 2 || userArgs.configFile.empty())
{
print_info();
return -1;
}
if (!Helpers::FileExists(userArgs.sensorInfoFile))
{
std::cout << "Sensor config file not found: " << userArgs.sensorInfoFile << std::endl;
print_info();
return -1;
}
else if (!Helpers::FileExists(userArgs.configFile))
{
std::cout << "Config file not found: " << userArgs.configFile << std::endl;
print_info();
return -1;
}
return 0; // Arguments are correct
}