Skip to content

Commit ffe849a

Browse files
committed
fix vtable warnings for LogTarget
Actually the pointer is unused in XyzAction classes.
1 parent 9af6895 commit ffe849a

File tree

4 files changed

+95
-110
lines changed

4 files changed

+95
-110
lines changed

daemon/core.cpp

Lines changed: 13 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -343,46 +343,9 @@ const char *x11opcodeToString(unsigned char opcode)
343343
return "";
344344
}
345345

346-
static const char *strLevel(int level)
347-
{
348-
switch (level)
349-
{
350-
case LOG_EMERG:
351-
return "Emergency";
352-
353-
case LOG_ALERT:
354-
return "Alert";
355-
356-
case LOG_CRIT:
357-
return "Critical";
358-
359-
case LOG_ERR:
360-
return "Error";
361-
362-
case LOG_WARNING:
363-
return "Warning";
364-
365-
case LOG_NOTICE:
366-
return "Notice";
367-
368-
case LOG_INFO:
369-
return "Info";
370-
371-
case LOG_DEBUG:
372-
return "Debug";
373-
374-
default:
375-
return "";
376-
}
377-
}
378-
379-
380346
Core::Core(bool useSyslog, bool minLogLevelSet, int minLogLevel, const QStringList &configFiles, bool multipleActionsBehaviourSet, MultipleActionsBehaviour multipleActionsBehaviour, QObject *parent)
381347
: QThread(parent)
382-
, LogTarget()
383348
, mReady(false)
384-
, mUseSyslog(useSyslog)
385-
, mMinLogLevel(minLogLevel)
386349
, mDisplay(nullptr)
387350
, mInterClientCommunicationWindow(0)
388351
, mServiceWatcher{new QDBusServiceWatcher{this}}
@@ -403,8 +366,11 @@ Core::Core(bool useSyslog, bool minLogLevelSet, int minLogLevel, const QStringLi
403366
{
404367
#if 0
405368
// debugging
406-
mUseSyslog = false;
407-
mMinLogLevel = 7;
369+
Q_UNUSED(minLogLevel);
370+
Q_UNUSED(useSyslog);
371+
mCoreLogger = std::make_unique<LogTarget>(LOG_DEBUG, false);
372+
#else
373+
s_CoreLogTarget = std::make_unique<LogTarget>(minLogLevel, useSyslog);
408374
#endif
409375

410376
s_Core = this;
@@ -489,31 +455,8 @@ Core::Core(bool useSyslog, bool minLogLevelSet, int minLogLevel, const QStringLi
489455
iniValue = settings.value(/* General/ */QStringLiteral("LogLevel")).toString();
490456
if (!iniValue.isEmpty())
491457
{
492-
minLogLevelSet = true;
493-
if (iniValue == QLatin1String("error"))
494-
{
495-
mMinLogLevel = LOG_ERR;
496-
}
497-
else if (iniValue == QLatin1String("warning"))
498-
{
499-
mMinLogLevel = LOG_WARNING;
500-
}
501-
else if (iniValue == QLatin1String("notice"))
502-
{
503-
mMinLogLevel = LOG_NOTICE;
504-
}
505-
else if (iniValue == QLatin1String("info"))
506-
{
507-
mMinLogLevel = LOG_INFO;
508-
}
509-
else if (iniValue == QLatin1String("debug"))
510-
{
511-
mMinLogLevel = LOG_DEBUG;
512-
}
513-
else
514-
{
515-
minLogLevelSet = false;
516-
}
458+
auto lvl = LogTarget::levelFromStr(qPrintable(iniValue));
459+
minLogLevelSet = strlen(LogTarget::strLevel(lvl)) > 0;
517460
}
518461
}
519462

@@ -681,7 +624,7 @@ Core::Core(bool useSyslog, bool minLogLevelSet, int minLogLevel, const QStringLi
681624
log(LOG_DEBUG, "Config file: %s", qPrintable(configs[0]));
682625
}
683626

684-
log(LOG_DEBUG, "MinLogLevel: %s", strLevel(mMinLogLevel));
627+
log(LOG_DEBUG, "MinLogLevel: %s", LogTarget::strLevel(mCoreLogger->mMinLogLevel));
685628
switch (mMultipleActionsBehaviour)
686629
{
687630
case MULTIPLE_ACTIONS_BEHAVIOUR_FIRST:
@@ -882,28 +825,6 @@ void Core::unixSignalHandler(int signalNumber)
882825
qApp->quit();
883826
}
884827

885-
void Core::log(int level, const char *format, ...) const
886-
{
887-
if (level > mMinLogLevel)
888-
{
889-
return;
890-
}
891-
892-
va_list ap;
893-
va_start(ap, format);
894-
if (mUseSyslog)
895-
{
896-
vsyslog(LOG_MAKEPRI(LOG_USER, level), format, ap);
897-
}
898-
else
899-
{
900-
fprintf(stderr, "[%s] ", strLevel(level));
901-
vfprintf(stderr, format, ap);
902-
fprintf(stderr, "\n");
903-
}
904-
va_end(ap);
905-
}
906-
907828
int Core::x11ErrorHandler(Display */*display*/, XErrorEvent *errorEvent)
908829
{
909830
if (error_t error = writeAll(mX11ErrorPipe[STDOUT_FILENO], errorEvent, sizeof(XErrorEvent)))
@@ -2067,7 +1988,7 @@ QPair<QString, qulonglong> Core::addOrRegisterClientAction(const QString &shortc
20671988
}
20681989

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

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

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

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

@@ -2193,7 +2114,7 @@ void Core::addCommandAction(QPair<QString, qulonglong> &result, const QString &s
21932114
qulonglong id = ++mLastId;
21942115

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

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

@@ -2304,7 +2225,7 @@ void Core::modifyMethodAction(bool &result, const qulonglong &id, const QString
23042225

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

@@ -2338,7 +2259,7 @@ void Core::modifyCommandAction(bool &result, const qulonglong &id, const QString
23382259

23392260
bool isEnabled = action->isEnabled();
23402261
delete action;
2341-
CommandAction *newAction = new CommandAction(this, command, arguments, description);
2262+
auto newAction = new CommandAction(mCoreLogger.get(), command, arguments, description);
23422263
newAction->setEnabled(isEnabled);
23432264
shortcutAndActionById.value().second = newAction;
23442265

daemon/core.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,21 @@ class QOrderedSet : public QMap<Key, Key>
6868
}
6969
};
7070

71-
class Core : public QThread, public LogTarget
71+
class Core : public QThread
7272
{
7373
Q_OBJECT
74+
7475
public:
7576
Core(bool useSyslog, bool minLogLevelSet, int minLogLevel, const QStringList &configFiles, bool multipleActionsBehaviourSet, MultipleActionsBehaviour multipleActionsBehaviour, QObject *parent = nullptr);
7677
~Core() override;
7778

7879
bool ready() const { return mReady; }
7980

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

8287
signals:
8388
void onShortcutGrabbed();
@@ -399,10 +404,9 @@ class Core : public QThread, public LogTarget
399404
bool waitForX11Error(int level, uint timeout);
400405

401406
private:
402-
bool mReady;
403-
bool mUseSyslog;
404-
int mMinLogLevel;
407+
std::unique_ptr<LogTarget> mCoreLogger;
405408

409+
bool mReady;
406410
int mX11ErrorPipe[2];
407411
int mX11RequestPipe[2];
408412
int mX11ResponsePipe[2];

daemon/log_target.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,32 @@
2626
* END_COMMON_COPYRIGHT_HEADER */
2727

2828
#include "log_target.h"
29+
#include <cstdio>
2930

30-
31-
LogTarget::LogTarget()
31+
LogTarget::LogTarget(int minLogLevel, bool useSyslog)
3232
{
33+
mMinLogLevel = minLogLevel;
34+
mUseSyslog = useSyslog;
3335
}
3436

35-
LogTarget::~LogTarget()
37+
void LogTarget::log(int level, const char *format, ...)
3638
{
39+
if (level > mMinLogLevel)
40+
{
41+
return;
42+
}
43+
44+
va_list ap;
45+
va_start(ap, format);
46+
if (mUseSyslog)
47+
{
48+
vsyslog(LOG_MAKEPRI(LOG_USER, level), format, ap);
49+
}
50+
else
51+
{
52+
fprintf(stderr, "[%s] ", strLevel(level));
53+
vfprintf(stderr, format, ap);
54+
fprintf(stderr, "\n");
55+
}
56+
va_end(ap);
3757
}

daemon/log_target.h

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,60 @@
2525
*
2626
* END_COMMON_COPYRIGHT_HEADER */
2727

28-
#ifndef GLOBAL_ACTION_DAEMON__LOG_TARGET__INCLUDED
29-
#define GLOBAL_ACTION_DAEMON__LOG_TARGET__INCLUDED
30-
28+
#pragma once
3129

3230
#include <syslog.h>
31+
#include <cstring>
3332

33+
class LogTarget {
34+
public:
35+
int mMinLogLevel = LOG_INFO;
36+
bool mUseSyslog = false;
3437

35-
class LogTarget
36-
{
3738
public:
38-
LogTarget();
39-
virtual ~LogTarget();
39+
LogTarget(int minLogLevel = LOG_INFO, bool useSyslog = false);
4040

41-
virtual void log(int level, const char *format, ...) const = 0;
42-
};
41+
void log(int level, const char *format, ...);
42+
43+
static constexpr auto strLevel(int level)
44+
{
45+
switch (level) {
46+
case LOG_EMERG: return "Emergency";
47+
case LOG_ALERT: return "Alert";
48+
case LOG_CRIT: return "Critical";
49+
case LOG_ERR: return "Error";
50+
case LOG_WARNING: return "Warning";
51+
case LOG_NOTICE: return "Notice";
52+
case LOG_INFO: return "Info";
53+
case LOG_DEBUG: return "Debug";
54+
}
4355

44-
#endif // GLOBAL_ACTION_DAEMON__LOG_TARGET__INCLUDED
56+
return ""; // fallthrough
57+
}
58+
59+
static constexpr auto levelFromStr(const char* str) {
60+
if (str == nullptr) {
61+
// error: null string
62+
return -2;
63+
}
64+
65+
if (std::strcmp(str, "error") == 0) {
66+
return LOG_ERR;
67+
}
68+
if (std::strcmp(str, "warning") == 0) {
69+
return LOG_WARNING;
70+
}
71+
if (std::strcmp(str, "notice") == 0) {
72+
return LOG_NOTICE;
73+
}
74+
if (std::strcmp(str, "info") == 0) {
75+
return LOG_INFO;
76+
}
77+
if (std::strcmp(str, "debug") == 0) {
78+
return LOG_DEBUG;
79+
}
80+
81+
// error: unknown log level string
82+
return -1;
83+
}
84+
};

0 commit comments

Comments
 (0)