|
| 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_ |
0 commit comments