feat: add debug startup logging

This commit is contained in:
Andrey Kutin 2026-10-25 12:43:24 +03:00
parent 151676df2b
commit f15bf41bfe
2 changed files with 72 additions and 18 deletions

View File

@ -43,6 +43,15 @@ cd ..
./build/bin/cards /path/to/config.json ./build/bin/cards /path/to/config.json
``` ```
Для подробного вывода этапов запуска и итоговой конфигурации используйте флаг `--debug`. Пароль базы данных в таком выводе не раскрывается.
```bash
./build/bin/cards --debug
./build/bin/cards --debug /path/to/config.json
```
Аргументы можно передавать в любом порядке. Программа принимает не более одного пути к файлу конфигурации.
### Поля `config.json` ### Поля `config.json`
**server** **server**

View File

@ -2,6 +2,7 @@
#include <exception> #include <exception>
#include <csignal> #include <csignal>
#include <atomic> #include <atomic>
#include <string>
#include "config.hpp" #include "config.hpp"
#include "database.hpp" #include "database.hpp"
@ -18,52 +19,97 @@ void signalHandler(int signal) {
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
try { try {
// Загрузка конфигурации
std::string configFile = "config.json"; std::string configFile = "config.json";
if (argc > 1) { bool debug = false;
configFile = argv[1]; bool configFileSpecified = false;
for (int i = 1; i < argc; ++i) {
const std::string argument = argv[i];
if (argument == "--debug") {
debug = true;
} else if (argument.starts_with("--")) {
throw std::invalid_argument("Unknown option: " + argument);
} else if (configFileSpecified) {
throw std::invalid_argument("Only one configuration file can be specified");
} else {
configFile = argument;
configFileSpecified = true;
}
} }
if (debug) {
std::cout << "[debug] Debug mode enabled" << std::endl;
std::cout << "[debug] Configuration file: " << configFile << std::endl;
}
std::cout << "Loading configuration from: " << configFile << std::endl; std::cout << "Loading configuration from: " << configFile << std::endl;
AppConfig config = AppConfig::loadFromFile(configFile); AppConfig config = AppConfig::loadFromFile(configFile);
// Инициализация базы данных if (debug) {
std::cout << "[debug] Server configuration: address=" << config.server.address
<< ", port=" << config.server.port
<< ", threads=" << config.server.threads << std::endl;
std::cout << "[debug] Database configuration: host=" << config.database.host
<< ", port=" << config.database.port
<< ", dbname=" << config.database.dbname
<< ", user=" << config.database.user
<< ", password=" << (config.database.password.empty() ? "not set" : "set")
<< std::endl;
std::cout << "[debug] Initializing database connection pool" << std::endl;
}
std::cout << "Connecting to database..." << std::endl; std::cout << "Connecting to database..." << std::endl;
auto db = std::make_shared<Database>(config.database); auto db = std::make_shared<Database>(config.database);
std::cout << "Database connected successfully!" << std::endl; std::cout << "Database connected successfully!" << std::endl;
// Инициализация и запуск сервера if (debug) {
std::cout << "[debug] Creating HTTP server" << std::endl;
}
Server server( Server server(
config.server.address, config.server.address,
config.server.port, config.server.port,
config.server.threads, config.server.threads,
db db
); );
// Установка обработчиков сигналов
std::signal(SIGINT, signalHandler); std::signal(SIGINT, signalHandler);
std::signal(SIGTERM, signalHandler); std::signal(SIGTERM, signalHandler);
if (debug) {
std::cout << "[debug] SIGINT and SIGTERM handlers installed" << std::endl;
}
std::cout << "Server is running on " std::cout << "Server is running on "
<< config.server.address << ":" << config.server.address << ":"
<< config.server.port << config.server.port
<< " with " << config.server.threads << " threads" << std::endl; << " with " << config.server.threads << " threads" << std::endl;
std::cout << "Press Ctrl+C to stop" << std::endl; std::cout << "Press Ctrl+C to stop" << std::endl;
// Запуск сервера в отдельном потоке
std::thread serverThread([&server]() { std::thread serverThread([&server]() {
server.run(); server.run();
}); });
// Ожидание сигнала остановки if (debug) {
std::cout << "[debug] Server thread started" << std::endl;
}
while (!stopServer) { while (!stopServer) {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
} }
if (debug) {
std::cout << "[debug] Stopping I/O context" << std::endl;
}
std::cout << "Stopping server..." << std::endl; std::cout << "Stopping server..." << std::endl;
server.stop(); server.stop();
serverThread.join(); serverThread.join();
if (debug) {
std::cout << "[debug] Server thread joined" << std::endl;
}
std::cout << "Server stopped successfully!" << std::endl; std::cout << "Server stopped successfully!" << std::endl;
} catch (const std::exception& e) { } catch (const std::exception& e) {
@ -73,4 +119,3 @@ int main(int argc, char* argv[]) {
return 0; return 0;
} }