regulators: Create ErrorHistory class

Create a class to track error history, such as error count and whether
the error has been logged.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: Iead88963110a699eee1d6cd8750303f05d6ecef1
diff --git a/phosphor-regulators/src/error_history.hpp b/phosphor-regulators/src/error_history.hpp
new file mode 100644
index 0000000..834cd79
--- /dev/null
+++ b/phosphor-regulators/src/error_history.hpp
@@ -0,0 +1,79 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include <cstddef>
+#include <cstdint>
+
+namespace phosphor::power::regulators
+{
+
+/**
+ * @class ErrorHistory
+ *
+ * This class represents the history of an error.
+ *
+ * ErrorHistory tracks the error count and whether the error has been logged.
+ *
+ * This class is often used to limit the number of journal messages and error
+ * logs created by code that runs repeatedly, such as sensor monitoring.
+ */
+class ErrorHistory
+{
+  public:
+    // Specify which compiler-generated methods we want
+    ErrorHistory() = default;
+    ErrorHistory(const ErrorHistory&) = default;
+    ErrorHistory(ErrorHistory&&) = default;
+    ErrorHistory& operator=(const ErrorHistory&) = default;
+    ErrorHistory& operator=(ErrorHistory&&) = default;
+    ~ErrorHistory() = default;
+
+    /**
+     * Clears the error history.
+     */
+    void clear()
+    {
+        count = 0;
+        wasLogged = false;
+    }
+
+    /**
+     * Increments the error count.
+     *
+     * Does nothing if the error count is already at the maximum.  This avoids
+     * wrapping back to 0 again.
+     */
+    void incrementCount()
+    {
+        if (count < SIZE_MAX)
+        {
+            ++count;
+        }
+    }
+
+    /**
+     * Error count.
+     */
+    std::size_t count{0};
+
+    /**
+     * Indicates whether this error was logged, resulting in an error log entry.
+     */
+    bool wasLogged{false};
+};
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/test/error_history_tests.cpp b/phosphor-regulators/test/error_history_tests.cpp
new file mode 100644
index 0000000..3e3cba8
--- /dev/null
+++ b/phosphor-regulators/test/error_history_tests.cpp
@@ -0,0 +1,60 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "error_history.hpp"
+
+#include <cstdint>
+
+#include <gtest/gtest.h>
+
+using namespace phosphor::power::regulators;
+
+TEST(ErrorHistoryTests, Constructor)
+{
+    ErrorHistory history{};
+    EXPECT_EQ(history.count, 0);
+    EXPECT_EQ(history.wasLogged, false);
+}
+
+TEST(ErrorHistoryTests, Clear)
+{
+    ErrorHistory history{};
+    history.count = 23;
+    history.wasLogged = true;
+    history.clear();
+    EXPECT_EQ(history.count, 0);
+    EXPECT_EQ(history.wasLogged, false);
+}
+
+TEST(ErrorHistoryTests, IncrementCount)
+{
+    ErrorHistory history{};
+
+    // Test where count is not near the max
+    EXPECT_EQ(history.count, 0);
+    history.incrementCount();
+    EXPECT_EQ(history.count, 1);
+    history.incrementCount();
+    EXPECT_EQ(history.count, 2);
+
+    // Test where count is near the max.  Verify it does not wrap/overflow.
+    history.count = SIZE_MAX - 2;
+    history.incrementCount();
+    EXPECT_EQ(history.count, SIZE_MAX - 1);
+    history.incrementCount();
+    EXPECT_EQ(history.count, SIZE_MAX);
+    history.incrementCount();
+    EXPECT_EQ(history.count, SIZE_MAX);
+}
diff --git a/phosphor-regulators/test/meson.build b/phosphor-regulators/test/meson.build
index b73c3a0..8a1d059 100644
--- a/phosphor-regulators/test/meson.build
+++ b/phosphor-regulators/test/meson.build
@@ -9,6 +9,7 @@
     'config_file_parser_tests.cpp',
     'configuration_tests.cpp',
     'device_tests.cpp',
+    'error_history_tests.cpp',
     'exception_utils_tests.cpp',
     'id_map_tests.cpp',
     'mock_journal.cpp',