Browse Source

优化栈信息打印效果

storage-in-realtime
zhaohe 11 months ago
parent
commit
a7bf89246d
  1. 105
      app_protocols/appexception/appexception.hpp
  2. 1
      appsrc/appsetting/project_port/project_port.cpp
  3. 1
      appsrc/service/app/disinfection_ctrl/disinfection_ctrl_service.hpp
  4. 7
      appsrc/service/app_core.cpp
  5. 58
      test/test.cpp

105
app_protocols/appexception/appexception.hpp

@ -32,25 +32,7 @@ class appexception : public std::exception {
string description; 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() { appexception() {
this->traceinfo = printStackTrace(); this->traceinfo = printStackTrace();
@ -65,7 +47,7 @@ class appexception : public std::exception {
string info; string info;
info += fmt::format("ecode:{}\n", ecode); info += fmt::format("ecode:{}\n", ecode);
info += fmt::format("description:{}\n", description); info += fmt::format("description:{}\n", description);
info += fmt::format("traceInfo:{}\n", traceinfo);
info += fmt::format("traceInfo:\n{}\n", traceinfo);
whatstr = info; whatstr = info;
// thisclass.logger->error(whatstr); // thisclass.logger->error(whatstr);
@ -74,6 +56,89 @@ class appexception : public std::exception {
virtual ~appexception() {} virtual ~appexception() {}
int getEcode() const { return ecode; } int getEcode() const { return ecode; }
const char *what() const _GLIBCXX_USE_NOEXCEPT { return whatstr.c_str(); } 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<std::string> 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) \ #define APPCHECK(expr, ecode, info) \

1
appsrc/appsetting/project_port/project_port.cpp

@ -74,7 +74,6 @@ string ProjectPort::getProjTypeString() { return projectType; }
void ProjectPort::initProjectSetting(int projectTypeInt) { void ProjectPort::initProjectSetting(int projectTypeInt) {
this->projectTypeInt = projectTypeInt; this->projectTypeInt = projectTypeInt;
projectType = projectType2Str(projectTypeInt); projectType = projectType2Str(projectTypeInt);
// 设备ID初始化 // 设备ID初始化
if (isLageSpaceDM() || isSmallSpaceDM() || isPipeDM()) { if (isLageSpaceDM() || isSmallSpaceDM() || isPipeDM()) {
// 加液泵 // 加液泵

1
appsrc/service/app/disinfection_ctrl/disinfection_ctrl_service.hpp

@ -96,6 +96,7 @@ class DisinfectionCtrlService : public enable_shared_from_this<DisinfectionCtrlS
} }
return 0; return 0;
} }
bool isDisinfectionTakeBreak() { return s_isDisinfectionTakeBreak; }
shared_ptr<H2O2SensorDataSnapshot> getH2O2Snapshot() { return dics->getH2O2SensorMgr()->takeSnapshot(); } shared_ptr<H2O2SensorDataSnapshot> getH2O2Snapshot() { return dics->getH2O2SensorMgr()->takeSnapshot(); }

7
appsrc/service/app_core.cpp

@ -40,6 +40,13 @@ static void installEcodeInfo() {
REG_ENUM_TYPE(UsrRoleType, UsrRoleType::getEnumStrList()); REG_ENUM_TYPE(UsrRoleType, UsrRoleType::getEnumStrList());
REG_ENUM_TYPE(AppEventType, AppEventType::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_reset_error, "电机复位错误");
AppEcodeInfoMgr::ins().regEcodeInfo(kerr_motor_subdevice_offline, "电机子设备离线"); AppEcodeInfoMgr::ins().regEcodeInfo(kerr_motor_subdevice_offline, "电机子设备离线");
AppEcodeInfoMgr::ins().regEcodeInfo(kerr_motor_driver_error, "电机驱动器错误"); AppEcodeInfoMgr::ins().regEcodeInfo(kerr_motor_driver_error, "电机驱动器错误");

58
test/test.cpp

@ -1,24 +1,46 @@
#pragma once
#include <signal.h>
#include <chrono>
#include <fstream>
#include <functional>
#include <iostream> #include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
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<std::string> 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; return 0;
} }
Loading…
Cancel
Save