Select Git revision
stack-path-depth.patch

Simon Schwitanski authored
stack-path-depth.patch 2.50 KiB
diff --git a/modules/MustBase/InitLocationId.cpp b/modules/MustBase/InitLocationId.cpp
index 202cf077..184b61f7 100644
--- a/modules/MustBase/InitLocationId.cpp
+++ b/modules/MustBase/InitLocationId.cpp
@@ -72,6 +72,12 @@ InitLocationId::InitLocationId(const char* instanceName)
<< std::endl;
assert(0);
}
+
+ myStackPathDepth = -1;
+ const char* depth_str = std::getenv("MUST_STACKTRACE_PATH_DEPTH");
+ if (depth_str != nullptr) {
+ myStackPathDepth = std::atoi(depth_str);
+ }
}
//=============================
@@ -90,6 +96,31 @@ InitLocationId::~InitLocationId()
}
}
+std::string InitLocationId::truncatePath(int height, std::string path)
+{
+ if (height <= 0)
+ return std::string(path);
+
+ auto rit = path.rbegin();
+ std::string::size_type pos = path.length();
+ int path_separator_count = 0;
+ // search for path separators backwards and abort if height is reached
+ while (path_separator_count < height) {
+ pos = path.rfind('/', pos - 1);
+ if (pos == std::string::npos) {
+ pos = 0;
+ break;
+ } else {
+ path_separator_count++;
+ }
+ }
+
+ if (pos > 0) // prepend truncation sequence
+ return "[...]" + path.substr(pos, path.length());
+ else
+ return std::string(path);
+}
+
//=============================
// init
//=============================
@@ -186,7 +217,7 @@ GTI_ANALYSIS_RETURN InitLocationId::init(MustLocationId* pStorage, const char* c
thisFullLocation.stack.emplace_back(trace.object_filename, trace.object_function);
} else {
thisFullLocation.stack.emplace_back(
- trace.source.filename,
+ truncatePath(myStackPathDepth, trace.source.filename),
trace.source.function,
std::to_string(trace.source.line));
}
diff --git a/modules/MustBase/InitLocationId.h b/modules/MustBase/InitLocationId.h
index 2aee6407..d1fdc01f 100644
--- a/modules/MustBase/InitLocationId.h
+++ b/modules/MustBase/InitLocationId.h
@@ -68,6 +68,10 @@ class InitLocationId : public gti::ModuleBase<InitLocationId, I_InitLocationId>
I_GenerateLocationId* myGenLId;
handleNewLocationP myNewLocFct;
+ private:
+ int myStackPathDepth;
+ std::string truncatePath(int length, std::string path);
+
#ifdef ENABLE_STACKTRACE
/**
* Helper function to call the handleNewLocation call when we use the callpath module
--
2.39.1