1#include "ComponentHeightTracer.h"
8namespace Odb::Lib::FileModel::Design
10 ComponentHeightTracer &ComponentHeightTracer::instance()
12 static ComponentHeightTracer tracer;
16 ComponentHeightTracer::ComponentHeightTracer()
20 const char* envComponents = std::getenv(
"ODBDESIGN_TRACE_COMPONENTS");
21 if (envComponents !=
nullptr && envComponents[0] !=
'\0')
23 std::istringstream stream(envComponents);
25 while (std::getline(stream, name,
','))
28 Utils::str_trim(name);
31 m_componentsToTrace.insert(name);
36 const char* envMaxFailures = std::getenv(
"ODBDESIGN_TRACE_MAX_FAILURES");
37 if (envMaxFailures !=
nullptr)
41 m_maxFailuresToTrace = std::stoi(envMaxFailures);
43 if (m_maxFailuresToTrace <= 0)
45 logwarn(
"[ComponentHeightTracer] ODBDESIGN_TRACE_MAX_FAILURES must be positive, using default value 20");
46 m_maxFailuresToTrace = 20;
51 logwarn(
"[ComponentHeightTracer] Failed to parse ODBDESIGN_TRACE_MAX_FAILURES env var, using default value 20");
52 m_maxFailuresToTrace = 20;
57 bool ComponentHeightTracer::shouldTrace(
const std::string &compName,
unsigned int pkgRef)
const
60 if (m_componentsToTrace.find(compName) != m_componentsToTrace.end())
65 return shouldTraceFailure();
68 bool ComponentHeightTracer::shouldTraceFailure()
const
70 return m_failureCount.load() < m_maxFailuresToTrace;
73 void ComponentHeightTracer::markFailure()
const
75 m_failureCount.fetch_add(1);
78 std::string ComponentHeightTracer::getCompKey(
const std::string &compName,
unsigned int pkgRef)
const
81 ss << compName <<
"(Package=" << pkgRef <<
")";
85 std::string ComponentHeightTracer::formatLookupTable(
const std::map<std::string, std::string> &lookupTable)
const
90 for (
const auto &kv : lookupTable)
94 ss <<
"\"" << kv.first <<
"\":\"" << kv.second <<
"\"";
101 std::string ComponentHeightTracer::formatAttributeNames(
const std::vector<std::string> &attributeNames)
const
103 std::stringstream ss;
105 for (
size_t i = 0; i < attributeNames.size(); ++i)
109 ss << i <<
":\"" << attributeNames[i] <<
"\"";
115 void ComponentHeightTracer::logParseStart(
const std::string &compName,
unsigned int pkgRef,
const std::string &attrIdString)
const
117 if (!shouldTrace(compName, pkgRef))
120 std::stringstream ss;
121 ss <<
"[CompHeightTrace][PARSE_START] Component=" << compName
122 <<
", Package=" << pkgRef
123 <<
", attrIdString=\"" << attrIdString <<
"\"";
127 void ComponentHeightTracer::logParseResult(
const std::string &compName,
unsigned int pkgRef,
128 const std::map<std::string, std::string> &lookupTable,
129 const std::vector<std::string> &attributeNames)
const
131 if (!shouldTrace(compName, pkgRef))
134 std::stringstream ss;
135 ss <<
"[CompHeightTrace][PARSE_RESULT] Component=" << compName
136 <<
", Package=" << pkgRef
137 <<
", AttributeNames=" << formatAttributeNames(attributeNames)
138 <<
", AttributeLookupTable=" << formatLookupTable(lookupTable);
141 bool hasCompHeightAttr = !attributeNames.empty() && attributeNames[0] ==
".comp_height";
142 bool hasKey0 = lookupTable.find(
"0") != lookupTable.end();
144 ss <<
", .comp_height@index0=" << (hasCompHeightAttr ?
"YES" :
"NO")
145 <<
", key\"0\"_exists=" << (hasKey0 ?
"YES" :
"NO");
147 if (hasCompHeightAttr && !hasKey0)
149 ss <<
" ⚠️ MISSING_KEY_0";
154 ss <<
", height_value=\"" << lookupTable.at(
"0") <<
"\"";
160 void ComponentHeightTracer::logSerialization(
const std::string &compName,
unsigned int pkgRef,
161 const std::map<std::string, std::string> &lookupTable,
162 const std::vector<std::string> &attributeNames)
const
164 if (!shouldTrace(compName, pkgRef))
167 std::stringstream ss;
168 ss <<
"[CompHeightTrace][SERIALIZE] Component=" << compName
169 <<
", Package=" << pkgRef
170 <<
", AttributeNames=" << formatAttributeNames(attributeNames)
171 <<
", AttributeLookupTable=" << formatLookupTable(lookupTable);
173 bool hasKey0 = lookupTable.find(
"0") != lookupTable.end();
176 ss <<
", height_value=\"" << lookupTable.at(
"0") <<
"\"";
180 ss <<
", key\"0\"_missing";
186 void ComponentHeightTracer::logGrpcResponse(
const std::string &compName,
unsigned int pkgRef,
187 const std::map<std::string, std::string> &lookupTable,
188 const std::vector<std::string> &attributeNames)
const
190 if (!shouldTrace(compName, pkgRef))
193 std::stringstream ss;
194 ss <<
"[CompHeightTrace][GRPC_RESPONSE] Component=" << compName
195 <<
", Package=" << pkgRef
196 <<
", AttributeNames=" << formatAttributeNames(attributeNames)
197 <<
", AttributeLookupTable=" << formatLookupTable(lookupTable);
199 bool hasKey0 = lookupTable.find(
"0") != lookupTable.end();
202 ss <<
", height_value=\"" << lookupTable.at(
"0") <<
"\"";
206 ss <<
", key\"0\"_missing";
212 void ComponentHeightTracer::logRestResponse(
const std::string &compName,
unsigned int pkgRef,
213 const std::map<std::string, std::string> &lookupTable,
214 const std::vector<std::string> &attributeNames)
const
216 if (!shouldTrace(compName, pkgRef))
219 std::stringstream ss;
220 ss <<
"[CompHeightTrace][REST_RESPONSE] Component=" << compName
221 <<
", Package=" << pkgRef
222 <<
", AttributeNames=" << formatAttributeNames(attributeNames)
223 <<
", AttributeLookupTable=" << formatLookupTable(lookupTable);
225 bool hasKey0 = lookupTable.find(
"0") != lookupTable.end();
228 ss <<
", height_value=\"" << lookupTable.at(
"0") <<
"\"";
232 ss <<
", key\"0\"_missing";
238 void ComponentHeightTracer::addComponentToTrace(
const std::string &compName)
240 m_componentsToTrace.insert(compName);
243 void ComponentHeightTracer::setMaxFailuresToTrace(
int maxFailures)
245 m_maxFailuresToTrace = maxFailures;