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 /* ################################################################################### */
51 /* ############################# MAKE MODIFICATIONS HERE ############################# */
52 /* ################################################################################### */
53
54 /* ################################################################################### */
55 /* ################################ END MODIFICATIONS ################################ */
56 /* ################################################################################### */
57
58 // Adjust log level
59 if (input.cmdOptionExists("-v"))
60 {
61 spdlog::set_level(spdlog::level::info);
62 }
63 if (input.cmdOptionExists("-vv"))
64 {
65 spdlog::set_level(spdlog::level::debug);
66 }
67 if (input.cmdOptionExists("-vvv"))
68 {
69 spdlog::set_level(spdlog::level::trace);
70 }
71
72 /* ################################################################################### */
73 /* ############################# MAKE MODIFICATIONS HERE ############################# */
74 /* ################################################################################### */
75
76 /* ################################################################################### */
77 /* ################################ END MODIFICATIONS ################################ */
78 /* ################################################################################### */
79
80 // Log input arguments
81 spdlog::debug("======== Detected input arguments ========");
82 for (const auto &entry : input.getCmdOptions())
83 {
84 spdlog::debug("{} = {}", entry.first, entry.second);
85 }
86
87 // Log detected configuration to debug
88 spdlog::debug("======== Detected configuration ========");
89 for (const auto &entry : config.getConfigMap())
90 {
91 spdlog::debug("{} = {}", entry.first, entry.second);
92 }
93
94 // Register interrupt signal handler
95 if (std::signal(SIGINT, interruptFunc) == SIG_ERR)
96 {
97 spdlog::critical("Can't set signal handler (SIGINT): {}", getErrnoString(errno));
98 return EXIT_FAILURE;
99 }
100
101 // Register termination signal handler
102 if (std::signal(SIGTERM, interruptFunc) == SIG_ERR)
103 {
104 spdlog::critical("Can't set signal handler (SIGTERM): {}", getErrnoString(errno));
105 return EXIT_FAILURE;
106 }
107
108 // Register alarm signal handler
109 if (std::signal(SIGALRM, alarmFunc) == SIG_ERR)
110 {
111 spdlog::critical("Can't set signal handler (SIGALRM): {}", getErrnoString(errno));
112 return EXIT_FAILURE;
113 }
114 alarm(alarmInterval);
115
116 // Initialize Crashpad handler
117 std::unique_ptr<Tracer> crashpadController(nullptr);
118 vCheckFlag.emplace_back("Crashpad Handler", std::make_shared<std::atomic_flag>(false));
119 crashpadController = std::make_unique<Tracer>(
120 vCheckFlag[vCheckFlag.size() - 1].second, config.get("CRASHPAD_REMOTE"), config.get("CRASHPAD_PROXY"),
121 config.get("CRASHPAD_EXECUTABLE_PATH"), config.get("CRASHPAD_REPORT_DIR"));
122
123 // Initialize Prometheus server
124 std::unique_ptr<PrometheusServer> mainPrometheusServer(nullptr);
125 const std::string prometheusAddr = input.getCmdOption("--enable-prometheus");
126 if (!prometheusAddr.empty())
127 {
128 try
129 {
130 mainPrometheusServer = std::make_unique<PrometheusServer>(prometheusAddr);
131 spdlog::info("Prometheus server start at {}", prometheusAddr);
132 }
133 catch (const std::exception &e)
134 {
135 spdlog::error("Can't start Prometheus Server: {}", e.what());
136 return EXIT_FAILURE;
137 }
138 }
139
140 // Initialize self monitoring
141 std::unique_ptr<ProcessMetrics> selfMonitor(nullptr);
142 vCheckFlag.emplace_back("Self Monitor", std::make_shared<std::atomic_flag>(false));
143 if (mainPrometheusServer)
144 {
145 selfMonitor = std::make_unique<ProcessMetrics>(vCheckFlag[vCheckFlag.size() - 1].second,
146 mainPrometheusServer->createNewRegistry());
147 }
148
149 // Initialize ZeroMQ server
150 std::unique_ptr<ZeroMQServer> zmqController(nullptr);
151 vCheckFlag.emplace_back("ZeroMQ Server", std::make_shared<std::atomic_flag>(false));
152 const std::string zeromqServerAddr = input.getCmdOption("--enable-zeromq");
153 if (!zeromqServerAddr.empty())
154 {
155 try
156 {
157 zmqController = std::make_unique<ZeroMQServer>(
158 zeromqServerAddr, vCheckFlag[vCheckFlag.size() - 1].second,
159 mainPrometheusServer ? mainPrometheusServer->createNewRegistry() : nullptr);
160 zmqController->messageCallback(ZeroMQServerMessageCallback);
161 zmqController->initialise();
162 }
163 catch (const std::exception &e)
164 {
165 spdlog::error("Can't start ZeroMQ Server: {}", e.what());
166 return EXIT_FAILURE;
167 }
168 }
169
170 // Initialize Telnet server
171 std::shared_ptr<TelnetServer> telnetController(nullptr);
172 vCheckFlag.emplace_back("Telnet Server", std::make_shared<std::atomic_flag>(false));
173 const unsigned long telnetPort =
174 input.cmdOptionExists("--enable-telnet") ? std::stoul(input.getCmdOption("--enable-telnet")) : 0;
175 if (telnetPort > 0 && telnetPort < 65536)
176 {
177 try
178 {
179 telnetController = std::make_shared<TelnetServer>();
180 telnetController->connectedCallback(TelnetConnectedCallback);
181 telnetController->newLineCallback(TelnetMessageCallback);
182 telnetController->tabCallback(TelnetTabCallback);
183 telnetController->initialise(telnetPort, vCheckFlag[vCheckFlag.size() - 1].second, "> ",
184 mainPrometheusServer ? mainPrometheusServer->createNewRegistry() : nullptr);
185 }
186 catch (const std::exception &e)
187 {
188 spdlog::error("Can't start Telnet Server: {}", e.what());
189 return EXIT_FAILURE;
190 }
191 }
192 else if (telnetPort != 0)
193 {
194 spdlog::error("Invalid Telnet port: {}", telnetPort);
195 return EXIT_FAILURE;
196 }
197
198 /* ################################################################################### */
199 /* ############################# MAKE MODIFICATIONS HERE ############################# */
200 /* ################################################################################### */
201
202 /* ################################################################################### */
203 /* ################################ END MODIFICATIONS ################################ */
204 /* ################################################################################### */
205
206 while (interruptFlag == 0)
207 {
208 std::this_thread::sleep_for(std::chrono::milliseconds(500));
209 }
210
211 /* ################################################################################### */
212 /* ############################# MAKE MODIFICATIONS HERE ############################# */
213 /* ################################################################################### */
214
215 /* ################################################################################### */
216 /* ################################ END MODIFICATIONS ################################ */
217 /* ################################################################################### */
218
219 curl_global_cleanup();
220
221 return EXIT_SUCCESS;
222}
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.