ipmid: sdr-dbus: Reduce log spam on bitmap error

Remove the log for out_of_range error for
getSensorNumberFromPath and getPathFromSensorNumber. Log only once at
the end of looping through the sensor tree instead of for every sensor.

Tested:
The spam of
```
bimap<>: invalid key
```
is now gone.

Google-Bug-Id: 314385885
Google-Bug-Id: 323193196
Change-Id: I16565ec24c72680bf888a4679dfe0cf733e88e21
Signed-off-by: Willy Tu <wltu@google.com>
(cherry picked from commit 4fcd48fc4763d3b25554971d46b791934f371224)
diff --git a/recipes-phosphor/ipmi/phosphor-ipmi-host/0001-sdr-dbus-Reduce-log-spam-on-bitmap-error.patch b/recipes-phosphor/ipmi/phosphor-ipmi-host/0001-sdr-dbus-Reduce-log-spam-on-bitmap-error.patch
new file mode 100644
index 0000000..0033aa9
--- /dev/null
+++ b/recipes-phosphor/ipmi/phosphor-ipmi-host/0001-sdr-dbus-Reduce-log-spam-on-bitmap-error.patch
@@ -0,0 +1,119 @@
+From 23e5e365e9a94055499ad2ea90c12a720006f268 Mon Sep 17 00:00:00 2001
+From: Willy Tu <wltu@google.com>
+Date: Wed, 31 Jan 2024 17:28:34 +0000
+Subject: [PATCH] sdr-dbus: Reduce log spam on bitmap error
+
+Remove the log for out_of_range error for
+getSensorNumberFromPath and getPathFromSensorNumber. Log only once at
+the end of looping through the sensor tree instead of for every sensor.
+
+Tested:
+The spam of
+```
+bimap<>: invalid key
+```
+is now gone.
+
+Patch Tracking Bug: b/323193196
+Upstream info / review: http://go/openbmccl/69121
+Upstream-Status: Submitted
+Justification:
+  Cleanup spams making it diffcult to debug in production. Waiting for upstream review.
+
+Change-Id: Ifc22141cc5450b57ce272c46537240084d94d2aa
+Signed-off-by: Willy Tu <wltu@google.com>
+---
+ dbus-sdr/sdrutils.cpp       |  2 --
+ dbus-sdr/sensorcommands.cpp | 30 ++++++++++++++++++++++--------
+ 2 files changed, 22 insertions(+), 10 deletions(-)
+
+diff --git a/dbus-sdr/sdrutils.cpp b/dbus-sdr/sdrutils.cpp
+index 3e7b966..d148d9a 100644
+--- a/dbus-sdr/sdrutils.cpp
++++ b/dbus-sdr/sdrutils.cpp
+@@ -271,7 +271,6 @@ uint16_t getSensorNumberFromPath(const std::string& path)
+     }
+     catch (const std::out_of_range& e)
+     {
+-        phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
+         return invalidSensorNumber;
+     }
+ }
+@@ -305,7 +304,6 @@ std::string getPathFromSensorNumber(uint16_t sensorNum)
+     }
+     catch (const std::out_of_range& e)
+     {
+-        phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
+         return std::string();
+     }
+ }
+diff --git a/dbus-sdr/sensorcommands.cpp b/dbus-sdr/sensorcommands.cpp
+index 55ba6d9..5d23a19 100644
+--- a/dbus-sdr/sensorcommands.cpp
++++ b/dbus-sdr/sensorcommands.cpp
+@@ -37,6 +37,7 @@
+ #include <chrono>
+ #include <cmath>
+ #include <cstring>
++#include <format>
+ #include <iostream>
+ #include <map>
+ #include <memory>
+@@ -2512,6 +2513,7 @@ std::tuple<uint8_t,                // Total of instance sensors
+ 
+     auto& ipmiDecoratorPaths = getIpmiDecoratorPaths(ctx);
+ 
++    size_t invalidSensorNumberErrCount = 0;
+     for (const auto& sensor : sensorTree)
+     {
+         const std::string& sensorObjPath = sensor.first;
+@@ -2571,26 +2573,38 @@ std::tuple<uint8_t,                // Total of instance sensors
+                 if (entityInstanceValue == entityInstance)
+                 {
+                     auto recordId = getSensorNumberFromPath(sensorObjPath);
+-                    if (recordId != invalidSensorNumber)
++                    if (recordId == invalidSensorNumber)
+                     {
+-                        sensorList.emplace_back(sensorObjPath, sensorTypeValue,
+-                                                recordId, entityIdValue,
+-                                                entityInstanceValue);
++                        ++invalidSensorNumberErrCount;
++                        continue;
+                     }
++                    sensorList.emplace_back(sensorObjPath, sensorTypeValue,
++                                            recordId, entityIdValue,
++                                            entityInstanceValue);
+                 }
+             }
+             else if (entityInstanceValue >= instanceStart)
+             {
+                 auto recordId = getSensorNumberFromPath(sensorObjPath);
+-                if (recordId != invalidSensorNumber)
++                if (recordId == invalidSensorNumber)
+                 {
+-                    sensorList.emplace_back(sensorObjPath, sensorTypeValue,
+-                                            recordId, entityIdValue,
+-                                            entityInstanceValue);
++                    ++invalidSensorNumberErrCount;
++                    continue;
+                 }
++                sensorList.emplace_back(sensorObjPath, sensorTypeValue,
++                                        recordId, entityIdValue,
++                                        entityInstanceValue);
+             }
+         }
+     }
++    if (invalidSensorNumberErrCount != 0)
++    {
++        phosphor::logging::log<phosphor::logging::level::ERR>(
++            std::format(
++                "getSensorNumberFromPath returned invalidSensorNumber {} times",
++                invalidSensorNumberErrCount)
++                .data());
++    }
+ 
+     auto cmpFunc = [](sensorInfo first, sensorInfo second) {
+         return first.entityInstance <= second.entityInstance;
+-- 
+2.43.0.429.g432eaa2c6b-goog
+
diff --git a/recipes-phosphor/ipmi/phosphor-ipmi-host_%.bbappend b/recipes-phosphor/ipmi/phosphor-ipmi-host_%.bbappend
index 7884436..3282fd0 100644
--- a/recipes-phosphor/ipmi/phosphor-ipmi-host_%.bbappend
+++ b/recipes-phosphor/ipmi/phosphor-ipmi-host_%.bbappend
@@ -3,6 +3,7 @@
 
 FILESEXTRAPATHS:prepend:gbmc := "${THISDIR}/phosphor-ipmi-host:"
 SRC_URI:append:gbmc = " \
+  file://0001-sdr-dbus-Reduce-log-spam-on-bitmap-error.patch \
   file://0002-Support-packing-cable-presence-sensor-state-into-dis.patch \
   file://0003-Fix-ID-String-Type-Length-Code-byte-for-get-sdr-comm.patch \
 "