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

Ashod Nakashian ashod.nakashian at collabora.co.uk
Mon Oct 31 06:09:06 UTC 2016


 loolwsd/Log.cpp |   20 +++++++++++++-------
 loolwsd/Log.hpp |   40 +++++++++++++++++++++++++++++++++-------
 2 files changed, 46 insertions(+), 14 deletions(-)

New commits:
commit b14961ffaf8488f2d64503120f5d074c8aaa09a3
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Sun Oct 30 19:07:41 2016 -0400

    loolwsd: logging cleanup
    
    Streaming to string can now be disabled when
    the not needed to reduce overhead.
    
    Change-Id: I95e5a700776bd9f05b0d969703fc471401dfea3b
    Reviewed-on: https://gerrit.libreoffice.org/30420
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/loolwsd/Log.cpp b/loolwsd/Log.cpp
index e9b9576..c263975 100644
--- a/loolwsd/Log.cpp
+++ b/loolwsd/Log.cpp
@@ -36,8 +36,10 @@ namespace Log
     using namespace Poco;
 
     static const Poco::Int64 epochStart = Poco::Timestamp().epochMicroseconds();
+
     /// Helper to avoid destruction ordering issues.
-    struct StaticNames {
+    struct StaticNames
+    {
         std::atomic<bool> inited;
         std::string name;
         std::string id;
@@ -56,9 +58,10 @@ namespace Log
     //   $ man 7 signal
     void signalLog(const char *message)
     {
-        while (true) {
-            int length = strlen(message);
-            int written = write (STDERR_FILENO, message, length);
+        while (true)
+        {
+            const int length = strlen(message);
+            const int written = write (STDERR_FILENO, message, length);
             if (written < 0)
             {
                 if (errno == EINTR)
@@ -66,6 +69,7 @@ namespace Log
                 else
                     break;
             }
+
             message += written;
             if (message[0] == '\0')
                 break;
@@ -81,17 +85,19 @@ namespace Log
 
         const Poco::Int64 one_s = 1000000;
         const Poco::Int64 hours = usec / (one_s*60*60);
-        usec %= (one_s*60*60);
+        usec %= (one_s * 60 * 60);
         const Poco::Int64 minutes = usec / (one_s*60);
-        usec %= (one_s*60);
+        usec %= (one_s * 60);
         const Poco::Int64 seconds = usec / (one_s);
         usec %= (one_s);
 
         char procName[32]; // we really need only 16
         if (prctl(PR_GET_NAME, reinterpret_cast<unsigned long>(procName), 0, 0, 0) != 0)
+        {
             strncpy(procName, "<noid>", sizeof(procName) - 1);
+        }
 
-        const char *appName = (Source.inited ? Source.id.c_str() : "<shutdown>");
+        const char* appName = (Source.inited ? Source.id.c_str() : "<shutdown>");
         assert(strlen(appName) + 32 + 28 < 1024 - 1);
 
         snprintf(buffer, 4095, "%s-%.04lu %d:%.2d:%.2d.%.6d [ %s ] %s  ", appName,
diff --git a/loolwsd/Log.hpp b/loolwsd/Log.hpp
index 6bded5e..ea3d6ee 100644
--- a/loolwsd/Log.hpp
+++ b/loolwsd/Log.hpp
@@ -35,6 +35,8 @@ namespace Log
     void fatal(const std::string& msg);
     void sysfatal(const std::string& msg);
 
+    inline bool traceEnabled() { return logger().getLevel() > Poco::Message::PRIO_TRACE; }
+
     /// Signal safe prefix logging
     void signalLogPrefix();
     /// Signal safe logging
@@ -55,31 +57,47 @@ namespace Log
     class StreamLogger
     {
     public:
+        /// No-op instance.
+        StreamLogger()
+          : _enabled(false)
+        {
+        }
+
         StreamLogger(std::function<void(const std::string&)> func)
-            : _func(std::move(func))
+          : _func(std::move(func)),
+            _enabled(true)
         {
         }
 
         StreamLogger(StreamLogger&& sl) noexcept
-            : _stream(sl._stream.str()),
-              _func(std::move(sl._func))
+          : _stream(sl._stream.str()),
+            _func(std::move(sl._func)),
+            _enabled(sl._enabled)
         {
         }
 
+        bool enabled() const { return _enabled; }
+
         void flush() const
         {
-            _func(_stream.str());
+            if (_enabled)
+            {
+                _func(_stream.str());
+            }
         }
 
         std::ostringstream _stream;
 
     private:
         std::function<void(const std::string&)> _func;
+        const bool _enabled;
     };
 
     inline StreamLogger trace()
     {
-        return StreamLogger([](const std::string& msg) { trace(msg); });
+        return traceEnabled()
+             ? StreamLogger([](const std::string& msg) { trace(msg); })
+             : StreamLogger();
     }
 
     inline StreamLogger debug()
@@ -110,14 +128,22 @@ namespace Log
     template <typename U>
     StreamLogger& operator<<(StreamLogger& lhs, const U& rhs)
     {
-        lhs._stream << rhs;
+        if (lhs.enabled())
+        {
+            lhs._stream << rhs;
+        }
+
         return lhs;
     }
 
     template <typename U>
     StreamLogger& operator<<(StreamLogger&& lhs, U&& rhs)
     {
-        lhs._stream << rhs;
+        if (lhs.enabled())
+        {
+            lhs._stream << rhs;
+        }
+
         return lhs;
     }
 


More information about the Libreoffice-commits mailing list