From a7bf89246d4e2b677aade58e356067f0886f9cc6 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Thu, 19 Sep 2024 15:55:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=A0=88=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=89=93=E5=8D=B0=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_protocols/appexception/appexception.hpp | 105 +++++++++++++++++---- appsrc/appsetting/project_port/project_port.cpp | 1 - .../disinfection_ctrl_service.hpp | 1 + appsrc/service/app_core.cpp | 7 ++ test/test.cpp | 58 ++++++++---- 5 files changed, 133 insertions(+), 39 deletions(-) diff --git a/app_protocols/appexception/appexception.hpp b/app_protocols/appexception/appexception.hpp index 88ffb04..5820f74 100644 --- a/app_protocols/appexception/appexception.hpp +++ b/app_protocols/appexception/appexception.hpp @@ -32,25 +32,7 @@ class appexception : public std::exception { string description; // - string whatstr; - static std::string printStackTrace() { - std::string ret = ""; - void *callstack[128]; - int frames = backtrace(callstack, sizeof(callstack) / sizeof(callstack[0])); - char **strs = backtrace_symbols(callstack, frames); - - if (strs == NULL) { - return ret; - } - // 从1开始,是不输出最后一层printStackTrace函数的调用信息 - for (int i = 1; i < frames; ++i) { - ret.append(std::string(strs[i])); - ret.append("\n"); - } - - free(strs); - return ret; - } + string whatstr; appexception() { this->traceinfo = printStackTrace(); @@ -65,7 +47,7 @@ class appexception : public std::exception { string info; info += fmt::format("ecode:{}\n", ecode); info += fmt::format("description:{}\n", description); - info += fmt::format("traceInfo:{}\n", traceinfo); + info += fmt::format("traceInfo:\n{}\n", traceinfo); whatstr = info; // thisclass.logger->error(whatstr); @@ -74,6 +56,89 @@ class appexception : public std::exception { virtual ~appexception() {} int getEcode() const { return ecode; } const char *what() const _GLIBCXX_USE_NOEXCEPT { return whatstr.c_str(); } + + /******************************************************************************* + * UTILS * + *******************************************************************************/ + std::string printStackTrace() { + std::string ret = ""; + void *callstack[128]; + int frames = backtrace(callstack, sizeof(callstack) / sizeof(callstack[0])); + char **strs = backtrace_symbols(callstack, frames); + + if (strs == NULL) { + return ret; + } + // 从1开始,是不输出最后一层printStackTrace函数的调用信息 + for (int i = 1; i < frames; ++i) { + ret.append(humanreadable(std::string(strs[i]))); + ret.append("\n"); + } + + free(strs); + return ret; + } + + string humanreadable(string gnusample) { + /** + * @brief + * "/app/app.out(_ZN7iflytop13zscanprotocol16ZSCanProtocolCom12base_callcmdEiiPhii+0x318) + * [0x555885a264]\n/app/app.out(_ZN7iflytop13zscanprotocol16ZSCanProtocolCom7callcmdEiiPhii+0xb4) + * [0x555885a3dc]\n/app/app.out(_ZN7iflytop13zscanprotocol16ZSCanProtocolCom8callcmd1Eiiii+0x38) + * [0x555885a620]\n/app/app.out(_ZN7iflytop29TransmitDisinfectionCanMaster13psBusReadDataEii+0x38) + * [0x555885f800]\n/app/app.out(_ZN7iflytop22DeviceIoControlService15ACPostPS_readPaEv+0x70) + * [0x555898edf8]\n/app/app.out(_ZN7iflytop17AirLeakDetectTest23airLeakDetectTestThreadEi+0xb04) + * [0x55588f9ea8]\n/app/app.out(+0x269f24) + * [0x55588f9f24]\n/app/app.out(_ZNKSt8functionIFvvEEclEv+0x18) + * [0x55588099c8]\n/app/app.out(+0x1baf74) + * [0x555884af74]\n/lib/aarch64-linux-gnu/libstdc++.so.6(+0xccf9c) + * [0x7f90575f9c]\n/lib/aarch64-linux-gnu/libpthread.so.0(+0x7624) + * [0x7f90b1f624]\n/lib/aarch64-linux-gnu/libc.so.6(+0xd162c) + */ + + // 判断是否是_ZN开头的字符串 + + auto zoffset = gnusample.find("_Z"); + if (zoffset == string::npos) { + return gnusample; + } + auto plusoffset = gnusample.find("+"); + string header = gnusample.substr(0, zoffset); + string append = gnusample.substr(plusoffset + 1); + string body = gnusample.substr(zoffset, plusoffset - zoffset - 1); + + // string header; + // string append; + // string body; + + std::vector parts; + size_t i = 0; + while (i < body.size()) { + if (std::isdigit(body[i])) { + // Read length of the following name + size_t length = 0; + while (std::isdigit(body[i])) { + length = length * 10 + (body[i] - '0'); + i++; + } + // Get the name part + std::string name = body.substr(i, length); + parts.push_back(name); + i += length; + } else { + i++; + } + } + + string ret = header; + for (auto &part : parts) { + ret += part + "."; + } + ret[ret.size() - 1] = ' '; + ret += "+"; + ret += append; + return ret; + } }; #define APPCHECK(expr, ecode, info) \ diff --git a/appsrc/appsetting/project_port/project_port.cpp b/appsrc/appsetting/project_port/project_port.cpp index 53c544f..9ef29e1 100644 --- a/appsrc/appsetting/project_port/project_port.cpp +++ b/appsrc/appsetting/project_port/project_port.cpp @@ -74,7 +74,6 @@ string ProjectPort::getProjTypeString() { return projectType; } void ProjectPort::initProjectSetting(int projectTypeInt) { this->projectTypeInt = projectTypeInt; projectType = projectType2Str(projectTypeInt); - // 设备ID初始化 if (isLageSpaceDM() || isSmallSpaceDM() || isPipeDM()) { // 加液泵 diff --git a/appsrc/service/app/disinfection_ctrl/disinfection_ctrl_service.hpp b/appsrc/service/app/disinfection_ctrl/disinfection_ctrl_service.hpp index 1d4f887..2f1ce13 100644 --- a/appsrc/service/app/disinfection_ctrl/disinfection_ctrl_service.hpp +++ b/appsrc/service/app/disinfection_ctrl/disinfection_ctrl_service.hpp @@ -96,6 +96,7 @@ class DisinfectionCtrlService : public enable_shared_from_this getH2O2Snapshot() { return dics->getH2O2SensorMgr()->takeSnapshot(); } diff --git a/appsrc/service/app_core.cpp b/appsrc/service/app_core.cpp index 6f153a3..7c02122 100644 --- a/appsrc/service/app_core.cpp +++ b/appsrc/service/app_core.cpp @@ -40,6 +40,13 @@ static void installEcodeInfo() { REG_ENUM_TYPE(UsrRoleType, UsrRoleType::getEnumStrList()); REG_ENUM_TYPE(AppEventType, AppEventType::getEnumStrList()); + AppEcodeInfoMgr::ins().regEcodeInfo(kerr_overtime, "通信超时"); + AppEcodeInfoMgr::ins().regEcodeInfo(kerr_invalid_param, "非法参数"); + AppEcodeInfoMgr::ins().regEcodeInfo(kerr_invalid_param_num, "参数数量错误"); + AppEcodeInfoMgr::ins().regEcodeInfo(kerr_subdevice_offline, "485总线设备异常"); + AppEcodeInfoMgr::ins().regEcodeInfo(kerr_function_not_support, "代码错误方法不支持"); + AppEcodeInfoMgr::ins().regEcodeInfo(kerr_subdevice_overtime, "485总线设备通信超时"); + AppEcodeInfoMgr::ins().regEcodeInfo(kerr_motor_reset_error, "电机复位错误"); AppEcodeInfoMgr::ins().regEcodeInfo(kerr_motor_subdevice_offline, "电机子设备离线"); AppEcodeInfoMgr::ins().regEcodeInfo(kerr_motor_driver_error, "电机驱动器错误"); diff --git a/test/test.cpp b/test/test.cpp index c42ffe6..a21f563 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -1,24 +1,46 @@ -#pragma once -#include - -#include -#include -#include #include -#include -#include -#include -#include -#include #include #include -using namespace std; -using namespace chrono; -int main(int argc, char *argv[]) { - std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); - std::string s(30, '\0'); - std::strftime(&s[0], s.size(), "%Y-%m-%d %H:%M:%S", std::localtime(&now)); - cout << s << endl; +std::string demangle(const std::string& mangled) { + std::string result; + std::vector parts; + + size_t i = 0; + while (i < mangled.size()) { + if (std::isdigit(mangled[i])) { + // Read length of the following name + size_t length = 0; + while (std::isdigit(mangled[i])) { + length = length * 10 + (mangled[i] - '0'); + i++; + } + // Get the name part + std::string name = mangled.substr(i, length); + parts.push_back(name); + i += length; + } else { + // Handle other characters (if needed) + // result += mangled[i]; + i++; + } + } + + // Join parts with '.' + for (const auto& part : parts) { + if (!result.empty()) { + result += "."; + } + result += part; + } + + return result; +} + +int main() { + std::string mangledName = "_ZNKiflytop13zscanprotocol16ZSCanProtocolCom7callcmdEiiPhii"; + std::string demangledName = demangle(mangledName); + + std::cout << "Demangled Name: " << demangledName << std::endl; return 0; } \ No newline at end of file