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

Ashod Nakashian ashod.nakashian at collabora.co.uk
Mon Aug 8 03:58:27 UTC 2016


 loolwsd/LOOLWSD.cpp |   29 ++++++++++++++++++++++++++++-
 loolwsd/Log.cpp     |   39 +++++++++++++++++++++++++++++++++------
 loolwsd/Log.hpp     |    6 +++++-
 3 files changed, 66 insertions(+), 8 deletions(-)

New commits:
commit 0c91e3313361b637d4fe2116064e104ce72cac16
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Sun Aug 7 23:32:39 2016 -0400

    loolwsd: logging to file support
    
    Change-Id: Ie18c6d7c3563078becb9d42e81ef3b6a14288e98
    Reviewed-on: https://gerrit.libreoffice.org/27976
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/loolwsd/LOOLWSD.cpp b/loolwsd/LOOLWSD.cpp
index c9ce709..58c1b59 100644
--- a/loolwsd/LOOLWSD.cpp
+++ b/loolwsd/LOOLWSD.cpp
@@ -1330,6 +1330,15 @@ void LOOLWSD::initialize(Application& self)
         { "storage.wopi.host[0]", "localhost" },
         { "storage.wopi.max_file_size", "0" },
         { "storage.webdav[@allow]", "false" },
+        { "logging.file[@enable]", "false" },
+        { "logging.file.property[0][@name]", "path" },
+        { "logging.file.property[0]", "loolwsd.log" },
+        { "logging.file.property[1][@name]", "rotation" },
+        { "logging.file.property[1]", "never" },
+        { "logging.file.property[2][@name]", "compress" },
+        { "logging.file.property[2]", "true" },
+        { "logging.file.property[3][@name]", "flush" },
+        { "logging.file.property[3]", "false" }
     };
 
     // Set default values, in case they are missing from the config file.
@@ -1356,7 +1365,25 @@ void LOOLWSD::initialize(Application& self)
     const auto withColor = getConfigValue<bool>(conf, "logging.color", true);
     if (withColor)
         setenv("LOOL_LOGCOLOR", "1", true);
-    Log::initialize("wsd", logLevel, withColor);
+
+    const auto logToFile = getConfigValue<bool>(conf, "logging.file[@enable]", false);
+    std::map<std::string, std::string> logProperties;
+    for (size_t i = 0; ; ++i)
+    {
+        const std::string confPath = "logging.file.property[" + std::to_string(i) + "]";
+        const auto name = config().getString(confPath + "[@name]", "");
+        if (!name.empty())
+        {
+            const auto value = config().getString(confPath, "");
+            logProperties.emplace(name, value);
+        }
+        else if (!config().has(confPath))
+        {
+            break;
+        }
+    }
+
+    Log::initialize("wsd", logLevel, withColor, logToFile, logProperties);
 
 #if ENABLE_SSL
     LOOLWSD::SSLEnabled.set(getConfigValue<bool>(conf, "ssl.enable", true));
diff --git a/loolwsd/Log.cpp b/loolwsd/Log.cpp
index dc3e0da..11db618 100644
--- a/loolwsd/Log.cpp
+++ b/loolwsd/Log.cpp
@@ -9,13 +9,18 @@
 
 #include <sys/prctl.h>
 
+#include <atomic>
 #include <cassert>
 #include <iomanip>
 #include <sstream>
 #include <string>
 
 #include <Poco/ConsoleChannel.h>
+#include <Poco/FileChannel.h>
+#include <Poco/FormattingChannel.h>
+#include <Poco/PatternFormatter.h>
 #include <Poco/Process.h>
+#include <Poco/SplitterChannel.h>
 #include <Poco/Thread.h>
 #include <Poco/Timestamp.h>
 
@@ -25,10 +30,12 @@ static char LogPrefix[256] = { '\0' };
 
 namespace Log
 {
+    using namespace Poco;
+
     static const Poco::Int64 epochStart = Poco::Timestamp().epochMicroseconds();
     // help avoid destruction ordering issues.
     struct StaticNames {
-        bool inited;
+        std::atomic<bool> inited;
         std::string name;
         std::string id;
         StaticNames() :
@@ -101,7 +108,11 @@ namespace Log
         signalLog(buffer);
     }
 
-    void initialize(const std::string& name, const std::string& logLevel, const bool withColor)
+    void initialize(const std::string& name,
+                    const std::string& logLevel,
+                    const bool withColor,
+                    const bool logToFile,
+                    std::map<std::string, std::string> config)
     {
         Source.name = name;
         std::ostringstream oss;
@@ -112,11 +123,27 @@ namespace Log
         strncpy(LogPrefix, oss.str().c_str(), sizeof(LogPrefix));
 
         // Configure the logger.
-        auto channel = (isatty(fileno(stderr)) || withColor
-                     ? static_cast<Poco::Channel*>(new Poco::ColorConsoleChannel())
-                     : static_cast<Poco::Channel*>(new Poco::ConsoleChannel()));
+        AutoPtr<Channel> channelConsole = (isatty(fileno(stderr)) || withColor
+                            ? static_cast<Poco::Channel*>(new Poco::ColorConsoleChannel())
+                            : static_cast<Poco::Channel*>(new Poco::ConsoleChannel()));
+
+        AutoPtr<Channel> channel = channelConsole;
+        if (logToFile)
+        {
+            AutoPtr<SplitterChannel> splitterChannel(new SplitterChannel());
+            splitterChannel->addChannel(channelConsole);
+
+            AutoPtr<FileChannel> rotatedFileChannel(new FileChannel("loolwsd.log"));
+            for (const auto& pair : config)
+            {
+                rotatedFileChannel->setProperty(pair.first, pair.second);
+            }
+
+            splitterChannel->addChannel(rotatedFileChannel);
+            channel = splitterChannel;
+        }
+
         auto& logger = Poco::Logger::create(Source.name, channel, Poco::Message::PRIO_TRACE);
-        channel->release();
 
         logger.setLevel(logLevel.empty() ? std::string("trace") : logLevel);
 
diff --git a/loolwsd/Log.hpp b/loolwsd/Log.hpp
index daa02dc..f129b63 100644
--- a/loolwsd/Log.hpp
+++ b/loolwsd/Log.hpp
@@ -18,7 +18,11 @@
 
 namespace Log
 {
-    void initialize(const std::string& name, const std::string& logLevel = "trace", const bool withColor = true);
+    void initialize(const std::string& name,
+                    const std::string& logLevel = "trace",
+                    const bool withColor = true,
+                    const bool logToFile = false,
+                    std::map<std::string, std::string> config = {});
     Poco::Logger& logger();
     std::string prefix();
 


More information about the Libreoffice-commits mailing list