Skip to content

Commit

Permalink
fix vtable warnings for LogTarget
Browse files Browse the repository at this point in the history
Actually the pointer is unused in XyzAction classes.
  • Loading branch information
antis81 committed Sep 17, 2021
1 parent 9af6895 commit 9520f11
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 110 deletions.
111 changes: 19 additions & 92 deletions daemon/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,46 +343,9 @@ const char *x11opcodeToString(unsigned char opcode)
return "";
}

static const char *strLevel(int level)
{
switch (level)
{
case LOG_EMERG:
return "Emergency";

case LOG_ALERT:
return "Alert";

case LOG_CRIT:
return "Critical";

case LOG_ERR:
return "Error";

case LOG_WARNING:
return "Warning";

case LOG_NOTICE:
return "Notice";

case LOG_INFO:
return "Info";

case LOG_DEBUG:
return "Debug";

default:
return "";
}
}


Core::Core(bool useSyslog, bool minLogLevelSet, int minLogLevel, const QStringList &configFiles, bool multipleActionsBehaviourSet, MultipleActionsBehaviour multipleActionsBehaviour, QObject *parent)
: QThread(parent)
, LogTarget()
, mReady(false)
, mUseSyslog(useSyslog)
, mMinLogLevel(minLogLevel)
, mDisplay(nullptr)
, mInterClientCommunicationWindow(0)
, mServiceWatcher{new QDBusServiceWatcher{this}}
Expand All @@ -403,8 +366,11 @@ Core::Core(bool useSyslog, bool minLogLevelSet, int minLogLevel, const QStringLi
{
#if 0
// debugging
mUseSyslog = false;
mMinLogLevel = 7;
Q_UNUSED(minLogLevel);
Q_UNUSED(useSyslog);
mCoreLogger = std::make_unique<LogTarget>(LOG_DEBUG, false);
#else
mCoreLogger = std::make_unique<LogTarget>(minLogLevel, useSyslog);
#endif

s_Core = this;
Expand Down Expand Up @@ -487,33 +453,16 @@ Core::Core(bool useSyslog, bool minLogLevelSet, int minLogLevel, const QStringLi
if (!minLogLevelSet)
{
iniValue = settings.value(/* General/ */QStringLiteral("LogLevel")).toString();
if (!iniValue.isEmpty())
auto lvl = LogTarget::levelFromStr(qPrintable(iniValue));
if (lvl >= 0)
{
mCoreLogger->mMinLogLevel = lvl;
minLogLevelSet = true;
if (iniValue == QLatin1String("error"))
{
mMinLogLevel = LOG_ERR;
}
else if (iniValue == QLatin1String("warning"))
{
mMinLogLevel = LOG_WARNING;
}
else if (iniValue == QLatin1String("notice"))
{
mMinLogLevel = LOG_NOTICE;
}
else if (iniValue == QLatin1String("info"))
{
mMinLogLevel = LOG_INFO;
}
else if (iniValue == QLatin1String("debug"))
{
mMinLogLevel = LOG_DEBUG;
}
else
{
minLogLevelSet = false;
}
}
else
{
auto lvlStr = LogTarget::strLevel(mCoreLogger->mMinLogLevel);
qInfo("no loglevel configured in %s (result = %d); current loglevel: %s", qUtf8Printable(settings.fileName()), lvl, lvlStr);
}
}

Expand Down Expand Up @@ -681,7 +630,7 @@ Core::Core(bool useSyslog, bool minLogLevelSet, int minLogLevel, const QStringLi
log(LOG_DEBUG, "Config file: %s", qPrintable(configs[0]));
}

log(LOG_DEBUG, "MinLogLevel: %s", strLevel(mMinLogLevel));
log(LOG_DEBUG, "MinLogLevel: %s", LogTarget::strLevel(mCoreLogger->mMinLogLevel));
switch (mMultipleActionsBehaviour)
{
case MULTIPLE_ACTIONS_BEHAVIOUR_FIRST:
Expand Down Expand Up @@ -882,28 +831,6 @@ void Core::unixSignalHandler(int signalNumber)
qApp->quit();
}

void Core::log(int level, const char *format, ...) const
{
if (level > mMinLogLevel)
{
return;
}

va_list ap;
va_start(ap, format);
if (mUseSyslog)
{
vsyslog(LOG_MAKEPRI(LOG_USER, level), format, ap);
}
else
{
fprintf(stderr, "[%s] ", strLevel(level));
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
}
va_end(ap);
}

int Core::x11ErrorHandler(Display */*display*/, XErrorEvent *errorEvent)
{
if (error_t error = writeAll(mX11ErrorPipe[STDOUT_FILENO], errorEvent, sizeof(XErrorEvent)))
Expand Down Expand Up @@ -2067,7 +1994,7 @@ QPair<QString, qulonglong> Core::addOrRegisterClientAction(const QString &shortc
}

mIdByClientPath[path] = id;
auto clientAction = sender.isEmpty() ? new ClientAction(this, path, description) : new ClientAction(this, QDBusConnection::sessionBus(), sender, path, description);
auto clientAction = sender.isEmpty() ? new ClientAction(mCoreLogger.get(), path, description) : new ClientAction(mCoreLogger.get(), QDBusConnection::sessionBus(), sender, path, description);
mShortcutAndActionById[id] = qMakePair<QString, BaseAction *>(newShortcut, clientAction);

log(LOG_INFO, "addClientAction shortcut:'%s' id:%llu", qPrintable(newShortcut), id);
Expand Down Expand Up @@ -2152,7 +2079,7 @@ void Core::addMethodAction(QPair<QString, qulonglong> &result, const QString &sh
qulonglong id = ++mLastId;

mIdsByShortcut[newShortcut].insert(id);
mShortcutAndActionById[id] = qMakePair<QString, BaseAction *>(newShortcut, new MethodAction(this, QDBusConnection::sessionBus(), service, path, interface, method, description));
mShortcutAndActionById[id] = qMakePair<QString, BaseAction *>(newShortcut, new MethodAction(mCoreLogger.get(), QDBusConnection::sessionBus(), service, path, interface, method, description));

log(LOG_INFO, "addMethodAction shortcut:'%s' id:%llu", qPrintable(newShortcut), id);

Expand Down Expand Up @@ -2193,7 +2120,7 @@ void Core::addCommandAction(QPair<QString, qulonglong> &result, const QString &s
qulonglong id = ++mLastId;

mIdsByShortcut[newShortcut].insert(id);
mShortcutAndActionById[id] = qMakePair<QString, BaseAction *>(newShortcut, new CommandAction(this, command, arguments, description));
mShortcutAndActionById[id] = qMakePair<QString, BaseAction *>(newShortcut, new CommandAction(mCoreLogger.get(), command, arguments, description));

log(LOG_INFO, "addCommandAction shortcut:'%s' id:%llu", qPrintable(newShortcut), id);

Expand Down Expand Up @@ -2304,7 +2231,7 @@ void Core::modifyMethodAction(bool &result, const qulonglong &id, const QString

bool isEnabled = action->isEnabled();
delete action;
MethodAction *newAction = new MethodAction(this, QDBusConnection::sessionBus(), service, path, interface, method, description);
auto newAction = new MethodAction(mCoreLogger.get(), QDBusConnection::sessionBus(), service, path, interface, method, description);
newAction->setEnabled(isEnabled);
shortcutAndActionById.value().second = newAction;

Expand Down Expand Up @@ -2338,7 +2265,7 @@ void Core::modifyCommandAction(bool &result, const qulonglong &id, const QString

bool isEnabled = action->isEnabled();
delete action;
CommandAction *newAction = new CommandAction(this, command, arguments, description);
auto newAction = new CommandAction(mCoreLogger.get(), command, arguments, description);
newAction->setEnabled(isEnabled);
shortcutAndActionById.value().second = newAction;

Expand Down
14 changes: 9 additions & 5 deletions daemon/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,21 @@ class QOrderedSet : public QMap<Key, Key>
}
};

class Core : public QThread, public LogTarget
class Core : public QThread
{
Q_OBJECT

public:
Core(bool useSyslog, bool minLogLevelSet, int minLogLevel, const QStringList &configFiles, bool multipleActionsBehaviourSet, MultipleActionsBehaviour multipleActionsBehaviour, QObject *parent = nullptr);
~Core() override;

bool ready() const { return mReady; }

void log(int level, const char *format, ...) const override;
template<class... Args>
void log(int level, const char *format, Args&&... args) const {
// NOTE: If the logger is unassigned the SEGFAULT is intentional!
mCoreLogger->log(level, format, std::forward<Args>(args)...);
}

signals:
void onShortcutGrabbed();
Expand Down Expand Up @@ -399,10 +404,9 @@ class Core : public QThread, public LogTarget
bool waitForX11Error(int level, uint timeout);

private:
bool mReady;
bool mUseSyslog;
int mMinLogLevel;
std::unique_ptr<LogTarget> mCoreLogger;

bool mReady;
int mX11ErrorPipe[2];
int mX11RequestPipe[2];
int mX11ResponsePipe[2];
Expand Down
26 changes: 23 additions & 3 deletions daemon/log_target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,32 @@
* END_COMMON_COPYRIGHT_HEADER */

#include "log_target.h"
#include <cstdio>


LogTarget::LogTarget()
LogTarget::LogTarget(int minLogLevel, bool useSyslog)
{
mMinLogLevel = minLogLevel;
mUseSyslog = useSyslog;
}

LogTarget::~LogTarget()
void LogTarget::log(int level, const char *format, ...)
{
if (level > mMinLogLevel)
{
return;
}

va_list ap;
va_start(ap, format);
if (mUseSyslog)
{
vsyslog(LOG_MAKEPRI(LOG_USER, level), format, ap);
}
else
{
fprintf(stderr, "[%s] ", strLevel(level));
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
}
va_end(ap);
}
60 changes: 50 additions & 10 deletions daemon/log_target.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,60 @@
*
* END_COMMON_COPYRIGHT_HEADER */

#ifndef GLOBAL_ACTION_DAEMON__LOG_TARGET__INCLUDED
#define GLOBAL_ACTION_DAEMON__LOG_TARGET__INCLUDED

#pragma once

#include <syslog.h>
#include <cstring>

class LogTarget {
public:
int mMinLogLevel = LOG_INFO;
bool mUseSyslog = false;

class LogTarget
{
public:
LogTarget();
virtual ~LogTarget();
LogTarget(int minLogLevel = LOG_INFO, bool useSyslog = false);

virtual void log(int level, const char *format, ...) const = 0;
};
void log(int level, const char *format, ...);

static constexpr auto strLevel(int level)
{
switch (level) {
case LOG_EMERG: return "Emergency";
case LOG_ALERT: return "Alert";
case LOG_CRIT: return "Critical";
case LOG_ERR: return "Error";
case LOG_WARNING: return "Warning";
case LOG_NOTICE: return "Notice";
case LOG_INFO: return "Info";
case LOG_DEBUG: return "Debug";
}

#endif // GLOBAL_ACTION_DAEMON__LOG_TARGET__INCLUDED
return ""; // fallthrough
}

static constexpr auto levelFromStr(const char* str) {
if (str == nullptr) {
// error: null string
return -2;
}

if (std::strcmp(str, "error") == 0) {
return LOG_ERR;
}
if (std::strcmp(str, "warning") == 0) {
return LOG_WARNING;
}
if (std::strcmp(str, "notice") == 0) {
return LOG_NOTICE;
}
if (std::strcmp(str, "info") == 0) {
return LOG_INFO;
}
if (std::strcmp(str, "debug") == 0) {
return LOG_DEBUG;
}

// error: unknown log level string
return -1;
}
};

0 comments on commit 9520f11

Please sign in to comment.