V0.36.3.250627_alpha:为XNEngine添加了规范的出错信息
This commit is contained in:
parent
c30b580a56
commit
cae0e9d7f6
BIN
Release/XNEngine
BIN
Release/XNEngine
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -237,7 +237,7 @@ bool XNScenarioManager::ParseConfig(const std::string &ConfigID)
|
||||
|
||||
// 执行查询
|
||||
if (sqlite3_step(stmt) != SQLITE_ROW) {
|
||||
LOG_ERROR("A01082014:未在数据库中找到配置ID为 %1 的记录", ConfigID);
|
||||
LOG_ERROR("A01082014:未在数据库中找到构型ID为 %1 的记录", ConfigID);
|
||||
sqlite3_finalize(stmt);
|
||||
sqlite3_close(db);
|
||||
return false;
|
||||
|
@ -63,35 +63,34 @@ bool XNEngine::ParseConfig(const std::string &XmlPath)
|
||||
if (index != std::string::npos) {
|
||||
std::string suffix = XmlPath.substr(index);
|
||||
if (suffix != ".xml" && suffix != ".sce") {
|
||||
std::cerr << "0x1005 配置文件不是 .xml 或 .sce 文件, 引擎将退出!" << std::endl;
|
||||
std::cerr << "A02012006: 构型配置文件不是 .xml 或 .sce 文件, 引擎将退出!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
std::cerr << "0x1006 配置文件没有 .xml 或 .sce 的后缀名, 引擎将退出!" << std::endl;
|
||||
std::cerr << "A02012007: 构型配置文件没有后缀名, 引擎将退出!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
// 打开配置文件
|
||||
std::ifstream file(XmlPath);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "0x1007 打开配置文件失败, 引擎将退出!" << std::endl;
|
||||
std::cerr << "C02012008: 打开构型配置文件失败, 引擎将退出!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
// 解析配置文件
|
||||
tinyxml2::XMLDocument doc;
|
||||
if (doc.LoadFile(XmlPath.c_str()) != tinyxml2::XML_SUCCESS) {
|
||||
std::cerr << "0x1008 解析配置文件失败, 引擎将退出!" << std::endl;
|
||||
std::cerr << "C02012009: 解析构型配置文件失败, 引擎将退出!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
// 获取根元素
|
||||
tinyxml2::XMLElement *root = doc.FirstChildElement("Scenario");
|
||||
if (!root) {
|
||||
std::cerr << "0x1009 配置文件中未找到 Scenario 根元素, 引擎将退出!" << std::endl;
|
||||
std::cerr << "A02012010: 构型配置文件中未找到 Scenario 根元素, 引擎将退出!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 顺便读取一下CPU亲和性
|
||||
int cpus = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
std::cout << "当前CPU核心数-> " << cpus << std::endl;
|
||||
|
||||
// 设置CPU亲和性
|
||||
cpu_set_t mask;
|
||||
@ -114,24 +113,24 @@ bool XNEngine::ParseConfig(const std::string &XmlPath)
|
||||
CPU_SET(index, &mask);
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "0x1010 无效的CPU亲和性值: " << cpuIndex << std::endl;
|
||||
std::cerr << "A02013011: 无效的CPU亲和性值: " << cpuIndex << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sched_setaffinity(0, sizeof(mask), &mask) == -1) {
|
||||
std::cerr << "0x1011 设置引擎CPU亲和性失败-> " << strerror(errno) << std::endl;
|
||||
std::cerr << "C02012012: 设置引擎CPU亲和性失败-> " << strerror(errno) << std::endl;
|
||||
return false;
|
||||
}
|
||||
std::cout << "成功设置引擎CPU亲和性-> " << CPUAffinity << std::endl;
|
||||
std::cout << "D02014013: 成功设置引擎CPU亲和性-> " << CPUAffinity << std::endl;
|
||||
|
||||
// 锁定内存
|
||||
if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
|
||||
std::cerr << "0x1012 锁定引擎内存失败-> " << strerror(errno) << std::endl;
|
||||
std::cerr << "C02012014: 锁定引擎内存失败-> " << strerror(errno) << std::endl;
|
||||
return false;
|
||||
}
|
||||
std::cout << "成功锁定引擎内存!" << std::endl;
|
||||
std::cout << "D02014015: 成功锁定引擎内存!" << std::endl;
|
||||
|
||||
// 获取配置文件中的日志元素
|
||||
bool isDebug = false;
|
||||
@ -141,7 +140,7 @@ bool XNEngine::ParseConfig(const std::string &XmlPath)
|
||||
// 获取配置文件中的控制台输出元素
|
||||
tinyxml2::XMLElement *consoleOutputNode = root->FirstChildElement("ConsoleOutput");
|
||||
if (!consoleOutputNode) {
|
||||
std::cout << "0x1013 配置文件中未找到 ConsoleOutput 元素, 控制台将输出所有日志!"
|
||||
std::cout << "A02013016: 构型配置文件中未找到 ConsoleOutput 元素, 控制台将输出所有日志!"
|
||||
<< std::endl;
|
||||
} else {
|
||||
// 获取配置文件中的调试日志输出元素
|
||||
@ -193,7 +192,7 @@ bool XNEngine::ParseConfig(const std::string &XmlPath)
|
||||
|
||||
tinyxml2::XMLElement *logNode = root->FirstChildElement("Log");
|
||||
if (!logNode) {
|
||||
std::cout << "0x1014 配置文件中未找到 Log 元素, 日志文件将记录所有日志!" << std::endl;
|
||||
std::cout << "A02013017: 配置文件中未找到 Log 元素, 日志文件将记录所有日志!" << std::endl;
|
||||
} else {
|
||||
// 获取配置文件中的调试日志输出元素
|
||||
const char *debugLog = logNode->Attribute("Debug");
|
||||
@ -434,7 +433,7 @@ bool XNEngine::Run(const std::string &XmlPath, const uint32_t InitializeType)
|
||||
bool ret = framework->Initialize(InitializeType);
|
||||
// 如果初始化失败
|
||||
if (!ret) {
|
||||
LOG_ERROR("0x1012 初始化失败, 引擎将退出!");
|
||||
LOG_ERROR("B02012018: 引擎初始化失败, 引擎将退出!");
|
||||
// 返回失败
|
||||
return false;
|
||||
}
|
||||
@ -442,18 +441,18 @@ bool XNEngine::Run(const std::string &XmlPath, const uint32_t InitializeType)
|
||||
// 设置框架状态
|
||||
frameworkStatus = XNFrameObjectStatus::Initialized;
|
||||
// 记录信息日志
|
||||
LOG_INFO("引擎初始化成功!");
|
||||
LOG_INFO("D02014019: 引擎初始化成功!");
|
||||
// 如果测试模式
|
||||
if (isTestMode) {
|
||||
// 记录信息日志
|
||||
LOG_INFO("引擎测试通过!");
|
||||
LOG_INFO("D02014020: 引擎测试通过!");
|
||||
// 返回成功
|
||||
return true;
|
||||
}
|
||||
ret = framework->PrepareForExecute();
|
||||
// 如果准备执行失败
|
||||
if (!ret) {
|
||||
LOG_ERROR("0x1013 准备执行失败, 引擎将退出!");
|
||||
LOG_ERROR("B02012021: 引擎准备执行失败, 引擎将退出!");
|
||||
// 返回失败
|
||||
return false;
|
||||
}
|
||||
@ -477,6 +476,7 @@ bool XNEngine::Run(const std::string &XmlPath, const uint32_t InitializeType)
|
||||
if (engineStatusWriter) {
|
||||
// 设置引擎运行标志
|
||||
engineRunning = true;
|
||||
//主线程循环发送运行状态
|
||||
while (engineRunning) {
|
||||
// 发布一次初始状态
|
||||
PublishEngineStatus();
|
||||
@ -491,20 +491,21 @@ bool XNEngine::Run(const std::string &XmlPath, const uint32_t InitializeType)
|
||||
}
|
||||
} else {
|
||||
// 记录错误日志
|
||||
LOG_ERROR("0x1014 无法发送引擎运行状态, 引擎将退出!");
|
||||
LOG_ERROR("B02012022: 无法发送引擎运行状态, 引擎将退出!");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 返回成功
|
||||
return true;
|
||||
}
|
||||
|
||||
// 运行引擎
|
||||
// 解析数据库
|
||||
bool XNEngine::ParseDataBase(const std::string &ConfigId)
|
||||
{
|
||||
// 获取数据库路径
|
||||
std::string dbPath = std::getenv("XNCore");
|
||||
if (dbPath.empty()) {
|
||||
LOG_ERROR("0x1015 未设置XNCore环境变量, 引擎将退出!");
|
||||
LOG_ERROR("A02012023: 未设置XNCore环境变量, 引擎将退出!");
|
||||
return false;
|
||||
}
|
||||
dbPath += "/database/XNSim.db";
|
||||
@ -512,7 +513,7 @@ bool XNEngine::ParseDataBase(const std::string &ConfigId)
|
||||
// 打开数据库
|
||||
sqlite3 *db;
|
||||
if (sqlite3_open(dbPath.c_str(), &db) != SQLITE_OK) {
|
||||
LOG_ERROR("0x1016 打开数据库失败: %1", sqlite3_errmsg(db));
|
||||
LOG_ERROR("C02012024: 打开数据库失败: %1", sqlite3_errmsg(db));
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -520,7 +521,7 @@ bool XNEngine::ParseDataBase(const std::string &ConfigId)
|
||||
std::string sql = "SELECT * FROM Configuration WHERE ConfID = ?";
|
||||
sqlite3_stmt *stmt;
|
||||
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
|
||||
LOG_ERROR("0x1017 准备SQL语句失败: %1", sqlite3_errmsg(db));
|
||||
LOG_ERROR("C02012025: 准备SQL语句失败: %1", sqlite3_errmsg(db));
|
||||
sqlite3_close(db);
|
||||
return false;
|
||||
}
|
||||
@ -528,7 +529,7 @@ bool XNEngine::ParseDataBase(const std::string &ConfigId)
|
||||
// 绑定参数
|
||||
int configIdInt = std::stoi(ConfigId);
|
||||
if (sqlite3_bind_int(stmt, 1, configIdInt) != SQLITE_OK) {
|
||||
LOG_ERROR("0x1018 绑定参数失败: %1", sqlite3_errmsg(db));
|
||||
LOG_ERROR("C02012026: 绑定参数失败: %1", sqlite3_errmsg(db));
|
||||
sqlite3_finalize(stmt);
|
||||
sqlite3_close(db);
|
||||
return false;
|
||||
@ -536,14 +537,13 @@ bool XNEngine::ParseDataBase(const std::string &ConfigId)
|
||||
|
||||
// 执行查询
|
||||
if (sqlite3_step(stmt) != SQLITE_ROW) {
|
||||
LOG_ERROR("0x1019 未找到配置ID为%1的记录", ConfigId);
|
||||
LOG_ERROR("A02012027: 未找到构型ID为%1的记录", ConfigId);
|
||||
sqlite3_finalize(stmt);
|
||||
sqlite3_close(db);
|
||||
return false;
|
||||
}
|
||||
|
||||
int cpus = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
std::cout << "当前CPU核心数-> " << cpus << std::endl;
|
||||
|
||||
// 设置CPU亲和性
|
||||
cpu_set_t mask;
|
||||
@ -564,22 +564,22 @@ bool XNEngine::ParseDataBase(const std::string &ConfigId)
|
||||
CPU_SET(index, &mask);
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "0x1010 无效的CPU亲和性值: " << cpuIndex << std::endl;
|
||||
std::cerr << "A02013028: 无效的CPU亲和性值: " << cpuIndex << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
if (sched_setaffinity(0, sizeof(mask), &mask) == -1) {
|
||||
std::cerr << "0x1011 设置引擎CPU亲和性失败-> " << strerror(errno) << std::endl;
|
||||
std::cerr << "C02012029: 设置引擎CPU亲和性失败-> " << strerror(errno) << std::endl;
|
||||
return false;
|
||||
}
|
||||
std::cout << "成功设置引擎CPU亲和性-> " << CPUAffinity << std::endl;
|
||||
std::cout << "D02014030: 成功设置引擎CPU亲和性-> " << CPUAffinity << std::endl;
|
||||
|
||||
// 锁定内存
|
||||
if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
|
||||
std::cerr << "0x1012 锁定引擎内存失败-> " << strerror(errno) << std::endl;
|
||||
std::cerr << "C02012031: 锁定引擎内存失败-> " << strerror(errno) << std::endl;
|
||||
return false;
|
||||
}
|
||||
std::cout << "成功锁定引擎内存!" << std::endl;
|
||||
std::cout << "D02014032: 成功锁定引擎内存!" << std::endl;
|
||||
|
||||
auto readBoolean = [](sqlite3_stmt *stmt, int column) -> bool {
|
||||
return sqlite3_column_int(stmt, column) != 0;
|
||||
|
@ -23,8 +23,8 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
XNEngine engine;
|
||||
//检测输入参数个数
|
||||
if (argc <= 2) {
|
||||
std::cerr << "0x1000 输入参数太少!" << std::endl;
|
||||
if (argc <= 1) {
|
||||
std::cerr << "A02012001: 输入参数太少! 使用 -h 查看帮助." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -33,6 +33,19 @@ int main(int argc, char *argv[])
|
||||
bool hasConfigPath = false;
|
||||
bool hasConfigId = false;
|
||||
|
||||
std::string arg = argv[1];
|
||||
if ("-h" == arg) {
|
||||
std::cout << "-f\t<config_file>\t指定构型配置文件路径, 不可与-id参数同时使用" << std::endl;
|
||||
std::cout << "-id\t<config_id>\t指定构型ID, 不可与-f参数同时使用" << std::endl;
|
||||
std::cout << "-test\t\t\t指定测试模式" << std::endl;
|
||||
std::cout << "-h\t\t\t查看帮助" << std::endl;
|
||||
std::cout << "-v\t\t\t查看版本" << std::endl;
|
||||
return 0;
|
||||
} else if ("-v" == arg) {
|
||||
std::cout << "XNEngine v1.0.0" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//检查是否包含-f和-id参数
|
||||
for (int i = 1; i < argc;) {
|
||||
std::string arg = argv[i];
|
||||
@ -42,7 +55,8 @@ int main(int argc, char *argv[])
|
||||
hasConfigPath = true;
|
||||
i += 2;
|
||||
} else {
|
||||
std::cerr << "0x1001 在-f参数后,未指定配置文件路径,引擎将退出!" << std::endl;
|
||||
std::cerr << "A02012002: 在-f参数后, 未指定构型配置文件路径, 引擎将退出!"
|
||||
<< std::endl;
|
||||
return -1;
|
||||
}
|
||||
} else if ("-id" == arg) {
|
||||
@ -51,22 +65,21 @@ int main(int argc, char *argv[])
|
||||
hasConfigId = true;
|
||||
i += 2;
|
||||
} else {
|
||||
std::cerr << "0x1002 在-id参数后,未指定配置ID,引擎将退出!" << std::endl;
|
||||
std::cerr << "A02012003: 在-id参数后, 未指定构型ID, 引擎将退出!" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
} else if ("-test" == arg) {
|
||||
engine.SetTestMode(true);
|
||||
i++;
|
||||
} else {
|
||||
std::cerr << "0x1003 无法识别的参数 " << arg << " ,引擎将退出!" << std::endl;
|
||||
std::cerr << "A02012004: 无法识别的参数 " << arg << " , 引擎将退出!" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
//检查是否同时包含-f和-id参数
|
||||
if (hasConfigPath && hasConfigId) {
|
||||
std::cerr << "0x1004 请指定 -f <config_file> 或 -id <config_id> ,但不要同时指定!"
|
||||
<< std::endl;
|
||||
std::cerr << "A02012005: 请不要同时使用 -f 和 -id 参数, 引擎将退出!" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user