summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>2018-10-25 20:18:04 +0200
committerSamuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>2018-10-26 10:31:53 +0200
commit33fdf7bc8bfd32c3609dc12dc949602586b2d48c (patch)
treee6bcbd996108785781f73b6985af634de9d02c27
parent053653062c1e65e41101cf0a542fa8d0fb32cc31 (diff)
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. Reviewed-on: https://gerrit.libreoffice.org/62369 Tested-by: Jenkins Reviewed-by: Samuel Mehrbrodt <Samuel.Mehrbrodt@cib.de> (cherry picked from commit e8d130e7c6b2ff2a941372f386ee46fe8a95f218) Change-Id: I4451aa51e2877ae48c453ee4791e36d970832567
-rw-r--r--comphelper/source/misc/logging.cxx5
-rw-r--r--desktop/source/pkgchk/unopkg/unopkg_app.cxx95
-rw-r--r--include/comphelper/logging.hxx12
3 files changed, 69 insertions, 43 deletions
diff --git a/comphelper/source/misc/logging.cxx b/comphelper/source/misc/logging.cxx
index 452d2a365132..75555bd3d8c0 100644
--- a/comphelper/source/misc/logging.cxx
+++ b/comphelper/source/misc/logging.cxx
@@ -113,6 +113,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 822a1b9c4276..b0ee3270fcd2 100644
--- a/desktop/source/pkgchk/unopkg/unopkg_app.cxx
+++ b/desktop/source/pkgchk/unopkg/unopkg_app.cxx
@@ -34,13 +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/ExtensionManager.hpp>
#include <com/sun/star/deployment/ui/PackageManagerDialog.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/ui/dialogs/XExecutableDialog.hpp>
#include <com/sun/star/ui/dialogs/XDialogClosedListener.hpp>
-#include <com/sun/star/bridge/BridgeFactory.hpp>
#include <stdio.h>
#if defined(UNX)
#include <unistd.h>
@@ -49,8 +53,10 @@
using namespace ::com::sun::star;
+using namespace ::com::sun::star::logging;
using namespace ::com::sun::star::uno;
using namespace ::unopkg;
+
namespace {
struct ExtensionName
@@ -90,7 +96,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"
@@ -195,6 +201,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" );
@@ -289,6 +298,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)
@@ -303,10 +337,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)
@@ -314,10 +347,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;
}
@@ -344,9 +375,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 ) );
@@ -550,61 +578,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: " +
- OUString(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 ef17801aa9f1..0fd7641fb2b5 100644
--- a/include/comphelper/logging.hxx
+++ b/include/comphelper/logging.hxx
@@ -29,10 +29,11 @@
#include <boost/optional.hpp>
#include <memory>
+#include <com/sun/star/logging/XLogger.hpp>
+
namespace comphelper
{
-
//= string conversions, employed by the templatized log* members of
//= EventLogger
@@ -118,6 +119,15 @@ namespace comphelper
//- XLogger::log equivalents/wrappers
//- string messages
+ /// logs a given message, without any arguments, or source class/method names
+ void log( const sal_Int32 _nLogLevel, const OUString& rMessage ) const
+ {
+ if ( isLoggable( _nLogLevel ) )
+ 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