mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
refactor(core): move logger to its own file
This commit is contained in:
parent
acbc1f8beb
commit
c7ca261679
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
59
src/core/toxlogger.cpp
Normal file
59
src/core/toxlogger.cpp
Normal file
|
@ -0,0 +1,59 @@
|
|||
#include "toxlogger.h"
|
||||
|
||||
#include <tox/tox.h>
|
||||
|
||||
#include <QDebug>
|
||||
#include <QRegularExpression>
|
||||
#include <QString>
|
||||
#include <QStringBuilder>
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
|
13
src/core/toxlogger.h
Normal file
13
src/core/toxlogger.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#ifndef TOXLOGGER_H
|
||||
#define TOXLOGGER_H
|
||||
|
||||
#include <tox/tox.h>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
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
|
Loading…
Reference in New Issue
Block a user