Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Commit

Permalink
New commandline option 'verbosity' to allow different log levels (#3364)
Browse files Browse the repository at this point in the history
* Fix indentation

* Added a new commandline option 'verbosity', to support more than two levels of logging. Currently, levels 0 (none), 1 (info) and 2 (extra) are supported.
Current 'quiet' option has been kept for backwards compatibility and it has been mapped to the new 'verbosity' values 0 (--quiet=true) and 1 (--quiet=false).

* Fix missing dpi parameter.

* Fix indentation.

* Log levels support.

* Common handler for LogLevel

* LogLevel integration into wkhtmltopdf

* LogLevel integration into wkhtmltoimage

* Bindings updated to support loglevel
  • Loading branch information
dmlambea authored and ashkulz committed Mar 7, 2017
1 parent e43bbb9 commit c754e38
Show file tree
Hide file tree
Showing 23 changed files with 243 additions and 36 deletions.
2 changes: 1 addition & 1 deletion include/wkhtmltox/converter.hh
Expand Up @@ -32,7 +32,7 @@ public:
virtual ~Converter() {};

int currentPhase();
int phaseCount();
int phaseCount();
QString phaseDescription(int phase=-1);
QString progressString();
int httpErrorCode();
Expand Down
8 changes: 6 additions & 2 deletions include/wkhtmltox/imagesettings.hh
@@ -1,3 +1,6 @@
// -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
// vi:set ts=4 sts=4 sw=4 noet :
//
// Copyright 2010 wkhtmltopdf authors
//
// This file is part of wkhtmltopdf.
Expand All @@ -19,6 +22,7 @@
#define __IMAGESETTINGS_HH__

#include <QString>
#include <wkhtmltox/logging.hh>
#include <wkhtmltox/loadsettings.hh>
#include <wkhtmltox/websettings.hh>

Expand Down Expand Up @@ -57,8 +61,8 @@ struct DLL_PUBLIC ImageGlobal {
LoadPage loadPage;
Web web;

//! Be less verbose
bool quiet;
//! Log level
LogLevel logLevel;

bool transparent;

Expand Down
43 changes: 43 additions & 0 deletions include/wkhtmltox/logging.hh
@@ -0,0 +1,43 @@
// -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
// vi:set ts=4 sts=4 sw=4 noet :
//
// Copyright 2010 wkhtmltopdf authors
//
// This file is part of wkhtmltopdf.
//
// wkhtmltopdf is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// wkhtmltopdf is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with wkhtmltopdf. If not, see <http://www.gnu.org/licenses/>.

#ifndef __LOGGING_HH__
#define __LOGGING_HH__

#include <QMap>
#include <QString>
#include <wkhtmltox/dllbegin.inc>
namespace wkhtmltopdf {
namespace settings {

enum LogLevel {
None,
Error,
Warn,
Info
};

DLL_PUBLIC LogLevel strToLogLevel(const char * s, bool * ok=0);
DLL_PUBLIC QString logLevelToStr(const LogLevel & l, bool * ok=0);

}
}
#include <wkhtmltox/dllend.inc>
#endif //__LOGGING_HH__
2 changes: 1 addition & 1 deletion include/wkhtmltox/multipageloader.hh
Expand Up @@ -45,7 +45,7 @@ class DLL_LOCAL MultiPageLoaderPrivate;
class DLL_LOCAL MultiPageLoader: public QObject {
Q_OBJECT
public:
MultiPageLoader(settings::LoadGlobal & s, bool mainLoader = false);
MultiPageLoader(settings::LoadGlobal & s, int dpi, bool mainLoader = false);
~MultiPageLoader();
LoaderObject * addResource(const QString & url, const settings::LoadPage & settings, const QString * data=NULL);
LoaderObject * addResource(const QUrl & url, const settings::LoadPage & settings);
Expand Down
12 changes: 8 additions & 4 deletions include/wkhtmltox/pdfsettings.hh
@@ -1,3 +1,6 @@
// -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
// vi:set ts=4 sts=4 sw=4 noet :
//
// Copyright 2010 wkhtmltopdf authors
//
// This file is part of wkhtmltopdf.
Expand All @@ -21,6 +24,7 @@
#include <QNetworkProxy>
#include <QPrinter>
#include <QString>
#include <wkhtmltox/logging.hh>
#include <wkhtmltox/loadsettings.hh>
#include <wkhtmltox/websettings.hh>

Expand Down Expand Up @@ -82,13 +86,13 @@ struct DLL_PUBLIC PdfGlobal {
//! Size related settings
Size size;

//! Be less verbose
bool quiet;
//! Log level
LogLevel logLevel;

//! Should we use the graphics system
bool useGraphics;
//! Should relative links be resolved

//! Should relative links be resolved or kept as-is
bool resolveRelativeLinks;

//! Should we orientate in landscape or portrate
Expand Down
3 changes: 2 additions & 1 deletion src/image/imagearguments.cc
Expand Up @@ -31,7 +31,8 @@ ImageCommandLineParser::ImageCommandLineParser(wkhtmltopdf::settings::ImageGloba

extended(false);
qthack(false);
addarg("quiet", 'q', "Be less verbose", new ConstSetter<bool>(s.quiet,true));
addarg("quiet", 'q', "Be less verbose, maintained for backwards compatibility; Same as using --log-level none", new ConstSetter<wkhtmltopdf::settings::LogLevel>(s.logLevel, wkhtmltopdf::settings::None));
addarg("log-level", 0, "Set log level to: none, error, warn or info", new LogLevelSetter(s.logLevel, "level"));
addarg("width",0,"Set screen width, note that this is used only as a guide line. Use --disable-smart-width to make it strict.", new IntSetter(s.screenWidth,"int"));
addarg("height",0,"Set screen height (default is calculated from page content)", new IntSetter(s.screenHeight, "int"));
// addarg("scale-w",0,"Set width for resizing", new IntSetter(s.scale.width,"int"));
Expand Down
2 changes: 1 addition & 1 deletion src/image/wkhtmltoimage.cc
Expand Up @@ -60,7 +60,7 @@ int main(int argc, char** argv) {
QObject::connect(&converter, SIGNAL(radiobuttonSvgChanged(const QString &)), style, SLOT(setRadioButtonSvg(const QString &)));
QObject::connect(&converter, SIGNAL(radiobuttonCheckedSvgChanged(const QString &)), style, SLOT(setRadioButtonCheckedSvg(const QString &)));

wkhtmltopdf::ProgressFeedback feedback(settings.quiet, converter);
wkhtmltopdf::ProgressFeedback feedback(settings.logLevel, converter);
bool success = converter.convert();
return handleError(success, converter.httpErrorCode());
}
2 changes: 1 addition & 1 deletion src/lib/converter.hh
Expand Up @@ -35,7 +35,7 @@ public:
virtual ~Converter() {};

int currentPhase();
int phaseCount();
int phaseCount();
QString phaseDescription(int phase=-1);
QString progressString();
int httpErrorCode();
Expand Down
6 changes: 3 additions & 3 deletions src/lib/image_c_bindings.cc
Expand Up @@ -49,11 +49,11 @@
using namespace wkhtmltopdf;

void MyImageConverter::warning(const QString & message) {
if (warning_cb) (warning_cb)(reinterpret_cast<wkhtmltoimage_converter*>(this), message.toUtf8().constData());
if (warning_cb && globalSettings->logLevel > settings::Error) (warning_cb)(reinterpret_cast<wkhtmltoimage_converter*>(this), message.toUtf8().constData());
}

void MyImageConverter::error(const QString & message) {
if (error_cb) (error_cb)(reinterpret_cast<wkhtmltoimage_converter*>(this), message.toUtf8().constData());
if (error_cb && globalSettings->logLevel > settings::None) (error_cb)(reinterpret_cast<wkhtmltoimage_converter*>(this), message.toUtf8().constData());
}

void MyImageConverter::phaseChanged() {
Expand Down Expand Up @@ -104,7 +104,7 @@ CAPI(wkhtmltoimage_global_settings *) wkhtmltoimage_create_global_settings() {
}

CAPI(void) wkhtmltoimage_destroy_global_settings(wkhtmltoimage_global_settings * obj) {
delete reinterpret_cast<settings::ImageGlobal *>(obj);
delete reinterpret_cast<settings::ImageGlobal *>(obj);
}

CAPI(int) wkhtmltoimage_set_global_setting(wkhtmltoimage_global_settings * settings, const char * name, const char * value) {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/imagesettings.cc
Expand Up @@ -41,7 +41,8 @@ struct DLL_LOCAL ReflectImpl<ImageGlobal>: public ReflectClass {
ReflectImpl(ImageGlobal & c) {
WKHTMLTOPDF_REFLECT(screenWidth);
WKHTMLTOPDF_REFLECT(screenHeight);
WKHTMLTOPDF_REFLECT(quiet);
ReflectClass::add("quiet", new QuietArgBackwardsCompatReflect(c.logLevel)); // Fake the "quiet" argument
WKHTMLTOPDF_REFLECT(logLevel);
WKHTMLTOPDF_REFLECT(transparent);
WKHTMLTOPDF_REFLECT(useGraphics);
WKHTMLTOPDF_REFLECT(in);
Expand All @@ -60,7 +61,7 @@ CropSettings::CropSettings():
height(-1) {}

ImageGlobal::ImageGlobal():
quiet(false),
logLevel(Info),
transparent(false),
useGraphics(false),
in(""),
Expand Down
5 changes: 3 additions & 2 deletions src/lib/imagesettings.hh
Expand Up @@ -22,6 +22,7 @@
#define __IMAGESETTINGS_HH__

#include <QString>
#include <wkhtmltox/logging.hh>
#include <wkhtmltox/loadsettings.hh>
#include <wkhtmltox/websettings.hh>

Expand Down Expand Up @@ -60,8 +61,8 @@ struct DLL_PUBLIC ImageGlobal {
LoadPage loadPage;
Web web;

//! Be less verbose
bool quiet;
//! Log level
LogLevel logLevel;

bool transparent;

Expand Down
4 changes: 2 additions & 2 deletions src/lib/lib.pri
Expand Up @@ -22,8 +22,8 @@ PUBLIC_HEADERS += ../lib/converter.hh ../lib/multipageloader.hh ../lib/dllbegin.
PUBLIC_HEADERS += ../lib/dllend.inc ../lib/loadsettings.hh ../lib/websettings.hh
PUBLIC_HEADERS += ../lib/utilities.hh
HEADERS += ../lib/multipageloader_p.hh ../lib/converter_p.hh
SOURCES += ../lib/loadsettings.cc ../lib/multipageloader.cc ../lib/tempfile.cc \
../lib/converter.cc ../lib/websettings.cc \
SOURCES += ../lib/loadsettings.cc ../lib/logging.cc ../lib/multipageloader.cc \
../lib/tempfile.cc ../lib/converter.cc ../lib/websettings.cc \
../lib/reflect.cc ../lib/utilities.cc

#Pdf
Expand Down
62 changes: 62 additions & 0 deletions src/lib/logging.cc
@@ -0,0 +1,62 @@
// -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
// vi:set ts=4 sts=4 sw=4 noet :
//
// Copyright 2010 wkhtmltopdf authors
//
// This file is part of wkhtmltopdf.
//
// wkhtmltopdf is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// wkhtmltopdf is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with wkhtmltopdf. If not, see <http://www.gnu.org/licenses/>.

#include "logging.hh"
#include <QMap>
#include <QString>
#include <wkhtmltox/dllbegin.inc>
namespace wkhtmltopdf {
namespace settings {

// Centralized name-to-enum value mapping
QMap<QString, LogLevel> logLevelMap() {
QMap<QString, LogLevel> res;
res["none"] = None;
res["error"] = Error;
res["warn"] = Warn;
res["info"] = Info;
return res;
}

QMap<QString, LogLevel> logLevels = logLevelMap();

LogLevel strToLogLevel(const char * s, bool * ok) {
for (QMap<QString, LogLevel>::const_iterator i = logLevels.begin(); i != logLevels.end(); ++i) {
if (i.key().compare(s, Qt::CaseInsensitive) != 0) continue;
if (ok) *ok = true;
return i.value();
}
if (ok) *ok = false;
return Info;
}

QString logLevelToStr(const LogLevel & l, bool * ok) {
for (QMap<QString, LogLevel>::const_iterator i = logLevels.begin(); i != logLevels.end(); ++i) {
if (i.value() != l) continue;
if (ok) *ok = true;
return i.key();
}
if (ok) *ok = false;
return QString();
}

}
}
#include <wkhtmltox/dllend.inc>
43 changes: 43 additions & 0 deletions src/lib/logging.hh
@@ -0,0 +1,43 @@
// -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
// vi:set ts=4 sts=4 sw=4 noet :
//
// Copyright 2010 wkhtmltopdf authors
//
// This file is part of wkhtmltopdf.
//
// wkhtmltopdf is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// wkhtmltopdf is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with wkhtmltopdf. If not, see <http://www.gnu.org/licenses/>.

#ifndef __LOGGING_HH__
#define __LOGGING_HH__

#include <QMap>
#include <QString>
#include <wkhtmltox/dllbegin.inc>
namespace wkhtmltopdf {
namespace settings {

enum LogLevel {
None,
Error,
Warn,
Info
};

DLL_PUBLIC LogLevel strToLogLevel(const char * s, bool * ok=0);
DLL_PUBLIC QString logLevelToStr(const LogLevel & l, bool * ok=0);

}
}
#include <wkhtmltox/dllend.inc>
#endif //__LOGGING_HH__
4 changes: 2 additions & 2 deletions src/lib/pdf_c_bindings.cc
Expand Up @@ -209,11 +209,11 @@ QApplication * a = 0;
int usage = 0;

void MyPdfConverter::warning(const QString & message) {
if (warning_cb) (warning_cb)(reinterpret_cast<wkhtmltopdf_converter*>(this), message.toUtf8().constData());
if (warning_cb && globalSettings->logLevel > settings::Error) (warning_cb)(reinterpret_cast<wkhtmltopdf_converter*>(this), message.toUtf8().constData());
}

void MyPdfConverter::error(const QString & message) {
if (error_cb) (error_cb)(reinterpret_cast<wkhtmltopdf_converter*>(this), message.toUtf8().constData());
if (error_cb && globalSettings->logLevel > settings::None) (error_cb)(reinterpret_cast<wkhtmltopdf_converter*>(this), message.toUtf8().constData());
}

void MyPdfConverter::phaseChanged() {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/pdfsettings.cc
Expand Up @@ -107,7 +107,8 @@ template<>
struct DLL_LOCAL ReflectImpl<PdfGlobal>: public ReflectClass {
ReflectImpl(PdfGlobal & c) {
WKHTMLTOPDF_REFLECT(size);
WKHTMLTOPDF_REFLECT(quiet);
ReflectClass::add("quiet", new QuietArgBackwardsCompatReflect(c.logLevel)); // Fake the "quiet" argument
WKHTMLTOPDF_REFLECT(logLevel);
WKHTMLTOPDF_REFLECT(useGraphics);
WKHTMLTOPDF_REFLECT(resolveRelativeLinks);
WKHTMLTOPDF_REFLECT(orientation);
Expand Down Expand Up @@ -368,7 +369,7 @@ Margin::Margin():
left(UnitReal(10,QPrinter::Millimeter)) {}

PdfGlobal::PdfGlobal():
quiet(false),
logLevel(Info),
useGraphics(false),
resolveRelativeLinks(true),
orientation(QPrinter::Portrait),
Expand Down
5 changes: 3 additions & 2 deletions src/lib/pdfsettings.hh
Expand Up @@ -24,6 +24,7 @@
#include <QNetworkProxy>
#include <QPrinter>
#include <QString>
#include <wkhtmltox/logging.hh>
#include <wkhtmltox/loadsettings.hh>
#include <wkhtmltox/websettings.hh>

Expand Down Expand Up @@ -85,8 +86,8 @@ struct DLL_PUBLIC PdfGlobal {
//! Size related settings
Size size;

//! Be less verbose
bool quiet;
//! Log level
LogLevel logLevel;

//! Should we use the graphics system
bool useGraphics;
Expand Down

0 comments on commit c754e38

Please sign in to comment.