Kaydet (Commit) e4583f1c authored tarafından Samuel Mehrbrodt's avatar Samuel Mehrbrodt

Introduce SimpleTextFormatter and format unopkg output using it

This will write log messages as plain text (no timestamp and other stuff
like PlainTextFormatter).
Warnings and errors will be prefixed accordingly.

Change-Id: Id82512d7dd3907a4c7cd69a963a375966189dc20
Reviewed-on: https://gerrit.libreoffice.org/62370
Tested-by: Jenkins
Reviewed-by: 's avatarSamuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>
üst e8d130e7
......@@ -44,6 +44,7 @@
#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/SimpleTextFormatter.hpp>
#include <com/sun/star/logging/XLogger.hpp>
#include <com/sun/star/ucb/CommandAbortedException.hpp>
#include <com/sun/star/ucb/CommandFailedException.hpp>
......@@ -300,16 +301,24 @@ extern "C" int unopkg_main()
xComponentContext = getUNO(
option_verbose, option_shared, subcmd_gui, xLocalComponentContext );
// Initialize logging. This will log errors to the console and
// also to file if the --log-file parameter was provided.
logger.reset(new comphelper::EventLogger(xComponentContext, "unopkg"));
const Reference<XLogger> xLogger(logger->getLogger());
xLogger->setLevel(LogLevel::WARNING);
xConsoleHandler.set(css::logging::ConsoleHandler::create(xComponentContext));
Reference<XLogFormatter> xLogFormatter(SimpleTextFormatter::create(xComponentContext));
Sequence < beans::NamedValue > aSeq { { "Formatter", Any(xLogFormatter) } };
xConsoleHandler.set(ConsoleHandler::createWithSettings(xComponentContext, aSeq));
xLogger->addLogHandler(xConsoleHandler);
xConsoleHandler->setLevel(LogLevel::WARNING);
xLogger->setLevel(LogLevel::WARNING);
if (!logFile.isEmpty())
{
xFileHandler.set(css::logging::FileHandler::create(xComponentContext, logFile));
Sequence < beans::NamedValue > aSeq2 { { "Formatter", Any(xLogFormatter) }, {"FileURL", Any(logFile)} };
xFileHandler.set(css::logging::FileHandler::createWithSettings(xComponentContext, aSeq2));
xFileHandler->setLevel(LogLevel::WARNING);
xLogger->addLogHandler(xFileHandler);
}
......
......@@ -26,6 +26,7 @@ $(eval $(call gb_Library_add_exception_objects,log,\
extensions/source/logging/loghandler \
extensions/source/logging/logrecord \
extensions/source/logging/plaintextformatter \
extensions/source/logging/simpletextformatter \
))
$(eval $(call gb_Library_use_libraries,log,\
......
......@@ -39,4 +39,8 @@
constructor="com_sun_star_comp_extensions_PlainTextFormatter">
<service name="com.sun.star.logging.PlainTextFormatter"/>
</implementation>
<implementation name="com.sun.star.comp.extensions.SimpleTextFormatter"
constructor="com_sun_star_comp_extensions_SimpleTextFormatter">
<service name="com.sun.star.logging.SimpleTextFormatter"/>
</implementation>
</component>
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
#include <com/sun/star/logging/XLogFormatter.hpp>
#include <com/sun/star/logging/LogLevel.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <cppuhelper/implbase.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <rtl/ustrbuf.hxx>
#include <osl/thread.h>
#include <stdio.h>
namespace logging
{
using css::logging::LogRecord;
using namespace css::uno;
class SimpleTextFormatter
: public cppu::WeakImplHelper<css::logging::XLogFormatter, css::lang::XServiceInfo>
{
public:
SimpleTextFormatter();
private:
// XLogFormatter
virtual OUString SAL_CALL getHead() override;
virtual OUString SAL_CALL format(const LogRecord& Record) override;
virtual OUString SAL_CALL getTail() override;
// XServiceInfo
virtual OUString SAL_CALL getImplementationName() override;
virtual sal_Bool SAL_CALL supportsService(const OUString& _rServiceName) override;
virtual Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
};
SimpleTextFormatter::SimpleTextFormatter() {}
OUString SAL_CALL SimpleTextFormatter::getHead() { return OUString(); }
OUString SAL_CALL SimpleTextFormatter::format(const LogRecord& _rRecord)
{
OUStringBuffer aLogEntry;
// Highlight warnings
if (_rRecord.Level == css::logging::LogLevel::SEVERE)
aLogEntry.append("ERROR: ");
if (_rRecord.Level == css::logging::LogLevel::WARNING)
aLogEntry.append("WARNING: ");
aLogEntry.append(_rRecord.Message);
aLogEntry.append("\n");
return aLogEntry.makeStringAndClear();
}
OUString SAL_CALL SimpleTextFormatter::getTail() { return OUString(); }
sal_Bool SAL_CALL SimpleTextFormatter::supportsService(const OUString& _rServiceName)
{
return cppu::supportsService(this, _rServiceName);
}
OUString SAL_CALL SimpleTextFormatter::getImplementationName()
{
return OUString("com.sun.star.comp.extensions.SimpleTextFormatter");
}
Sequence<OUString> SAL_CALL SimpleTextFormatter::getSupportedServiceNames()
{
return { "com.sun.star.logging.SimpleTextFormatter" };
}
} // namespace logging
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
com_sun_star_comp_extensions_SimpleTextFormatter(css::uno::XComponentContext*,
css::uno::Sequence<css::uno::Any> const&)
{
return cppu::acquire(new logging::SimpleTextFormatter());
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
......@@ -254,6 +254,7 @@ $(eval $(call gb_UnoApi_add_idlfiles_nohdl,offapi,com/sun/star/logging,\
FileHandler \
LoggerPool \
PlainTextFormatter \
SimpleTextFormatter \
))
$(eval $(call gb_UnoApi_add_idlfiles_nohdl,offapi,com/sun/star/mail,\
MailMessage \
......
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#ifndef __com_sun_star_logging_SimpleTextFormatter_idl__
#define __com_sun_star_logging_SimpleTextFormatter_idl__
module com { module sun { module star { module logging {
interface XLogFormatter;
/** specifies a service which formats log records as single line plain text
<p>Every log record, as passed to XLogFormatter::format(), will
be formatted into a single text line, with just the log message being output.
If the loglevel is WARNING, or SEVERE, the line will be prefixed accordingly.</p>
@since LibreOffice 6.2
*/
service SimpleTextFormatter : XLogFormatter
{
/// creates a SimpleTextFormatter instance
create();
};
}; }; }; };
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment