[Libreoffice-commits] core.git: comphelper/source desktop/source include/comphelper

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Fri Oct 26 08:22:00 UTC 2018


 comphelper/source/misc/logging.cxx          |    5 +
 desktop/source/pkgchk/unopkg/unopkg_app.cxx |   94 +++++++++++++++-------------
 include/comphelper/logging.hxx              |    4 +
 3 files changed, 62 insertions(+), 41 deletions(-)

New commits:
commit e8d130e7c6b2ff2a941372f386ee46fe8a95f218
Author:     Samuel Mehrbrodt <Samuel.Mehrbrodt at cib.de>
AuthorDate: Thu Oct 25 20:18:04 2018 +0200
Commit:     Samuel Mehrbrodt <Samuel.Mehrbrodt at cib.de>
CommitDate: Fri Oct 26 10:21:33 2018 +0200

    Log unopkg errors to log file if requested
    
    Before only the output generated by extension add/remove/... functions
    were logged to file, but not the error messages of unopkg itself.
    
    Change-Id: I4451aa51e2877ae48c453ee4791e36d970832567
    Reviewed-on: https://gerrit.libreoffice.org/62369
    Tested-by: Jenkins
    Reviewed-by: Samuel Mehrbrodt <Samuel.Mehrbrodt at cib.de>

diff --git a/comphelper/source/misc/logging.cxx b/comphelper/source/misc/logging.cxx
index 5b6c6bc6fda2..68aa28b2b3ac 100644
--- a/comphelper/source/misc/logging.cxx
+++ b/comphelper/source/misc/logging.cxx
@@ -89,6 +89,11 @@ namespace comphelper
         return false;
     }
 
+    const css::uno::Reference<css::logging::XLogger> EventLogger::getLogger()
+    {
+        return m_pImpl->getLogger();
+    }
+
 
     namespace
     {
diff --git a/desktop/source/pkgchk/unopkg/unopkg_app.cxx b/desktop/source/pkgchk/unopkg/unopkg_app.cxx
index fea990384e9c..ab812f48f269 100644
--- a/desktop/source/pkgchk/unopkg/unopkg_app.cxx
+++ b/desktop/source/pkgchk/unopkg/unopkg_app.cxx
@@ -34,12 +34,17 @@
 #include <cppuhelper/implbase.hxx>
 #include <cppuhelper/exc_hlp.hxx>
 #include <comphelper/anytostring.hxx>
+#include <comphelper/logging.hxx>
 #include <comphelper/sequence.hxx>
 #include <com/sun/star/deployment/DeploymentException.hpp>
 #include <com/sun/star/deployment/ExtensionManager.hpp>
 
 #include <com/sun/star/deployment/ui/PackageManagerDialog.hpp>
 #include <com/sun/star/lang/IllegalArgumentException.hpp>
+#include <com/sun/star/logging/ConsoleHandler.hpp>
+#include <com/sun/star/logging/FileHandler.hpp>
+#include <com/sun/star/logging/LogLevel.hpp>
+#include <com/sun/star/logging/XLogger.hpp>
 #include <com/sun/star/ucb/CommandAbortedException.hpp>
 #include <com/sun/star/ucb/CommandFailedException.hpp>
 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
@@ -53,8 +58,10 @@
 
 
 using namespace ::com::sun::star;
+using namespace ::com::sun::star::logging;
 using namespace ::com::sun::star::uno;
 using namespace ::unopkg;
+
 namespace {
 
 struct ExtensionName
@@ -92,7 +99,7 @@ const char s_usingText [] =
 "options:\n"
 " -h, --help              this help\n"
 " -V, --version           version information\n"
-" -v, --verbose           verbose output to stdout\n"
+" -v, --verbose           verbose output\n"
 " -f, --force             force overwriting existing extensions\n"
 " -s, --suppress-license  prevents showing the license\n"
 " --log-file <file>       custom log file; default: <cache-dir>/log.txt\n"
@@ -193,6 +200,9 @@ extern "C" int unopkg_main()
     OUString repository;
     OUString cmdArg;
     std::vector<OUString> cmdPackages;
+    Reference<XLogHandler> xFileHandler;
+    Reference<XLogHandler> xConsoleHandler;
+    std::unique_ptr<comphelper::EventLogger> logger;
 
     OptionInfo const * info_shared = getOptionInfo(
         s_option_infos, "shared" );
@@ -287,6 +297,31 @@ extern "C" int unopkg_main()
             }
         }
 
+        xComponentContext = getUNO(
+        option_verbose, option_shared, subcmd_gui, xLocalComponentContext );
+
+        logger.reset(new comphelper::EventLogger(xComponentContext, "unopkg"));
+        const Reference<XLogger> xLogger(logger->getLogger());
+        xLogger->setLevel(LogLevel::WARNING);
+        xConsoleHandler.set(css::logging::ConsoleHandler::create(xComponentContext));
+        xLogger->addLogHandler(xConsoleHandler);
+        xConsoleHandler->setLevel(LogLevel::WARNING);
+        xLogger->setLevel(LogLevel::WARNING);
+        if (!logFile.isEmpty())
+        {
+            xFileHandler.set(css::logging::FileHandler::create(xComponentContext, logFile));
+            xFileHandler->setLevel(LogLevel::WARNING);
+            xLogger->addLogHandler(xFileHandler);
+        }
+
+        if (option_verbose)
+        {
+            xLogger->setLevel(LogLevel::INFO);
+            xConsoleHandler->setLevel(LogLevel::INFO);
+            if (xFileHandler.is())
+                xFileHandler->setLevel(LogLevel::INFO);
+        }
+
         if (repository.isEmpty())
         {
             if (option_shared)
@@ -301,10 +336,9 @@ extern "C" int unopkg_main()
             if ( repository == "shared" ) {
                 option_shared = true;
             }
-            else if (option_shared) {
-                dp_misc::writeConsoleError(
-                    "WARNING: explicit context given!  Ignoring option " +
-                    toString( info_shared ) + "!\n" );
+            else if (option_shared)
+            {
+                logger->log(LogLevel::WARNING, "Explicit context given! Ignoring option '$1$'",  toString(info_shared));
             }
         }
 #if defined(UNX)
@@ -312,10 +346,8 @@ extern "C" int unopkg_main()
         {
             if ( !(option_shared || option_bundled || option_help) )
             {
-                dp_misc::writeConsoleError(
-                    "ERROR: cannot run "  APP_NAME  " as root without " +
-                    toString( info_shared ) + " or " + toString( info_bundled )
-                    + " option.\n");
+                logger->log(LogLevel::SEVERE, "Cannot run $1$ as root without $2$ or $3$ option.",
+                           APP_NAME, toString(info_shared), toString(info_bundled));
                 return 1;
             }
 
@@ -342,9 +374,6 @@ extern "C" int unopkg_main()
                 throw Exception("Could not delete " + extensionUnorc, nullptr);
         }
 
-        xComponentContext = getUNO(
-            option_verbose, option_shared, subcmd_gui, xLocalComponentContext );
-
         Reference<deployment::XExtensionManager> xExtensionManager(
             deployment::ExtensionManager::get( xComponentContext ) );
 
@@ -544,61 +573,44 @@ extern "C" int unopkg_main()
         }
         else
         {
-            dp_misc::writeConsoleError(
-                "\nERROR: unknown sub-command " +
-                subCommand + "!\n       Use " APP_NAME " " +
-                toString(info_help) + " to print all options.\n");
+            logger->log(LogLevel::SEVERE,
+                       "Unknown sub-command: '$1$'. Use $2$ $3$ to print all options.",
+                       subCommand, APP_NAME, toString(info_help));
             return 1;
         }
 
-        if (option_verbose)
-            dp_misc::writeConsole("\n" APP_NAME " done.\n");
+        logger->log(LogLevel::INFO, "$1$ done.", APP_NAME);
         //Force to release all bridges which connect us to the child processes
         dp_misc::disposeBridges(xLocalComponentContext);
         return 0;
     }
     catch (const ucb::CommandFailedException &e)
     {
-        dp_misc::writeConsoleError(e.Message + "\n");
+        logger->log(LogLevel::SEVERE, "Exception occurred: $1$", e.Message);
         bNoOtherErrorMsg = true;
     }
     catch (const ucb::CommandAbortedException &)
     {
-        dp_misc::writeConsoleError("\n" APP_NAME " aborted!\n");
+        logger->log(LogLevel::SEVERE, "$1$ aborted.", APP_NAME);
     }
     catch (const deployment::DeploymentException & exc)
     {
-        OUString cause;
-        if (option_verbose)
-        {
-            cause = ::comphelper::anyToString(exc.Cause);
-        }
-        else
-        {
-            css::uno::Exception e;
-            if (exc.Cause >>= e)
-                cause = e.Message;
-        }
-
-        dp_misc::writeConsoleError("\nERROR: " + exc.Message + "\n");
-        if (!cause.isEmpty())
-            dp_misc::writeConsoleError("       Cause: " + cause + "\n");
+        logger->log(LogLevel::SEVERE, "Exception occurred: $1$", exc.Message);
+        logger->log(LogLevel::INFO, "    Cause: $1$", comphelper::anyToString(exc.Cause));
     }
     catch (const LockFileException & e)
     {
-        if (!subcmd_gui)
-            dp_misc::writeConsoleError(e.Message + "\n");
+        logger->log(LogLevel::SEVERE, "Exception occurred: $1$", e.Message);
         bNoOtherErrorMsg = true;
     }
     catch (const css::uno::Exception & e ) {
         Any exc( ::cppu::getCaughtException() );
 
-        dp_misc::writeConsoleError("\nERROR: " +
-            (option_verbose ? e.Message + "\nException details: \n" +
-            ::comphelper::anyToString(exc) : e.Message) + "\n");
+        logger->log(LogLevel::SEVERE, "Exception occurred: $1$", e.Message);
+        logger->log(LogLevel::INFO, "    Cause: $1$", comphelper::anyToString(exc));
     }
     if (!bNoOtherErrorMsg)
-        dp_misc::writeConsoleError("\n" APP_NAME " failed.\n");
+        logger->log(LogLevel::SEVERE, "$1$ failed.", APP_NAME);
     dp_misc::disposeBridges(xLocalComponentContext);
     return 1;
 }
diff --git a/include/comphelper/logging.hxx b/include/comphelper/logging.hxx
index 475f067062bf..8c1ddbd7fec0 100644
--- a/include/comphelper/logging.hxx
+++ b/include/comphelper/logging.hxx
@@ -26,6 +26,8 @@
 #include <boost/optional.hpp>
 #include <memory>
 
+#include <com/sun/star/logging/XLogger.hpp>
+
 namespace com { namespace sun { namespace star { namespace uno { template <class interface_type> class Reference; } } } }
 namespace com { namespace sun { namespace star { namespace uno { class XComponentContext; } } } }
 
@@ -123,6 +125,8 @@ namespace comphelper
                 impl_log(_nLogLevel, nullptr, nullptr, rMessage);
         }
 
+        const css::uno::Reference<css::logging::XLogger> getLogger();
+
         /** logs a given message, replacing a placeholder in the message with an argument
 
             The function takes, additionally to the log level and the message, an arbitrary


More information about the Libreoffice-commits mailing list