1
0

init.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <clocale>
  2. #include "init.hpp"
  3. using namespace aFuncore;
  4. using namespace aFuntool;
  5. namespace aFuncore {
  6. std::string log_path;
  7. std::string lang_path;
  8. std::string varlib_path;
  9. aFuntool::Logger *aFunCoreLogger;
  10. };
  11. /**
  12. * 初始化程序
  13. * @param info 初始化信息
  14. * @return 是否初始化成功
  15. */
  16. bool aFuncore::aFunCoreInit(aFuncore::InitInfo *info) {
  17. if (info == nullptr) {
  18. static InitInfo info_default = {.base_dir=".",
  19. .log_asyn=true,
  20. .level=log_info};
  21. info = &info_default;
  22. }
  23. getEndian();
  24. if (setlocale(LC_ALL, "") == nullptr)
  25. return false;
  26. if (info->base_dir.empty())
  27. return false;
  28. log_path = info->base_dir + SEP + aFunLogDir + SEP;
  29. lang_path = info->base_dir + SEP + aFunLangDir + SEP;
  30. varlib_path = info->base_dir + SEP + aFunVarLibDir + SEP;
  31. std::string log = log_path + "aFunlang";
  32. bool re = log_factory.initLogSystem(log, info->log_asyn);
  33. if (re == 0)
  34. return false;
  35. static aFuntool::Logger logger {"aFunlang-core", info->level};
  36. aFuncore::aFunCoreLogger = &logger;
  37. debugLog(aFunCoreLogger, "aFunCore log path: %s", log_path.c_str());
  38. debugLog(aFunCoreLogger, "aFunCore var.lib path: %s", varlib_path.c_str());
  39. debugLog(aFunCoreLogger, "aFunCore lang path: %s", lang_path.c_str());
  40. char LANG_path[218] = {0};
  41. snprintf(LANG_path, 218, "%sLANG", lang_path.c_str());
  42. FILE *LANG_file = fileOpen(LANG_path, "r");
  43. if (LANG_file != nullptr) {
  44. char LANG[100] = {0};
  45. fgets(LANG, 100, LANG_file);
  46. if (LANG[strlen(LANG) - 1] == '\n')
  47. LANG[strlen(LANG) - 1] = NUL; // 去除`\n`
  48. debugLog(aFunCoreLogger, "language = %s", LANG);
  49. char LANG_lib[218] = {0};
  50. std::string tmp = std::string("%s") + SHARED_PREFIX + "%s" + SHARED_SUFFIX;
  51. snprintf(LANG_lib, 218, tmp.c_str(), lang_path.c_str(), LANG);
  52. if (HT_initaFunGetText(LANG_lib) == 0)
  53. debugLog(aFunCoreLogger, "aFunCore lang init success: %s", LANG_lib);
  54. else
  55. debugLog(aFunCoreLogger, "aFunCore lang init failed: %s", LANG_lib);
  56. fileClose(LANG_file);
  57. } else
  58. HT_initaFunGetText(nullptr);
  59. debugLog(aFunCoreLogger, "aFunCore init success");
  60. return true;
  61. }