sensor-monitor/threshold_alarm_logger: Log sensor name

This patch introduces an optional feature to append the sensor name to
the error message description. This helps in identifying the specific
sensor that triggered the alarm, improving debugging and visibility.

Change-Id: I32942ab74ebdcf0675a35718a4140d221c3aa20a
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
Signed-off-by: Naresh Solanki <naresh.solanki@9elements.com>
diff --git a/sensor-monitor/threshold_alarm_logger.cpp b/sensor-monitor/threshold_alarm_logger.cpp
index 6b20e77..545f01e 100644
--- a/sensor-monitor/threshold_alarm_logger.cpp
+++ b/sensor-monitor/threshold_alarm_logger.cpp
@@ -359,11 +359,30 @@
 
     type.front() = toupper(type.front());
     std::string errorName = errorNameBase + type + name + status;
+    if (LOG_SENSOR_NAME_ON_ERROR != 0)
+    {
+        errorName += " on sensor " + getSensorName(sensorPath);
+    }
 
     SDBusPlus::callMethod(loggingService, loggingPath, loggingCreateIface,
                           "Create", errorName, convertForMessage(severity), ad);
 }
 
+std::string ThresholdAlarmLogger::getSensorName(const std::string& sensorPath)
+{
+    auto pos = sensorPath.find_last_of('/');
+    if ((sensorPath.back() == '/') || (pos == std::string::npos))
+    {
+        log<level::ERR>(
+            std::format("Cannot get sensor name from sensor path {}",
+                        sensorPath)
+                .c_str());
+        return "unknown_sensor";
+    }
+
+    return sensorPath.substr(pos + 1);
+}
+
 std::string ThresholdAlarmLogger::getSensorType(std::string sensorPath)
 {
     auto pos = sensorPath.find_last_of('/');