main.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "app.hpp"
  2. #include <smartbotic/microbit/common/config_loader.hpp>
  3. #include <smartbotic/microbit/common/logging.hpp>
  4. #include <spdlog/spdlog.h>
  5. #include <csignal>
  6. #include <filesystem>
  7. #include <iostream>
  8. namespace {
  9. std::atomic<bool> g_running{true};
  10. void signalHandler(int) { g_running = false; }
  11. }
  12. int main(int argc, char* argv[]) {
  13. std::filesystem::path configPath = "config/microbit.json";
  14. for (int i = 1; i < argc; ++i) {
  15. std::string arg = argv[i];
  16. if ((arg == "--config" || arg == "-c") && i + 1 < argc) {
  17. configPath = argv[++i];
  18. } else if (arg == "--help" || arg == "-h") {
  19. std::cout << "Usage: smartbotic-microbit [options]\n"
  20. << " --config, -c <path> Configuration file path (default: config/microbit.json)\n"
  21. << " --help, -h Show this help\n";
  22. return 0;
  23. }
  24. }
  25. try {
  26. auto config = smartbotic::microbit::ConfigLoader::loadFromFile(configPath);
  27. auto logLevel = spdlog::level::from_str(
  28. smartbotic::microbit::ConfigLoader::get<std::string>(config, "log_level", "info"));
  29. smartbotic::microbit::logging::init("microbit", logLevel);
  30. std::signal(SIGINT, signalHandler);
  31. std::signal(SIGTERM, signalHandler);
  32. std::signal(SIGPIPE, SIG_IGN);
  33. smartbotic::microbit::App app(config);
  34. app.start();
  35. while (g_running && app.isRunning()) {
  36. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  37. }
  38. app.stop();
  39. } catch (const std::exception& e) {
  40. spdlog::error("Fatal error: {}", e.what());
  41. return 1;
  42. }
  43. return 0;
  44. }