Browse Source

cmCTest: Reduce string copies during logging

Brad King 1 year ago
parent
commit
6fcb64d6b4
2 changed files with 8 additions and 11 deletions
  1. 5 8
      Source/cmCTest.cxx
  2. 3 3
      Source/cmCTest.h

+ 5 - 8
Source/cmCTest.cxx

@@ -3647,9 +3647,9 @@ static const char* cmCTestStringLogType[] = { "DEBUG",
                                               "WARNING",
                                               "ERROR_MESSAGE" };
 
-void cmCTest::Log(LogType logType, const char* msg, bool suppress)
+void cmCTest::Log(LogType logType, std::string msg, bool suppress)
 {
-  if (!msg || !*msg) {
+  if (msg.empty()) {
     return;
   }
   if (suppress && logType != cmCTest::ERROR_MESSAGE) {
@@ -3694,15 +3694,12 @@ void cmCTest::Log(LogType logType, const char* msg, bool suppress)
           out.flush();
         }
 
-        std::string msg_str{ msg };
-        auto const lineBreakIt = msg_str.find('\n');
-        if (lineBreakIt != std::string::npos) {
+        if (msg.find('\n') != std::string::npos) {
           this->Impl->FlushTestProgressLine = true;
-          msg_str.erase(std::remove(msg_str.begin(), msg_str.end(), '\n'),
-                        msg_str.end());
+          msg.erase(std::remove(msg.begin(), msg.end(), '\n'), msg.end());
         }
 
-        out << msg_str;
+        out << msg;
 #ifndef _WIN32
         printf("\x1B[K"); // move caret to end
 #endif

+ 3 - 3
Source/cmCTest.h

@@ -380,7 +380,7 @@ public:
   };
 
   /** Add log to the output */
-  void Log(LogType logType, const char* msg, bool suppress = false);
+  void Log(LogType logType, std::string msg, bool suppress = false);
 
   /** Color values */
   enum class Color
@@ -549,12 +549,12 @@ private:
   do {                                                                        \
     std::ostringstream cmCTestLog_msg;                                        \
     cmCTestLog_msg << msg;                                                    \
-    (ctSelf)->Log(cmCTest::logType, cmCTestLog_msg.str().c_str());            \
+    (ctSelf)->Log(cmCTest::logType, cmCTestLog_msg.str());                    \
   } while (false)
 
 #define cmCTestOptionalLog(ctSelf, logType, msg, suppress)                    \
   do {                                                                        \
     std::ostringstream cmCTestLog_msg;                                        \
     cmCTestLog_msg << msg;                                                    \
-    (ctSelf)->Log(cmCTest::logType, cmCTestLog_msg.str().c_str(), suppress);  \
+    (ctSelf)->Log(cmCTest::logType, cmCTestLog_msg.str(), suppress);          \
   } while (false)