Repo-Init
 
Loading...
Searching...
No Matches
main.cpp File Reference
#include "logging/Logger.hpp"
#include "metrics/ProcessMetrics.hpp"
#include "metrics/PrometheusServer.hpp"
#include "telnet/TelnetServer.hpp"
#include "utils/ConfigParser.hpp"
#include "utils/ErrorHelpers.hpp"
#include "utils/InputParser.hpp"
#include "utils/Tracer.hpp"
#include "zeromq/ZeroMQServer.hpp"
#include <curl/curl.h>
#include <spdlog/spdlog.h>
#include <csignal>
Include dependency graph for main.cpp:

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Variables

constexpr uintmax_t alarmInterval = 30
 

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 37 of file main.cpp.

38{
39 const InputParser input(argc, argv);
40 const ConfigParser config(input.cmdOptionExists("--config") ? input.getCmdOption("--config") : "config.json");
41 const MainLogger logger(config.get("LOKI_ADDRESS"), config.get("SENTRY_ADDRESS"));
42
43 // Initialize curl as soon as possible
44 if (curl_global_init(CURL_GLOBAL_DEFAULT) < 0)
45 {
46 spdlog::critical("Can't init curl");
47 return EXIT_FAILURE;
48 }
49
50 // Adjust log level
51 if (input.cmdOptionExists("-v"))
52 {
53 spdlog::set_level(spdlog::level::info);
54 }
55 if (input.cmdOptionExists("-vv"))
56 {
57 spdlog::set_level(spdlog::level::debug);
58 }
59 if (input.cmdOptionExists("-vvv"))
60 {
61 spdlog::set_level(spdlog::level::trace);
62 }
63
64 // Log input arguments
65 spdlog::debug("======== Detected input arguments ========");
66 for (const auto &entry : input.getCmdOptions())
67 {
68 spdlog::debug("{} = {}", entry.first, entry.second);
69 }
70
71 // Log detected configuration to debug
72 spdlog::debug("======== Detected configuration ========");
73 for (const auto &entry : config.getConfigMap())
74 {
75 spdlog::debug("{} = {}", entry.first, entry.second);
76 }
77
78 // Register interrupt signal handler
79 if (std::signal(SIGINT, interruptFunc) == SIG_ERR)
80 {
81 spdlog::critical("Can't set signal handler (SIGINT): {}", getErrnoString(errno));
82 return EXIT_FAILURE;
83 }
84
85 // Register termination signal handler
86 if (std::signal(SIGTERM, interruptFunc) == SIG_ERR)
87 {
88 spdlog::critical("Can't set signal handler (SIGTERM): {}", getErrnoString(errno));
89 return EXIT_FAILURE;
90 }
91
92 // Register alarm signal handler
93 if (std::signal(SIGALRM, alarmFunc) == SIG_ERR)
94 {
95 spdlog::critical("Can't set signal handler (SIGALRM): {}", getErrnoString(errno));
96 return EXIT_FAILURE;
97 }
98 alarm(alarmInterval);
99
100 // Initialize Crashpad handler
101 std::unique_ptr<Tracer> crashpadController(nullptr);
102 vCheckFlag.emplace_back("Crashpad Handler", std::make_shared<std::atomic_flag>(false));
103 crashpadController = std::make_unique<Tracer>(
104 vCheckFlag[vCheckFlag.size() - 1].second, config.get("CRASHPAD_REMOTE"), config.get("CRASHPAD_PROXY"),
105 config.get("CRASHPAD_EXECUTABLE_PATH"), config.get("CRASHPAD_REPORT_DIR"));
106
107 // Initialize Prometheus server
108 std::unique_ptr<PrometheusServer> mainPrometheusServer(nullptr);
109 const std::string prometheusAddr = input.getCmdOption("--enable-prometheus");
110 if (!prometheusAddr.empty())
111 {
112 try
113 {
114 mainPrometheusServer = std::make_unique<PrometheusServer>(prometheusAddr);
115 spdlog::info("Prometheus server start at {}", prometheusAddr);
116 }
117 catch (const std::exception &e)
118 {
119 spdlog::error("Can't start Prometheus Server: {}", e.what());
120 return EXIT_FAILURE;
121 }
122 }
123
124 // Initialize self monitoring
125 std::unique_ptr<ProcessMetrics> selfMonitor(nullptr);
126 vCheckFlag.emplace_back("Self Monitor", std::make_shared<std::atomic_flag>(false));
127 if (mainPrometheusServer)
128 {
129 selfMonitor = std::make_unique<ProcessMetrics>(vCheckFlag[vCheckFlag.size() - 1].second,
130 mainPrometheusServer->createNewRegistry());
131 }
132
133 // Initialize ZeroMQ server
134 std::unique_ptr<ZeroMQServer> zmqController(nullptr);
135 vCheckFlag.emplace_back("ZeroMQ Server", std::make_shared<std::atomic_flag>(false));
136 const std::string zeromqServerAddr = input.getCmdOption("--enable-zeromq");
137 if (!zeromqServerAddr.empty())
138 {
139 try
140 {
141 zmqController = std::make_unique<ZeroMQServer>(
142 zeromqServerAddr, vCheckFlag[vCheckFlag.size() - 1].second,
143 mainPrometheusServer ? mainPrometheusServer->createNewRegistry() : nullptr);
144 zmqController->messageCallback(ZeroMQServerMessageCallback);
145 zmqController->initialise();
146 }
147 catch (const std::exception &e)
148 {
149 spdlog::error("Can't start ZeroMQ Server: {}", e.what());
150 return EXIT_FAILURE;
151 }
152 }
153
154 // Initialize Telnet server
155 std::unique_ptr<TelnetServer> telnetController(nullptr);
156 vCheckFlag.emplace_back("Telnet Server", std::make_shared<std::atomic_flag>(false));
157 const unsigned long telnetPort =
158 input.cmdOptionExists("--enable-telnet") ? std::stoul(input.getCmdOption("--enable-telnet")) : 0;
159 if (telnetPort > 0 && telnetPort < 65536)
160 {
161 try
162 {
163 telnetController = std::make_unique<TelnetServer>();
164 telnetController->connectedCallback(TelnetConnectedCallback);
165 telnetController->newLineCallback(TelnetMessageCallback);
166 telnetController->tabCallback(TelnetTabCallback);
167 telnetController->initialise(telnetPort, vCheckFlag[vCheckFlag.size() - 1].second, "> ",
168 mainPrometheusServer ? mainPrometheusServer->createNewRegistry() : nullptr);
169 }
170 catch (const std::exception &e)
171 {
172 spdlog::error("Can't start Telnet Server: {}", e.what());
173 return EXIT_FAILURE;
174 }
175 }
176 else if (telnetPort != 0)
177 {
178 spdlog::error("Invalid Telnet port: {}", telnetPort);
179 return EXIT_FAILURE;
180 }
181
182 /* ################################################################################### */
183 /* ############################# MAKE MODIFICATIONS HERE ############################# */
184 /* ################################################################################### */
185
186 /* ################################################################################### */
187 /* ################################ END MODIFICATIONS ################################ */
188 /* ################################################################################### */
189
190 while (interruptFlag == 0)
191 {
192 std::this_thread::sleep_for(std::chrono::milliseconds(500));
193 }
194
195 /* ################################################################################### */
196 /* ############################# MAKE MODIFICATIONS HERE ############################# */
197 /* ################################################################################### */
198
199 /* ################################################################################### */
200 /* ################################ END MODIFICATIONS ################################ */
201 /* ################################################################################### */
202
203 curl_global_cleanup();
204
205 return EXIT_SUCCESS;
206}
std::vector< std::pair< std::string, std::shared_ptr< std::atomic_flag > > > vCheckFlag
Global variable to check if the servers are running.
std::string getErrnoString(int errVal)
void TelnetConnectedCallback(const SP_TelnetSession &session)
std::string TelnetTabCallback(const SP_TelnetSession &session, std::string_view line)
bool TelnetMessageCallback(const SP_TelnetSession &session, const std::string &line)
bool ZeroMQServerMessageCallback(const std::vector< zmq::message_t > &recvMsgs, std::vector< zmq::message_t > &replyMsgs)
constexpr uintmax_t alarmInterval
Definition main.cpp:17
Here is the call graph for this function:

Variable Documentation

◆ alarmInterval

uintmax_t alarmInterval = 30
constexpr

Definition at line 17 of file main.cpp.