[Libreoffice-commits] online.git: common/Log.cpp common/Log.hpp

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Tue Apr 23 01:01:17 UTC 2019


 common/Log.cpp |   15 +++++++++++++++
 common/Log.hpp |   35 ++++++++++++++++++-----------------
 2 files changed, 33 insertions(+), 17 deletions(-)

New commits:
commit ce78fec310ab5ab6aecabb73cad7d782afcb885f
Author:     Ashod Nakashian <ashod.nakashian at collabora.co.uk>
AuthorDate: Sat Apr 20 11:39:20 2019 -0400
Commit:     Ashod Nakashian <ashnakash at gmail.com>
CommitDate: Tue Apr 23 03:01:00 2019 +0200

    wsd: reuse ostringstream when logging
    
    This is faster and reduces memory fragmentation.
    
    Also, cleans up the logging macros and implementation.
    
    Change-Id: I7fb00da041d1261c694c4b48b67a3c66ad0cbf8d
    Reviewed-on: https://gerrit.libreoffice.org/71020
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/common/Log.cpp b/common/Log.cpp
index b892c5b4b..46cfd6095 100644
--- a/common/Log.cpp
+++ b/common/Log.cpp
@@ -126,6 +126,21 @@ namespace Log
         return buffer;
     }
 
+    // Reuse the same buffer to minimize memory fragmentation.
+    static thread_local std::ostringstream Oss;
+
+    std::ostringstream& begin(const char* level)
+    {
+        // Reset the oss.
+        Oss.clear();
+        Oss.seekp(0);
+
+        // Output the prefix.
+        char buffer[1024];
+        Oss << Log::prefix(buffer, sizeof(buffer) - 1, level) << std::boolalpha;
+        return Oss;
+    }
+
     void signalLogPrefix()
     {
         char buffer[1024];
diff --git a/common/Log.hpp b/common/Log.hpp
index bdc654b39..f4a51d8da 100644
--- a/common/Log.hpp
+++ b/common/Log.hpp
@@ -54,6 +54,9 @@ namespace Log
 
     char* prefix(char* buffer, std::size_t len, const char* level);
 
+    /// Starts logging by generating the prefix and returning an oss.
+    std::ostringstream& begin(const char* level);
+
     inline bool traceEnabled() { return logger().trace(); }
     inline bool debugEnabled() { return logger().debug(); }
     inline bool infoEnabled() { return logger().information(); }
@@ -237,31 +240,29 @@ namespace Log
 #define LOG_FILE_NAME(f) (strrchr(f, '/')+1)
 #endif
 
-#define LOG_END(LOG, FILEP)                             \
-    do                                                  \
-    {                                                   \
-        if (FILEP)                                      \
-            LOG << "| " << LOG_FILE_NAME(__FILE__) << ':' << __LINE__; \
+#define LOG_END(LOG, FILEP)                                                                        \
+    do                                                                                             \
+    {                                                                                              \
+        if (FILEP)                                                                                 \
+            LOG << "| " << LOG_FILE_NAME(__FILE__) << ':' << __LINE__;                             \
     } while (false)
 
 #ifdef __ANDROID__
 
-#define LOG_BODY_(LOG, PRIO, LVL, X, FILEP)                                                 \
-    char b_[1024];                                                                          \
-    std::ostringstream oss_(Log::prefix(b_, sizeof(b_) - 1, LVL), std::ostringstream::ate); \
-    oss_ << std::boolalpha << X;                                                            \
-    LOG_END(oss_, FILEP);                                                                   \
+#define LOG_BODY_(LOG, PRIO, LVL, X, FILEP)                                                        \
+    std::ostringstream& oss_ = Log::begin(LVL);                                                    \
+    oss_ << X;                                                                                     \
+    LOG_END(oss_, FILEP);                                                                          \
     ((void)__android_log_print(ANDROID_LOG_DEBUG, "loolwsd", "%s %s", LVL, oss_.str().c_str()))
 
 #else
 
-#define LOG_BODY_(LOG, PRIO, LVL, X, FILEP)                                                 \
-    Poco::Message m_(LOG.name(), "", Poco::Message::PRIO_##PRIO);                           \
-    char b_[1024];                                                                          \
-    std::ostringstream oss_(Log::prefix(b_, sizeof(b_) - 1, LVL), std::ostringstream::ate); \
-    oss_ << std::boolalpha << X;                                                            \
-    LOG_END(oss_, FILEP);                                                                   \
-    m_.setText(oss_.str());                                                                 \
+#define LOG_BODY_(LOG, PRIO, LVL, X, FILEP)                                                        \
+    Poco::Message m_(LOG.name(), "", Poco::Message::PRIO_##PRIO);                                  \
+    std::ostringstream& oss_ = Log::begin(LVL);                                                    \
+    oss_ << X;                                                                                     \
+    LOG_END(oss_, FILEP);                                                                          \
+    m_.setText(oss_.str());                                                                        \
     LOG.log(m_);
 
 #endif


More information about the Libreoffice-commits mailing list