diff --git a/CMakeLists.txt b/CMakeLists.txt index 92b128ff6..0d3d195e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -255,6 +255,8 @@ set(${PROJECT_NAME}_SOURCES src/core/toxfile.h src/core/toxid.cpp src/core/toxid.h + src/core/toxlogger.cpp + src/core/toxlogger.h src/core/toxpk.cpp src/core/toxpk.h src/core/toxstring.cpp diff --git a/src/core/core.cpp b/src/core/core.cpp index 86d54d6cf..705e72cc6 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -23,6 +23,7 @@ #include "src/core/coreav.h" #include "src/core/icoresettings.h" #include "src/core/toxstring.h" +#include "src/core/toxlogger.h" #include "src/model/groupinvite.h" #include "src/nexus.h" #include "src/persistence/profile.h" @@ -152,56 +153,6 @@ private: QByteArray proxyAddrData; }; -/** - * @brief Map TOX_LOG_LEVEL to a string - * @param level log level - * @return Descriptive string for the log level - */ -static QString getToxLogLevel(TOX_LOG_LEVEL level) { - switch (level) { - case TOX_LOG_LEVEL_TRACE: - return QLatin1Literal("TRACE"); - case TOX_LOG_LEVEL_DEBUG: - return QLatin1Literal("DEBUG"); - case TOX_LOG_LEVEL_INFO: - return QLatin1Literal("INFO "); - case TOX_LOG_LEVEL_WARNING: - return QLatin1Literal("WARN "); - case TOX_LOG_LEVEL_ERROR: - return QLatin1Literal("ERROR"); - default: - // Invalid log level - return QLatin1Literal("INVAL"); - } -} - -/** - * @brief Log message handler for toxcore log messages - * @note See tox.h for the parameter definitions - */ -void onLogMessage(Tox *tox, TOX_LOG_LEVEL level, const char *file, uint32_t line, - const char *func, const char *message, void *user_data) -{ - // for privacy, make the path relative to the c-toxcore source directory - const QRegularExpression pathCleaner(QLatin1Literal{"[\\s|\\S]*c-toxcore."}); - const QString cleanPath = QString{file}.remove(pathCleaner); - - const QString logMsg = getToxLogLevel(level) % QLatin1Literal{":"} % cleanPath - % QLatin1Literal{":"} % func % QStringLiteral(":%1: ").arg(line) - % message; - - switch (level) { - case TOX_LOG_LEVEL_TRACE: - case TOX_LOG_LEVEL_DEBUG: - case TOX_LOG_LEVEL_INFO: - qDebug() << logMsg; - break; - case TOX_LOG_LEVEL_WARNING: - case TOX_LOG_LEVEL_ERROR: - qWarning() << logMsg; - } -} - /** * @brief Initializes Tox_Options instance * @param savedata Previously saved Tox data @@ -230,7 +181,7 @@ ToxOptionsWrapper initToxOptions(const QByteArray& savedata, const ICoreSettings ToxOptionsWrapper toxOptions = ToxOptionsWrapper(tox_options_new(nullptr), proxyAddr.toUtf8()); // register log first, to get messages as early as possible - tox_options_set_log_callback(toxOptions, onLogMessage); + tox_options_set_log_callback(toxOptions, ToxLogger::onLogMessage); tox_options_set_ipv6_enabled(toxOptions, enableIPv6); tox_options_set_udp_enabled(toxOptions, !forceTCP); diff --git a/src/core/toxlogger.cpp b/src/core/toxlogger.cpp new file mode 100644 index 000000000..dae7e340f --- /dev/null +++ b/src/core/toxlogger.cpp @@ -0,0 +1,59 @@ +#include "toxlogger.h" + +#include + +#include +#include +#include +#include + +/** + * @brief Map TOX_LOG_LEVEL to a string + * @param level log level + * @return Descriptive string for the log level + */ +QString getToxLogLevel(TOX_LOG_LEVEL level) { + switch (level) { + case TOX_LOG_LEVEL_TRACE: + return QLatin1Literal("TRACE"); + case TOX_LOG_LEVEL_DEBUG: + return QLatin1Literal("DEBUG"); + case TOX_LOG_LEVEL_INFO: + return QLatin1Literal("INFO "); + case TOX_LOG_LEVEL_WARNING: + return QLatin1Literal("WARN "); + case TOX_LOG_LEVEL_ERROR: + return QLatin1Literal("ERROR"); + default: + // Invalid log level + return QLatin1Literal("INVAL"); + } +} + +/** + * @brief Log message handler for toxcore log messages + * @note See tox.h for the parameter definitions + */ +void ToxLogger::onLogMessage(Tox *tox, TOX_LOG_LEVEL level, const char *file, uint32_t line, + const char *func, const char *message, void *user_data) +{ + // for privacy, make the path relative to the c-toxcore source directory + const QRegularExpression pathCleaner(QLatin1Literal{"[\\s|\\S]*c-toxcore."}); + const QString cleanPath = QString{file}.remove(pathCleaner); + + const QString logMsg = getToxLogLevel(level) % QLatin1Literal{":"} % cleanPath + % QLatin1Literal{":"} % func % QStringLiteral(":%1: ").arg(line) + % message; + + switch (level) { + case TOX_LOG_LEVEL_TRACE: + case TOX_LOG_LEVEL_DEBUG: + case TOX_LOG_LEVEL_INFO: + qDebug() << logMsg; + break; + case TOX_LOG_LEVEL_WARNING: + case TOX_LOG_LEVEL_ERROR: + qWarning() << logMsg; + } +} + diff --git a/src/core/toxlogger.h b/src/core/toxlogger.h new file mode 100644 index 000000000..624bf8c91 --- /dev/null +++ b/src/core/toxlogger.h @@ -0,0 +1,13 @@ +#ifndef TOXLOGGER_H +#define TOXLOGGER_H + +#include + +#include + +namespace ToxLogger { + void onLogMessage(Tox *tox, TOX_LOG_LEVEL level, const char *file, uint32_t line, + const char *func, const char *message, void *user_data); +} + +#endif // TOXLOGGER_H