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

Ashod Nakashian ashod.nakashian at collabora.co.uk
Fri Apr 15 01:07:31 UTC 2016


 loolwsd/Log.cpp     |  135 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 loolwsd/Log.hpp     |  121 ++++++++++++++++++++++++++++++++++++++++++++++
 loolwsd/Makefile.am |   19 ++++---
 loolwsd/Util.cpp    |  117 ---------------------------------------------
 loolwsd/Util.hpp    |  103 ---------------------------------------
 5 files changed, 272 insertions(+), 223 deletions(-)

New commits:
commit 322a3c1afe83a88f6bd3519eb504525f9016cc84
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Thu Apr 14 20:12:43 2016 -0400

    loolwsd: logging moved to own files
    
    Change-Id: Idf1dd91cf37675e1bea8aeb5d9e8cf6d2ac9a725
    Reviewed-on: https://gerrit.libreoffice.org/24099
    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
new file mode 100644
index 0000000..48fe800
--- /dev/null
+++ b/loolwsd/Log.cpp
@@ -0,0 +1,135 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <sys/prctl.h>
+
+#include <cassert>
+#include <iomanip>
+#include <sstream>
+#include <string>
+
+#include <Poco/ConsoleChannel.h>
+#include <Poco/Process.h>
+#include <Poco/Thread.h>
+
+#include "Log.hpp"
+
+static char LogPrefix[256] = { '\0' };
+
+namespace Log
+{
+    static const Poco::Int64 epochStart = Poco::Timestamp().epochMicroseconds();
+    // help avoid destruction ordering issues.
+    struct StaticNames {
+        bool inited;
+        std::string name;
+        std::string id;
+        StaticNames() :
+            inited(true)
+        {
+        }
+        ~StaticNames()
+        {
+            inited = false;
+        }
+    };
+    static StaticNames Source;
+
+    std::string prefix()
+    {
+        Poco::Int64 usec = Poco::Timestamp().epochMicroseconds() - epochStart;
+
+        const Poco::Int64 one_s = 1000000;
+        const Poco::Int64 hours = usec / (one_s*60*60);
+        usec %= (one_s*60*60);
+        const Poco::Int64 minutes = usec / (one_s*60);
+        usec %= (one_s*60);
+        const Poco::Int64 seconds = usec / (one_s);
+        usec %= (one_s);
+
+        std::ostringstream stream;
+        stream << (Source.inited ? Source.id : std::string())
+               << '-' << std::setw(2) << std::setfill('0')
+               << (Poco::Thread::current() ? Poco::Thread::current()->id() : 0) << ' '
+               << std::setw(2) << hours << ':' << std::setw(2) << minutes << ':'
+               << std::setw(2) << seconds << "." << std::setw(6) << usec
+               << ' ';
+
+        char buf[32]; // we really need only 16
+        if (prctl(PR_GET_NAME, reinterpret_cast<unsigned long>(buf), 0, 0, 0) == 0)
+            stream << '[' << std::setw(15) << std::setfill(' ') << std::left << buf << "] ";
+
+        return stream.str();
+    }
+
+    void initialize(const std::string& name)
+    {
+        Source.name = name;
+        std::ostringstream oss;
+        oss << Source.name << '-'
+            << std::setw(5) << std::setfill('0') << Poco::Process::id();
+        Source.id = oss.str();
+        assert (sizeof (LogPrefix) > strlen(oss.str().c_str()) + 1);
+        strncpy(LogPrefix, oss.str().c_str(), sizeof(LogPrefix));
+
+        auto channel = (isatty(fileno(stdout)) || std::getenv("LOOL_LOGCOLOR")
+                     ? static_cast<Poco::Channel*>(new Poco::ColorConsoleChannel())
+                     : static_cast<Poco::Channel*>(new Poco::ConsoleChannel()));
+        auto& logger = Poco::Logger::create(Source.name, channel, Poco::Message::PRIO_TRACE);
+        channel->release();
+
+        // Configure the logger.
+        // TODO: This should come from a file.
+        // See Poco::Logger::setLevel docs for values.
+        // Try: error, information, debug
+        char *loglevel = std::getenv("LOOL_LOGLEVEL");
+        if (loglevel)
+            logger.setLevel(std::string(loglevel));
+
+        info("Initializing " + name);
+        info("Log level is [" + std::to_string(logger.getLevel()) + "].");
+    }
+
+    Poco::Logger& logger()
+    {
+        return Poco::Logger::get(Source.inited ? Source.name : std::string());
+    }
+
+    void trace(const std::string& msg)
+    {
+        logger().trace(prefix() + msg);
+    }
+
+    void debug(const std::string& msg)
+    {
+        logger().debug(prefix() + msg);
+    }
+
+    void info(const std::string& msg)
+    {
+        logger().information(prefix() + msg);
+    }
+
+    void warn(const std::string& msg)
+    {
+        logger().warning(prefix() + msg);
+    }
+
+    void error(const std::string& msg)
+    {
+        logger().error(prefix() + msg);
+    }
+
+    void syserror(const std::string& msg)
+    {
+        logger().error(prefix() + msg + " (errno: " + std::string(std::strerror(errno)) + ")");
+    }
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/loolwsd/Log.hpp b/loolwsd/Log.hpp
new file mode 100644
index 0000000..b64f766
--- /dev/null
+++ b/loolwsd/Log.hpp
@@ -0,0 +1,121 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#ifndef INCLUDED_LOG_HPP
+#define INCLUDED_LOG_HPP
+
+#include <string>
+#include <sstream>
+#include <functional>
+
+#include <Poco/Logger.h>
+
+namespace Log
+{
+    void initialize(const std::string& name);
+    Poco::Logger& logger();
+    std::string prefix();
+
+    void trace(const std::string& msg);
+    void debug(const std::string& msg);
+    void info(const std::string& msg);
+    void warn(const std::string& msg);
+    void error(const std::string& msg);
+    void syserror(const std::string& msg);
+
+    /// The following is to write streaming logs.
+    /// Log::info() << "Value: 0x" << std::hex << value
+    ///             << ", pointer: " << this << Log::end;
+    static const struct _end_marker
+    {
+        _end_marker()
+        {
+        }
+    } end;
+
+    class StreamLogger
+    {
+        public:
+            StreamLogger(std::function<void(const std::string&)> func)
+              : _func(func)
+            {
+            }
+
+            StreamLogger(StreamLogger&& sl)
+              : _stream(std::move(sl._stream.str()))
+              , _func(std::move(sl._func))
+            {
+            }
+
+            void flush() const
+            {
+                _func(_stream.str());
+            }
+
+            std::ostringstream _stream;
+
+        private:
+            std::function<void(const std::string&)> _func;
+    };
+
+    inline
+    StreamLogger trace()
+    {
+        return StreamLogger([](const std::string& msg) { trace(msg);});
+    }
+
+    inline
+    StreamLogger debug()
+    {
+        return StreamLogger([](const std::string& msg) { debug(msg);});
+    }
+
+    inline
+    StreamLogger info()
+    {
+        return StreamLogger([](const std::string& msg) { info(msg);});
+    }
+
+    inline
+    StreamLogger warn()
+    {
+        return StreamLogger([](const std::string& msg) { warn(msg);});
+    }
+
+    inline
+    StreamLogger error()
+    {
+        return StreamLogger([](const std::string& msg) { error(msg);});
+    }
+
+    template <typename U>
+    StreamLogger& operator <<(StreamLogger& lhs, const U& rhs)
+    {
+        lhs._stream << rhs;
+        return lhs;
+    }
+
+    template <typename U>
+    StreamLogger& operator <<(StreamLogger&& lhs, U&& rhs)
+    {
+        lhs._stream << rhs;
+        return lhs;
+    }
+
+    inline
+    void operator <<(StreamLogger& lhs, const _end_marker&)
+    {
+        (void)end;
+        lhs.flush();
+    }
+}
+
+#endif
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/loolwsd/Makefile.am b/loolwsd/Makefile.am
index d5feb3a..b11426b 100644
--- a/loolwsd/Makefile.am
+++ b/loolwsd/Makefile.am
@@ -27,15 +27,16 @@ AM_ETAGSFLAGS = --c++-kinds=+p --fields=+iaS --extra=+q -R --totals=yes *
 AM_CTAGSFLAGS = $(AM_ETAGSFLAGS)
 
 shared_sources = ChildProcessSession.cpp \
-	  	 IoUtil.cpp \
+                 IoUtil.cpp \
+				 Log.cpp \
                  LOOLProtocol.cpp \
                  LOOLSession.cpp \
                  MessageQueue.cpp \
-		 Unit.cpp \
+                 Unit.cpp \
                  Util.cpp
 
 loolwsd_SOURCES = Admin.cpp \
-		  AdminModel.cpp \
+                  AdminModel.cpp \
                   Auth.cpp \
                   DocumentBroker.cpp \
                   LOOLWSD.cpp \
@@ -49,20 +50,23 @@ noinst_PROGRAMS = connect \
                   lokitclient
 
 loadtest_SOURCES = LoadTest.cpp \
+                   Log.cpp \
                    LOOLProtocol.cpp \
                    Util.cpp
 
 connect_SOURCES = Connect.cpp \
+                  Log.cpp \
                   LOOLProtocol.cpp \
                   Util.cpp
 
 lokitclient_SOURCES = IoUtil.cpp \
+                      Log.cpp \
                       LOKitClient.cpp \
                       LOOLProtocol.cpp \
                       Util.cpp
 
 loolforkit_SOURCES = LOOLForKit.cpp \
-		     LOOLKit.cpp \
+                     LOOLKit.cpp \
                      $(shared_sources)
 
 loolmount_SOURCES = loolmount.c
@@ -80,8 +84,9 @@ noinst_HEADERS = Admin.hpp \
                  FileServer.hpp \
                  IoUtil.hpp \
                  LoadTest.hpp \
+				 Log.hpp \
                  LOKitHelper.hpp \
-		 LOOLKit.hpp \
+                 LOOLKit.hpp \
                  LOOLProtocol.hpp \
                  LOOLSession.hpp \
                  LOOLWSD.hpp \
@@ -92,8 +97,8 @@ noinst_HEADERS = Admin.hpp \
                  Rectangle.hpp \
                  Storage.hpp \
                  TileCache.hpp \
-		 Unit.hpp \
-	         UnitHTTP.hpp \
+                 Unit.hpp \
+                 UnitHTTP.hpp \
                  Util.hpp \
                  bundled/include/LibreOfficeKit/LibreOfficeKit.h \
                  bundled/include/LibreOfficeKit/LibreOfficeKitEnums.h \
diff --git a/loolwsd/Util.cpp b/loolwsd/Util.cpp
index eaea899..32b9072 100644
--- a/loolwsd/Util.cpp
+++ b/loolwsd/Util.cpp
@@ -92,119 +92,6 @@ namespace rng
 }
 }
 
-static char LogPrefix[256] = { '\0' };
-
-namespace Log
-{
-    static const Poco::Int64 epochStart = Poco::Timestamp().epochMicroseconds();
-    // help avoid destruction ordering issues.
-    struct StaticNames {
-        bool inited;
-        std::string name;
-        std::string id;
-        StaticNames() :
-            inited(true)
-        {
-        }
-        ~StaticNames()
-        {
-            inited = false;
-        }
-    };
-    static StaticNames Source;
-
-    std::string logPrefix()
-    {
-        Poco::Int64 usec = Poco::Timestamp().epochMicroseconds() - epochStart;
-
-        const Poco::Int64 one_s = 1000000;
-        const Poco::Int64 hours = usec / (one_s*60*60);
-        usec %= (one_s*60*60);
-        const Poco::Int64 minutes = usec / (one_s*60);
-        usec %= (one_s*60);
-        const Poco::Int64 seconds = usec / (one_s);
-        usec %= (one_s);
-
-        std::ostringstream stream;
-        stream << (Source.inited ? Source.id : std::string())
-               << '-' << std::setw(2) << std::setfill('0')
-               << (Poco::Thread::current() ? Poco::Thread::current()->id() : 0) << ' '
-               << std::setw(2) << hours << ':' << std::setw(2) << minutes << ':'
-               << std::setw(2) << seconds << "." << std::setw(6) << usec
-               << ' ';
-
-        char buf[32]; // we really need only 16
-        if (prctl(PR_GET_NAME, reinterpret_cast<unsigned long>(buf), 0, 0, 0) == 0)
-            stream << '[' << std::setw(15) << std::setfill(' ') << std::left << buf << "] ";
-
-        return stream.str();
-    }
-
-    void initialize(const std::string& name)
-    {
-        Source.name = name;
-        std::ostringstream oss;
-        oss << Source.name << '-'
-            << std::setw(5) << std::setfill('0') << Poco::Process::id();
-        Source.id = oss.str();
-        assert (sizeof (LogPrefix) > strlen(oss.str().c_str()) + 1);
-        strncpy(LogPrefix, oss.str().c_str(), sizeof(LogPrefix));
-
-        auto channel = (isatty(fileno(stdout)) || std::getenv("LOOL_LOGCOLOR")
-                     ? static_cast<Poco::Channel*>(new Poco::ColorConsoleChannel())
-                     : static_cast<Poco::Channel*>(new Poco::ConsoleChannel()));
-        auto& logger = Poco::Logger::create(Source.name, channel, Poco::Message::PRIO_TRACE);
-        channel->release();
-
-        // Configure the logger.
-        // TODO: This should come from a file.
-        // See Poco::Logger::setLevel docs for values.
-        // Try: error, information, debug
-        char *loglevel = std::getenv("LOOL_LOGLEVEL");
-        if (loglevel)
-            logger.setLevel(std::string(loglevel));
-
-        info("Initializing " + name);
-        info("Log level is [" + std::to_string(logger.getLevel()) + "].");
-    }
-
-    Poco::Logger& logger()
-    {
-        return Poco::Logger::get(Source.inited ? Source.name : std::string());
-    }
-
-    void trace(const std::string& msg)
-    {
-        logger().trace(logPrefix() + msg);
-    }
-
-    void debug(const std::string& msg)
-    {
-        logger().debug(logPrefix() + msg);
-    }
-
-    void info(const std::string& msg)
-    {
-        logger().information(logPrefix() + msg);
-    }
-
-    void warn(const std::string& msg)
-    {
-        logger().warning(logPrefix() + msg);
-    }
-
-    void error(const std::string& msg)
-    {
-        logger().error(logPrefix() + msg);
-    }
-
-    void syserror(const std::string& msg)
-    {
-        logger().error(logPrefix() + msg + " (errno: " + std::string(std::strerror(errno)) + ")");
-
-    }
-}
-
 namespace Util
 {
     std::string encodeId(const unsigned number, const int padding)
@@ -391,7 +278,7 @@ namespace Util
         {
             TerminationFlag = true;
 
-            log_signal(LogPrefix);
+            log_signal(Log::prefix().c_str());
             log_signal(" Termination signal received: ");
             log_signal(signalName(signal));
             log_signal("\n");
@@ -417,7 +304,7 @@ namespace Util
     static
     void handleFatalSignal(const int signal)
     {
-        log_signal(LogPrefix);
+        log_signal(Log::prefix().c_str());
         log_signal(" Fatal signal received: ");
         log_signal(signalName(signal));
         log_signal("\n");
diff --git a/loolwsd/Util.hpp b/loolwsd/Util.hpp
index 109a78a..46b973b 100644
--- a/loolwsd/Util.hpp
+++ b/loolwsd/Util.hpp
@@ -19,11 +19,12 @@
 #include <Poco/Path.h>
 #include <Poco/Process.h>
 #include <Poco/Net/WebSocket.h>
-#include <Poco/Logger.h>
 
 #define LOK_USE_UNSTABLE_API
 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
 
+#include "Log.hpp"
+
 /// Flag to stop pump loops.
 extern volatile bool TerminationFlag;
 
@@ -115,106 +116,6 @@ namespace Util
     bool hasCorrectUID();
 };
 
-//TODO: Move to own file.
-namespace Log
-{
-    void initialize(const std::string& name);
-    Poco::Logger& logger();
-
-    void trace(const std::string& msg);
-    void debug(const std::string& msg);
-    void info(const std::string& msg);
-    void warn(const std::string& msg);
-    void error(const std::string& msg);
-    void syserror(const std::string& msg);
-
-    /// The following is to write streaming logs.
-    /// Log::info() << "Value: 0x" << std::hex << value
-    ///             << ", pointer: " << this << Log::end;
-    static const struct _end_marker
-    {
-        _end_marker()
-        {
-        }
-    } end;
-
-    class StreamLogger
-    {
-        public:
-            StreamLogger(std::function<void(const std::string&)> func)
-              : _func(func)
-            {
-            }
-
-            StreamLogger(StreamLogger&& sl)
-              : _stream(std::move(sl._stream.str()))
-              , _func(std::move(sl._func))
-            {
-            }
-
-            void flush() const
-            {
-                _func(_stream.str());
-            }
-
-            std::ostringstream _stream;
-
-        private:
-            std::function<void(const std::string&)> _func;
-    };
-
-    inline
-    StreamLogger trace()
-    {
-        return StreamLogger([](const std::string& msg) { trace(msg);});
-    }
-
-    inline
-    StreamLogger debug()
-    {
-        return StreamLogger([](const std::string& msg) { debug(msg);});
-    }
-
-    inline
-    StreamLogger info()
-    {
-        return StreamLogger([](const std::string& msg) { info(msg);});
-    }
-
-    inline
-    StreamLogger warn()
-    {
-        return StreamLogger([](const std::string& msg) { warn(msg);});
-    }
-
-    inline
-    StreamLogger error()
-    {
-        return StreamLogger([](const std::string& msg) { error(msg);});
-    }
-
-    template <typename U>
-    StreamLogger& operator <<(StreamLogger& lhs, const U& rhs)
-    {
-        lhs._stream << rhs;
-        return lhs;
-    }
-
-    template <typename U>
-    StreamLogger& operator <<(StreamLogger&& lhs, U&& rhs)
-    {
-        lhs._stream << rhs;
-        return lhs;
-    }
-
-    inline
-    void operator <<(StreamLogger& lhs, const _end_marker&)
-    {
-        (void)end;
-        lhs.flush();
-    }
-}
-
 #endif
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list