feat: add debug startup logging
This commit is contained in:
parent
151676df2b
commit
f15bf41bfe
|
|
@ -43,6 +43,15 @@ cd ..
|
|||
./build/bin/cards /path/to/config.json
|
||||
```
|
||||
|
||||
Для подробного вывода этапов запуска и итоговой конфигурации используйте флаг `--debug`. Пароль базы данных в таком выводе не раскрывается.
|
||||
|
||||
```bash
|
||||
./build/bin/cards --debug
|
||||
./build/bin/cards --debug /path/to/config.json
|
||||
```
|
||||
|
||||
Аргументы можно передавать в любом порядке. Программа принимает не более одного пути к файлу конфигурации.
|
||||
|
||||
### Поля `config.json`
|
||||
|
||||
**server**
|
||||
|
|
|
|||
81
src/main.cpp
81
src/main.cpp
|
|
@ -2,6 +2,7 @@
|
|||
#include <exception>
|
||||
#include <csignal>
|
||||
#include <atomic>
|
||||
#include <string>
|
||||
|
||||
#include "config.hpp"
|
||||
#include "database.hpp"
|
||||
|
|
@ -18,52 +19,97 @@ void signalHandler(int signal) {
|
|||
|
||||
int main(int argc, char* argv[]) {
|
||||
try {
|
||||
// Загрузка конфигурации
|
||||
std::string configFile = "config.json";
|
||||
if (argc > 1) {
|
||||
configFile = argv[1];
|
||||
bool debug = false;
|
||||
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;
|
||||
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;
|
||||
auto db = std::make_shared<Database>(config.database);
|
||||
std::cout << "Database connected successfully!" << std::endl;
|
||||
|
||||
// Инициализация и запуск сервера
|
||||
|
||||
if (debug) {
|
||||
std::cout << "[debug] Creating HTTP server" << std::endl;
|
||||
}
|
||||
|
||||
Server server(
|
||||
config.server.address,
|
||||
config.server.port,
|
||||
config.server.threads,
|
||||
db
|
||||
);
|
||||
|
||||
// Установка обработчиков сигналов
|
||||
|
||||
std::signal(SIGINT, signalHandler);
|
||||
std::signal(SIGTERM, signalHandler);
|
||||
|
||||
|
||||
if (debug) {
|
||||
std::cout << "[debug] SIGINT and SIGTERM handlers installed" << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Server is running on "
|
||||
<< config.server.address << ":"
|
||||
<< config.server.port
|
||||
<< " with " << config.server.threads << " threads" << std::endl;
|
||||
std::cout << "Press Ctrl+C to stop" << std::endl;
|
||||
|
||||
// Запуск сервера в отдельном потоке
|
||||
|
||||
std::thread serverThread([&server]() {
|
||||
server.run();
|
||||
});
|
||||
|
||||
// Ожидание сигнала остановки
|
||||
|
||||
if (debug) {
|
||||
std::cout << "[debug] Server thread started" << std::endl;
|
||||
}
|
||||
|
||||
while (!stopServer) {
|
||||
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;
|
||||
server.stop();
|
||||
serverThread.join();
|
||||
|
||||
|
||||
if (debug) {
|
||||
std::cout << "[debug] Server thread joined" << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Server stopped successfully!" << std::endl;
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
|
|
@ -73,4 +119,3 @@ int main(int argc, char* argv[]) {
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue