regulators: Create error_logging_utils

Create the error_logging_utils namespace for utility functions that make
it easier to log errors.  Create two initial functions within the
namespace.

The first function logs an error based on an exception.  The exception
may have one or more nested inner exceptions.  The function finds the
highest priority exception and logs a corresponding error.

The second function provides the same basic behavior as the first, but
it adds an ErrorHistory parameter.  An error will only be logged if it
was not previously logged.  The ErrorHistory object is used to
determine whether an error has been previously logged.  This avoids
logging duplicate errors if a regulator operation is occurring
repeatedly, such as reading sensor values.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: If246cde9a0f60c5bba34ae4a0d68fb511c0024fc
diff --git a/phosphor-regulators/src/error_logging_utils.hpp b/phosphor-regulators/src/error_logging_utils.hpp
new file mode 100644
index 0000000..d90b2df
--- /dev/null
+++ b/phosphor-regulators/src/error_logging_utils.hpp
@@ -0,0 +1,85 @@
+/**
+ * Copyright © 2021 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 "error_history.hpp"
+#include "error_logging.hpp"
+#include "services.hpp"
+
+#include <exception>
+
+/**
+ * @namespace error_logging_utils
+ *
+ * Contains utility functions for logging errors.
+ */
+namespace phosphor::power::regulators::error_logging_utils
+{
+
+/**
+ * Logs an error based on the specified exception and any nested inner
+ * exceptions.
+ *
+ * @param eptr exception pointer
+ * @param severity severity level
+ * @param services system services like error logging and the journal
+ */
+void logError(std::exception_ptr eptr, Entry::Level severity,
+              Services& services);
+
+/**
+ * Logs an error, if necessary, based on the specified exception and any nested
+ * inner exceptions.
+ *
+ * Finds the error type would be logged based on the specified exception and any
+ * nested inner exceptions.
+ *
+ * Checks to see if this error type has already been logged according to the
+ * specified ErrorHistory object.
+ *
+ * If the error type has not been logged, an error log entry is created, and the
+ * ErrorHistory is updated.
+ *
+ * If the error type has been logged, no further action is taken.
+ *
+ * @param eptr exception pointer
+ * @param severity severity level
+ * @param services system services like error logging and the journal
+ * @param history error logging history
+ */
+void logError(std::exception_ptr eptr, Entry::Level severity,
+              Services& services, ErrorHistory& history);
+
+/*
+ * Internal implementation details
+ */
+namespace internal
+{
+
+/**
+ * Returns the exception to use when logging an error.
+ *
+ * Inspects the specified exception and any nested inner exceptions.  Returns
+ * the highest priority exception from an error logging perspective.
+ *
+ * @param eptr exception pointer
+ * @return exception to log
+ */
+std::exception_ptr getExceptionToLog(std::exception_ptr eptr);
+
+} // namespace internal
+
+} // namespace phosphor::power::regulators::error_logging_utils