Qt_Logger_Example/lib_logger/logger.h

108 lines
3.8 KiB
C++

/****************************************************************************
** Copyright (c) 2025 Evgeny Teterin (nayk) <nayk@nxt.ru>
** All right reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
#pragma once
#ifndef LOGGER_H
#define LOGGER_H
#if defined (LIB_LOGGER)
# define LOGGER_EXPORT Q_DECL_EXPORT
#else
# define LOGGER_EXPORT Q_DECL_IMPORT
#endif
#include <QtCore/QObject>
#include <QtCore/QString>
#include <QtCore/QLoggingCategory>
#include <QtCore/QThread>
#include <atomic>
class LogWorker;
//==============================================================================
class LOGGER_EXPORT Logger : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(Logger)
public:
enum class LogType: quint8 {
LogInfo = 0, // Информация
LogWarning = 1, // Предупреждение
LogError = 2, // Ошибка
LogInput = 3, // Входящие данные
LogOutput = 4, // Исходящие данные
LogDebug = 5 // Отладочная информация
};
Q_ENUM(LogType)
static bool isRunning();
static Logger& instance();
static QString logTypeToString(LogType logType);
static QString logTypeToString(quint8 logType);
static LogType stringToLogType(const QString &stringType);
static LogType qtMsgToLogType(QtMsgType type);
static const QLoggingCategory &inputData();
static const QLoggingCategory &outputData();
static QString logDirectory();
public slots:
void start();
void stop();
void writeLogQtType(QtMsgType type, const QMessageLogContext &context, const QString &msg);
void writeLog(const QString &msg, LogType type = LogType::LogInfo, const QString &objectName = QString());
private:
const int maximumLogCount {100};
LogWorker *logWorker {nullptr};
QThread *workThread {nullptr};
explicit Logger(QObject *parent = nullptr);
virtual ~Logger();
};
//==============================================================================
class LOGGER_EXPORT FunctionLogger
{
public:
explicit FunctionLogger(const char *file,
int line,
const char *name,
const QLoggingCategory &category = QLoggingCategory("Function"));
~FunctionLogger();
private:
static std::atomic<size_t> counter;
size_t id;
QString functionName;
QString logCategory;
};
//==============================================================================
#define LOG_FUNCTION(...) FunctionLogger funcLogger(__FILE__, __LINE__, __PRETTY_FUNCTION__ __VA_OPT__(, __VA_ARGS__))
//==============================================================================
Q_DECLARE_METATYPE(Logger::LogType)
#endif // LOGGER_H