1 /**
2  * Copyright © 2020 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 namespace phosphor::power::regulators
22 {
23 
24 /**
25  * @class ErrorHistory
26  *
27  * This class represents the history of an error.
28  *
29  * ErrorHistory tracks the error count and whether the error has been logged.
30  *
31  * This class is often used to limit the number of journal messages and error
32  * logs created by code that runs repeatedly, such as sensor monitoring.
33  */
34 class ErrorHistory
35 {
36   public:
37     // Specify which compiler-generated methods we want
38     ErrorHistory() = default;
39     ErrorHistory(const ErrorHistory&) = default;
40     ErrorHistory(ErrorHistory&&) = default;
41     ErrorHistory& operator=(const ErrorHistory&) = default;
42     ErrorHistory& operator=(ErrorHistory&&) = default;
43     ~ErrorHistory() = default;
44 
45     /**
46      * Clears the error history.
47      */
48     void clear()
49     {
50         count = 0;
51         wasLogged = false;
52     }
53 
54     /**
55      * Increments the error count.
56      *
57      * Does nothing if the error count is already at the maximum.  This avoids
58      * wrapping back to 0 again.
59      */
60     void incrementCount()
61     {
62         if (count < SIZE_MAX)
63         {
64             ++count;
65         }
66     }
67 
68     /**
69      * Error count.
70      */
71     std::size_t count{0};
72 
73     /**
74      * Indicates whether this error was logged, resulting in an error log entry.
75      */
76     bool wasLogged{false};
77 };
78 
79 } // namespace phosphor::power::regulators
80