Skip to content

Commit 78b8552

Browse files
committed
--reorganize; build diagnostics parent classes for other subsystems
1 parent 8340c1e commit 78b8552

File tree

8 files changed

+483
-183
lines changed

8 files changed

+483
-183
lines changed

src/esp/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ set(
145145
core/Check.h
146146
core/Configuration.cpp
147147
core/Configuration.h
148+
core/diagnostics/DiagnosticsRecord.h
149+
core/diagnostics/DiagnosticsTool.h
148150
core/Esp.cpp
149151
core/Esp.h
150152
core/Logging.cpp
@@ -313,6 +315,9 @@ set(
313315
metadata/attributes/StageAttributes.cpp
314316
metadata/diagnostics/DatasetDiagnosticsTool.h
315317
metadata/diagnostics/DatasetDiagnosticsTool.cpp
318+
metadata/diagnostics/DatasetDiagnosticsRecord.h
319+
metadata/diagnostics/DatasetDiagnostics.h
320+
metadata/diagnostics/DatasetDiagnostics.cpp
316321
metadata/managers/AbstractAttributesManager.h
317322
metadata/managers/AbstractObjectAttributesManager.h
318323
metadata/managers/AOAttributesManager.h
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Copyright (c) Meta Platforms, Inc. and its affiliates.
2+
// This source code is licensed under the MIT license found in the
3+
// LICENSE file in the root directory of this source tree.
4+
5+
#ifndef ESP_CORE_DIAGNOSTICS_DIAGNOSTICSRECORD_H_
6+
#define ESP_CORE_DIAGNOSTICS_DIAGNOSTICSRECORD_H_
7+
8+
#include <type_traits>
9+
#include <unordered_map>
10+
#include "esp/core/Esp.h"
11+
12+
namespace esp {
13+
namespace core {
14+
namespace diagnostics {
15+
16+
/**
17+
* @brief Construct to provide a foundation for building a record of the results
18+
* of a series of diagnostics.
19+
*/
20+
class DiagnosticsRecord {
21+
public:
22+
explicit DiagnosticsRecord(uint32_t diagnosticsFlags)
23+
: diagnosticsFlags_(diagnosticsFlags) {}
24+
25+
protected:
26+
/**
27+
* @brief Set the diagnostic flag representing the type of diagnostics
28+
* recorded by this record
29+
*/
30+
template <class T>
31+
inline void setFlag(T flag, bool val) {
32+
//_flag must be an enum value
33+
static_assert(std::is_enum<T>::value,
34+
"The Diagnostics Record flag must be an enum");
35+
//_flag must be backed by a uint32_t
36+
static_assert(
37+
std::is_same<typename std::underlying_type<T>::type, uint32_t>::value,
38+
"The Diagnostics Record flag enum must be backed by a uint32_t");
39+
if (val) {
40+
diagnosticsFlags_ |= static_cast<uint32_t>(flag);
41+
} else {
42+
diagnosticsFlags_ &= ~static_cast<uint32_t>(flag);
43+
}
44+
}
45+
46+
/**
47+
* @brief Get the diagnostic flag representing the type of diagnostics
48+
* recorded by this record
49+
*/
50+
template <class T>
51+
inline bool getFlag(T flag) const {
52+
//_flag must be an enum value
53+
static_assert(std::is_enum<T>::value,
54+
"The Diagnostics Record flag must be an enum");
55+
//_flag must be backed by a uint32_t
56+
static_assert(
57+
std::is_same<typename std::underlying_type<T>::type, uint32_t>::value,
58+
"The Diagnostics Record flag enum must be backed by a uint32_t");
59+
return (diagnosticsFlags_ & static_cast<uint32_t>(flag)) ==
60+
static_cast<uint32_t>(flag);
61+
}
62+
63+
/**
64+
* @brief Record diagnostic information for specified test.
65+
* @param diagFlag The diagnostic test enum representing the type of the test.
66+
* @param report String report to be appended to the @p _resultsLog
67+
*/
68+
template <class T>
69+
void recordDiagResult(T diagFlag, const std::string& report) {
70+
//_flag must be an enum value
71+
static_assert(std::is_enum<T>::value,
72+
"The Diagnostics Record flag must be an enum");
73+
//_flag must be backed by a uint32_t
74+
static_assert(
75+
std::is_same<typename std::underlying_type<T>::type, uint32_t>::value,
76+
"The Diagnostics Record flag enum must be backed by a uint32_t");
77+
// either add a new or retrieve existing
78+
auto emplaceRes = resultsLog_.emplace(static_cast<uint32_t>(diagFlag),
79+
std::vector<std::string>());
80+
emplaceRes.first->second.push_back(report);
81+
}
82+
83+
/**
84+
* @brief Retrieve results log for specific diagnostic test type
85+
*/
86+
template <class T>
87+
const std::vector<std::string> getDiagResult(T diagFlag) const {
88+
//_flag must be an enum value
89+
static_assert(std::is_enum<T>::value,
90+
"The Diagnostics Record flag must be an enum");
91+
//_flag must be backed by a uint32_t
92+
static_assert(
93+
std::is_same<typename std::underlying_type<T>::type, uint32_t>::value,
94+
"The Diagnostics Record flag enum must be backed by a uint32_t");
95+
auto reportIter = resultsLog_.find(static_cast<uint32_t>(diagFlag));
96+
if (reportIter == resultsLog_.end()) {
97+
// Caller should check for empty and respond accordingly
98+
return {};
99+
}
100+
return reportIter->second;
101+
} // getDiagResult
102+
103+
private:
104+
/**
105+
* @brief Holds collections of strings recording the results of each enabled
106+
* diagnostic.
107+
*/
108+
std::unordered_map<uint32_t, std::vector<std::string>> resultsLog_;
109+
/**
110+
* @brief Flags to record the diagnostics performed
111+
*/
112+
uint32_t diagnosticsFlags_ = 0u;
113+
114+
public:
115+
ESP_SMART_POINTERS(DiagnosticsRecord)
116+
}; // class DiagnosticsRecord
117+
118+
} // namespace diagnostics
119+
} // namespace core
120+
} // namespace esp
121+
#endif // ESP_CORE_DIAGNOSTICS_DIAGNOSTICSRECORD_H_
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Copyright (c) Meta Platforms, Inc. and its affiliates.
2+
// This source code is licensed under the MIT license found in the
3+
// LICENSE file in the root directory of this source tree.
4+
5+
#ifndef ESP_CORE_DIAGNOSTICS_DIAGNOSTICSTOOL_H_
6+
#define ESP_CORE_DIAGNOSTICS_DIAGNOSTICSTOOL_H_
7+
8+
#include <type_traits>
9+
#include "esp/core/Check.h"
10+
#include "esp/core/Esp.h"
11+
12+
namespace esp {
13+
namespace core {
14+
namespace diagnostics {
15+
16+
/**
17+
* @brief This class will track requested diagnostics for specific config
18+
* files/attributes. It is intended that each @ref AbstractAttributesManager
19+
* would instantiate one of these objects and use it to determine various
20+
* diagnostic behavior.
21+
*/
22+
class DiagnosticsTool {
23+
public:
24+
DiagnosticsTool() = default;
25+
26+
/**
27+
* @brief Reset the current state of the Diagnostics tool.
28+
*/
29+
void reset() {
30+
clearDiagnosticFlags();
31+
resetIndiv();
32+
}
33+
34+
/**
35+
* @brief Implementation class-specific reset code
36+
*/
37+
virtual void resetIndiv() = 0;
38+
39+
/**
40+
* @brief Clear all existing diagnostic flag settings.
41+
*/
42+
void clearDiagnosticFlags() { diagnosticsFlags_ = 0u; }
43+
44+
protected:
45+
/**
46+
* @brief Merge the passed @ref DiagnosticsTool's @p _diagnosticsFlag
47+
* settings into this one's, preserving this one's diagnostic requests as
48+
* well. Only referenced internally to ensure tool type consistency.
49+
*/
50+
void mergeDiagnosticsToolInternal(const DiagnosticsTool& tool) {
51+
diagnosticsFlags_ |= tool.diagnosticsFlags_;
52+
}
53+
54+
/**
55+
* @brief Enable/disable the diagnostic given by @p diagnostic string based on
56+
* @p val.
57+
*
58+
* @param diagnostic The string name of the diagnostic. This must be mappable
59+
* to an appropriate enum type via @p diagMap .
60+
* @param diagMap Map holding the string to enum mapping for the implementing
61+
* diagnostic tool.
62+
* @param val Whether to enable or disable the given diagnostic
63+
* @param abortOnFail Whether or not to abort the program if the
64+
* @p diagnostic requeseted is not found in @p diagMap. If false,
65+
* will print an error message and skip the unknown request.
66+
* @return Whether the passed string successfully mapped to a known diagnostic
67+
*/
68+
template <class T>
69+
bool setNamedDiagnosticInternal(const std::string& diagnostic,
70+
const std::map<std::string, T>& diagMap,
71+
bool val,
72+
bool abortOnFail = false);
73+
74+
template <class T>
75+
inline void setFlag(T _flag, bool _val) {
76+
//_flag must be an enum value
77+
static_assert(std::is_enum<T>::value,
78+
"The Diagnostics Tool's flag must be an enum");
79+
//_flag must be backed by a uint32_t
80+
static_assert(
81+
std::is_same<typename std::underlying_type<T>::type, uint32_t>::value,
82+
"The Diagnostics Tool's flag enum must be backed by a uint32_t");
83+
if (_val) {
84+
diagnosticsFlags_ |= static_cast<uint32_t>(_flag);
85+
} else {
86+
diagnosticsFlags_ &= ~static_cast<uint32_t>(_flag);
87+
}
88+
}
89+
90+
template <class T>
91+
inline bool getFlag(T _flag) const {
92+
//_flag must be an enum value
93+
static_assert(std::is_enum<T>::value,
94+
"The Diagnostics Tool's flag must be an enum");
95+
//_flag must be backed by a uint32_t
96+
static_assert(
97+
std::is_same<typename std::underlying_type<T>::type, uint32_t>::value,
98+
"The Diagnostics Tool's flag enum must be backed by a uint32_t");
99+
return (diagnosticsFlags_ & static_cast<uint32_t>(_flag)) ==
100+
static_cast<uint32_t>(_flag);
101+
}
102+
103+
private:
104+
uint32_t diagnosticsFlags_ = 0u;
105+
106+
public:
107+
ESP_SMART_POINTERS(DiagnosticsTool)
108+
109+
}; // class DiagnosticsTool
110+
111+
/////////////////////////////
112+
// Class Template Method Definitions
113+
template <class T>
114+
bool DiagnosticsTool::setNamedDiagnosticInternal(
115+
const std::string& diagnostic,
116+
const std::map<std::string, T>& diagMap,
117+
bool val,
118+
bool abortOnFail) {
119+
//_flag must be an enum value
120+
static_assert(std::is_enum<T>::value,
121+
"The Diagnostics Tool's flag must be an enum");
122+
//_flag must be backed by a uint32_t
123+
static_assert(
124+
std::is_same<typename std::underlying_type<T>::type, uint32_t>::value,
125+
"The Diagnostics Tool's flag enum must be backed by a uint32_t");
126+
const std::string diagnosticLC =
127+
Corrade::Utility::String::lowercase(diagnostic);
128+
129+
auto mapIter = diagMap.find(diagnosticLC);
130+
if (abortOnFail) {
131+
// If not found then abort.
132+
ESP_CHECK(mapIter != diagMap.end(),
133+
"Unknown Diagnostic Test requested to be "
134+
<< (val ? "Enabled :" : "Disabled :") << diagnostic
135+
<< ". Aborting.");
136+
} else {
137+
if (mapIter == diagMap.end()) {
138+
ESP_ERROR() << "Unknown Diagnostic Test requested to be "
139+
<< (val ? "Enabled" : "Disabled") << " :" << diagnostic
140+
<< " so ignoring request.";
141+
return false;
142+
}
143+
}
144+
setFlag(mapIter->second, val);
145+
return true;
146+
} // DiagnosticsTool::setNamedDiagnostic
147+
148+
} // namespace diagnostics
149+
} // namespace core
150+
} // namespace esp
151+
152+
#endif // ESP_CORE_DIAGNOSTICS_DIAGNOSTICSTOOL_H_
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
// Copyright (c) Meta Platforms, Inc. and its affiliates.
3+
// This source code is licensed under the MIT license found in the
4+
// LICENSE file in the root directory of this source tree.
5+
6+
#include "DatasetDiagnostics.h"
7+
8+
namespace esp {
9+
namespace metadata {
10+
namespace diagnostics {
11+
12+
const std::map<std::string, DSDiagnosticType> DSDiagnosticTypeMap = {
13+
{"savecorrected", DSDiagnosticType::SaveCorrected},
14+
{"sceneinstanceduplicates", DSDiagnosticType::TestForDuplicateInstances},
15+
{"semanticregionduplicates", DSDiagnosticType::TestForDuplicateRegions},
16+
// Future diagnostics should be listed here, before "all"
17+
{"all", DSDiagnosticType::AllDiagnostics},
18+
{"allsavecorrected", DSDiagnosticType::AllDiagnosticsSaveCorrected},
19+
};
20+
21+
std::string getDSDiagnosticsTypeName(DSDiagnosticType dsDiagTypeName) {
22+
// this verifies that enum value being checked is supported by string-keyed
23+
// map. The values below should be the minimum and maximum enums supported by
24+
// DSDiagnosticTypeMap
25+
if (dsDiagTypeName <= DSDiagnosticType::Unknown) {
26+
return "unknown";
27+
}
28+
// Must always be valid value
29+
for (const auto& it : DSDiagnosticTypeMap) {
30+
if (it.second == dsDiagTypeName) {
31+
return it.first;
32+
}
33+
}
34+
return "unknown";
35+
} // getDSDiagnosticsTypeName
36+
37+
} // namespace diagnostics
38+
39+
} // namespace metadata
40+
} // namespace esp

0 commit comments

Comments
 (0)