Replace error strings with logging errors in device operations

Change-Id: I0fc1c23e51a0233c18775ee712132dba7e4c7d7e
Signed-off-by: Dhruvaraj Subhashchandran <dhruvaraj@in.ibm.com>
diff --git a/cfam_access.cpp b/cfam_access.cpp
index 83afdc7..d1d5c8f 100644
--- a/cfam_access.cpp
+++ b/cfam_access.cpp
@@ -16,6 +16,8 @@
 #include <unistd.h>
 #include "cfam_access.hpp"
 #include "targeting.hpp"
+#include <phosphor-logging/elog.hpp>
+#include "elog-errors.hpp"
 
 namespace openpower
 {
@@ -44,15 +46,15 @@
               cfam_address_t address,
               cfam_data_t data)
 {
+    using namespace phosphor::logging;
     int rc = lseek(target->getCFAMFD(), makeOffset(address), SEEK_SET);
     if (rc < 0)
     {
-        //Future: use a different exception to create an error log
-        char msg[100];
-        sprintf(msg, "writeCFAMReg: Failed seek for address 0x%X, "
-                "processor %d.  errno = %d",
-                address, static_cast<int>(target->getPos()), errno);
-        throw std::runtime_error(msg);
+        elog<org::open_power::Proc::CFAM::SeekFailure>(
+            org::open_power::Proc::CFAM::SeekFailure::ERRNO(errno),
+            org::open_power::Proc::CFAM::SeekFailure::ADDRESS(address),
+            org::open_power::Proc::CFAM::SeekFailure::OFFSET(makeOffset(address)),
+            org::open_power::Proc::CFAM::SeekFailure::PATH(target->getCFAMPath().c_str()));
     }
 
     data = target->swapEndian(data);
@@ -60,12 +62,10 @@
     rc = write(target->getCFAMFD(), &data, cfamRegSize);
     if (rc < 0)
     {
-        //Future: use a different exception to create an error log
-        char msg[100];
-        sprintf(msg, "writeCFAMReg: Failed write to address 0x%X, "
-                "processor %d. errno = %d",
-                address, static_cast<int>(target->getPos()), errno);
-        throw std::runtime_error(msg);
+        elog<org::open_power::Proc::CFAM::WriteFailure>(
+            org::open_power::Proc::CFAM::WriteFailure::CALLOUT_ERRNO(errno),
+            org::open_power::Proc::CFAM::WriteFailure::CALLOUT_DEVICE_PATH(
+                target->getCFAMPath().c_str()));
     }
 }
 
@@ -73,28 +73,27 @@
 cfam_data_t readReg(const std::unique_ptr<Target>& target,
                     cfam_address_t address)
 {
+    using namespace phosphor::logging;
+
     cfam_data_t data = 0;
 
     int rc = lseek(target->getCFAMFD(), makeOffset(address), SEEK_SET);
     if (rc < 0)
     {
-        //Future: use a different exception to create an error log
-        char msg[100];
-        sprintf(msg, "readCFAMReg: Failed seek for address 0x%X, "
-                "processor %d.  errno = %d",
-                address, static_cast<int>(target->getPos()), errno);
-        throw std::runtime_error(msg);
+        elog<org::open_power::Proc::CFAM::SeekFailure>(
+            org::open_power::Proc::CFAM::SeekFailure::ERRNO(errno),
+            org::open_power::Proc::CFAM::SeekFailure::ADDRESS(address),
+            org::open_power::Proc::CFAM::SeekFailure::OFFSET(makeOffset(address)),
+            org::open_power::Proc::CFAM::SeekFailure::PATH(target->getCFAMPath().c_str()));
     }
 
     rc = read(target->getCFAMFD(), &data, cfamRegSize);
     if (rc < 0)
     {
-        //Future: use a different exception to create an error log
-        char msg[100];
-        sprintf(msg, "readCFAMReg: Failed read for address 0x%X, "
-                "processor %d. errno = %d",
-                address, static_cast<int>(target->getPos()), errno);
-        throw std::runtime_error(msg);
+        elog<org::open_power::Proc::CFAM::ReadFailure>(
+            org::open_power::Proc::CFAM::WriteFailure::CALLOUT_ERRNO(errno),
+            org::open_power::Proc::CFAM::WriteFailure::CALLOUT_DEVICE_PATH(
+                target->getCFAMPath().c_str()));
     }
 
     return target->swapEndian(data);
diff --git a/filedescriptor.cpp b/filedescriptor.cpp
index c8bcab0..9354d6e 100644
--- a/filedescriptor.cpp
+++ b/filedescriptor.cpp
@@ -16,6 +16,8 @@
 #include <stdexcept>
 #include <unistd.h>
 #include "filedescriptor.hpp"
+#include <phosphor-logging/elog.hpp>
+#include "elog-errors.hpp"
 
 namespace openpower
 {
@@ -24,15 +26,15 @@
 
 FileDescriptor::FileDescriptor(const std::string& path)
 {
+    using namespace phosphor::logging;
+
     fd = open(path.c_str(), O_RDWR | O_SYNC);
 
     if (fd < 0)
     {
-        //Future: use a different exception to create an error log
-        char msg[200];
-        sprintf(msg, "Failed to open FSI device path %s.  errno = %d",
-                path.c_str(), errno);
-        throw std::runtime_error(msg);
+        elog<org::open_power::Proc::CFAM::OpenFailure>(
+            org::open_power::Proc::CFAM::OpenFailure::ERRNO(errno),
+            org::open_power::Proc::CFAM::OpenFailure::PATH(path.c_str()));
     }
 }
 
diff --git a/proc_control.cpp b/proc_control.cpp
index 10c3ffa..a86b354 100644
--- a/proc_control.cpp
+++ b/proc_control.cpp
@@ -17,7 +17,9 @@
 #include <functional>
 #include <iostream>
 #include <phosphor-logging/log.hpp>
+#include <phosphor-logging/elog.hpp>
 #include "registration.hpp"
+#include "elog-errors.hpp"
 
 using namespace openpower::util;
 
@@ -34,6 +36,7 @@
 
 int main(int argc, char** argv)
 {
+    using namespace phosphor::logging;
     const ProcedureMap& procedures = Registration::getProcedures();
 
     if (argc != 2)
@@ -56,10 +59,24 @@
     {
         procedure->second();
     }
-    catch (std::exception& e)
+    catch (org::open_power::Proc::CFAM::SeekFailure& e)
     {
-        //TODO: commit an actual error that does a callout
-        phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
+        commit<org::open_power::Proc::CFAM::SeekFailure>();
+        return -1;
+    }
+    catch (org::open_power::Proc::CFAM::OpenFailure& e)
+    {
+        commit<org::open_power::Proc::CFAM::OpenFailure>();
+        return -1;
+    }
+    catch (org::open_power::Proc::CFAM::WriteFailure& e)
+    {
+        commit<org::open_power::Proc::CFAM::WriteFailure>();
+        return -1;
+    }
+    catch (org::open_power::Proc::CFAM::ReadFailure& e)
+    {
+        commit<org::open_power::Proc::CFAM::ReadFailure>();
         return -1;
     }
 
diff --git a/targeting.cpp b/targeting.cpp
index 1237025..ce3fc67 100644
--- a/targeting.cpp
+++ b/targeting.cpp
@@ -18,8 +18,11 @@
 #include <experimental/filesystem>
 #include <phosphor-logging/log.hpp>
 #include <regex>
+#include <phosphor-logging/elog.hpp>
+#include "elog-errors.hpp"
 #include "targeting.hpp"
 
+
 namespace openpower
 {
 namespace targeting
@@ -91,27 +94,35 @@
 
     //Always create P0, the FSI master.
     targets.push_back(std::make_unique<Target>(0, fsiMasterPath, swapper));
-
-    //Find the the remaining P9s dynamically based on which files show up
-    for (auto& file : fs::directory_iterator(fsiSlaveBasePath))
+    try
     {
-        std::smatch match;
-        std::string path = file.path();
-        if (std::regex_search(path, match, exp))
+        //Find the the remaining P9s dynamically based on which files show up
+        for (auto& file : fs::directory_iterator(fsiSlaveBasePath))
         {
-            auto pos = atoi(match[1].str().c_str());
-            if (pos == 0)
+            std::smatch match;
+            std::string path = file.path();
+            if (std::regex_search(path, match, exp))
             {
-                log<level::ERR>("Unexpected FSI slave device name found",
-                                entry("DEVICE_NAME=%s", path.c_str()));
-                continue;
+                auto pos = atoi(match[1].str().c_str());
+                if (pos == 0)
+                {
+                    log<level::ERR>("Unexpected FSI slave device name found",
+                                    entry("DEVICE_NAME=%s", path.c_str()));
+                    continue;
+                }
+
+                path += "/raw";
+
+                targets.push_back(std::make_unique<Target>(pos, path, swapper));
             }
-
-            path += "/raw";
-
-            targets.push_back(std::make_unique<Target>(pos, path, swapper));
         }
     }
+    catch (fs::filesystem_error& e)
+    {
+        elog<org::open_power::Proc::CFAM::OpenFailure>(
+            org::open_power::Proc::CFAM::OpenFailure::ERRNO(e.code().value()),
+            org::open_power::Proc::CFAM::OpenFailure::PATH(e.path1().c_str()));
+    }
 
     auto sortTargets = [](const std::unique_ptr<Target>& left,
                           const std::unique_ptr<Target>& right)