From 1636981a384da94401c123cdb2dab43bbb765ade Mon Sep 17 00:00:00 2001
From: Roman Tolchenov <roman.tolchenov@stfc.ac.uk>
Date: Tue, 17 Jul 2018 16:04:42 +0100
Subject: [PATCH] Allow loading of logs after duplicate detected

Re #22963.
---
 Framework/DataHandling/src/LoadLog.cpp    | 32 ++++++++++------
 Framework/DataHandling/test/LoadLogTest.h | 45 ++++++++++++++++++++---
 2 files changed, 59 insertions(+), 18 deletions(-)

diff --git a/Framework/DataHandling/src/LoadLog.cpp b/Framework/DataHandling/src/LoadLog.cpp
index 5c903033ff4..b312bf2a832 100644
--- a/Framework/DataHandling/src/LoadLog.cpp
+++ b/Framework/DataHandling/src/LoadLog.cpp
@@ -39,6 +39,24 @@ using API::WorkspaceProperty;
 using DataObjects::Workspace2D_sptr;
 using Types::Core::DateAndTime;
 
+namespace {
+
+template <class MapClass, class LoggerType>
+void addLogDataToRun(Mantid::API::Run &run, MapClass &aMap,
+                     LoggerType &logger) {
+  for (auto &itr : aMap) {
+    try {
+      run.addLogData(itr.second.release());
+    } catch (std::invalid_argument &e) {
+      logger.warning() << e.what() << '\n';
+    } catch (Exception::ExistsError &e) {
+      logger.warning() << e.what() << '\n';
+    }
+  }
+}
+
+} // namespace
+
 /// Empty default constructor
 LoadLog::LoadLog() {}
 
@@ -288,18 +306,8 @@ void LoadLog::loadThreeColumnLogFile(std::ifstream &logFileStream,
       }
     }
   }
-  try {
-    for (auto &itr : dMap) {
-      run.addLogData(itr.second.release());
-    }
-    for (auto &sitr : sMap) {
-      run.addLogData(sitr.second.release());
-    }
-  } catch (std::invalid_argument &e) {
-    g_log.warning() << e.what();
-  } catch (Exception::ExistsError &e) {
-    g_log.warning() << e.what();
-  }
+  addLogDataToRun(run, dMap, g_log);
+  addLogDataToRun(run, sMap, g_log);
 }
 
 /**
diff --git a/Framework/DataHandling/test/LoadLogTest.h b/Framework/DataHandling/test/LoadLogTest.h
index 82385321870..13212a39304 100644
--- a/Framework/DataHandling/test/LoadLogTest.h
+++ b/Framework/DataHandling/test/LoadLogTest.h
@@ -3,23 +3,25 @@
 
 #include <cxxtest/TestSuite.h>
 
-#include "MantidDataHandling/LoadLog.h"
-#include "MantidAPI/WorkspaceFactory.h"
-#include "MantidGeometry/Instrument.h"
-#include "MantidDataObjects/Workspace2D.h"
+#include "MantidAPI/Algorithm.h"
 #include "MantidAPI/AnalysisDataService.h"
-#include "MantidKernel/Exception.h"
 #include "MantidAPI/FrameworkManager.h"
 #include "MantidAPI/Workspace.h"
-#include "MantidAPI/Algorithm.h"
+#include "MantidAPI/WorkspaceFactory.h"
+#include "MantidDataHandling/LoadLog.h"
+#include "MantidDataObjects/Workspace2D.h"
+#include "MantidGeometry/Instrument.h"
 #include "MantidGeometry/Instrument/Component.h"
+#include "MantidKernel/Exception.h"
 #include "MantidKernel/TimeSeriesProperty.h"
+#include "MantidTestHelpers/ScopedFileHelper.h"
 #include <vector>
 
 using namespace Mantid::API;
 using namespace Mantid::Kernel;
 using namespace Mantid::DataHandling;
 using namespace Mantid::DataObjects;
+using namespace ScopedFileHelper;
 using Mantid::Types::Core::DateAndTime;
 
 class LoadLogTest : public CxxTest::TestSuite {
@@ -135,6 +137,37 @@ public:
     AnalysisDataService::Instance().remove(outputSpace);
   }
 
+  void test_log_file_has_error() {
+    std::string logFileText("2007-11-16T13:25:48 i1 0 \n"
+                            "2007-11-16T13:29:36 str1  a\n"
+                            "2007-11-16T13:29:49 i2 1\n"
+                            "2007-11-16T13:30:21 str2  b\n"
+                            "2007-11-16T13:32:38 num1 3\n"
+                            "2007-11-16T13:43:40 nspectra 12\n"
+                            "2007-11-16T13:44:33 num2 4\n"
+                            "2007-11-16T14:00:21 str3 c\n");
+    ScopedFile file(logFileText, "test_log_file.log");
+    MatrixWorkspace_sptr ws =
+        WorkspaceFactory::Instance().create("Workspace2D", 1, 1, 1);
+    ws->mutableRun().addProperty("nspectra", 1);
+    // "nspectra" is already in the logs when LoadLog runs
+    LoadLog alg;
+    alg.initialize();
+    alg.setPropertyValue("Filename", file.getFileName());
+    alg.setProperty("Workspace", ws);
+    alg.execute();
+    auto props = ws->run().getProperties();
+    TS_ASSERT_EQUALS(props.size(), 8);
+    TS_ASSERT(ws->run().hasProperty("nspectra"));
+    TS_ASSERT(ws->run().hasProperty("i1"));
+    TS_ASSERT(ws->run().hasProperty("i2"));
+    TS_ASSERT(ws->run().hasProperty("num1"));
+    TS_ASSERT(ws->run().hasProperty("num2"));
+    TS_ASSERT(ws->run().hasProperty("str1"));
+    TS_ASSERT(ws->run().hasProperty("str2"));
+    TS_ASSERT(ws->run().hasProperty("str3"));
+  }
+
   void do_test_SNSTextFile(std::string names, std::string units, bool willFail,
                            bool createWorkspace = true,
                            std::string expectedLastUnit = "Furlongs") {
-- 
GitLab