mirror of
https://github.com/mchernigin/libqbase.git
synced 2025-01-03 13:17:41 +03:00
start
This commit is contained in:
commit
c0d789db33
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
build
|
10
CMakeLists.txt
Normal file
10
CMakeLists.txt
Normal file
@ -0,0 +1,10 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
|
||||
project(baselib)
|
||||
|
||||
set(SOURCES
|
||||
main.cpp)
|
||||
|
||||
add_subdirectory(logger)
|
||||
|
||||
add_library(baselib STATIC ${SOURCES})
|
32
base.h
Normal file
32
base.h
Normal file
@ -0,0 +1,32 @@
|
||||
/***********************************************************************************************************************
|
||||
**
|
||||
** Copyright (C) 2021 BaseALT Ltd. <org@basealt.ru>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or
|
||||
** modify it under the terms of the GNU General Public License
|
||||
** as published by the Free Software Foundation; either version 2
|
||||
** of the License, or (at your option) any later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful,
|
||||
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
** GNU General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU General Public License
|
||||
** along with this program; if not, write to the Free Software
|
||||
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
***********************************************************************************************************************/
|
||||
|
||||
#ifndef BASELIB_BASE_H
|
||||
#define BASELIB_BASE_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#ifdef BASELIB_CORE_LIBRARY
|
||||
#define BASELIB_CORE_EXPORT BASELIB_SYMBOL_EXPORT
|
||||
#else
|
||||
#define BASELIB_CORE_EXPORT BASELIB_SYMBOL_IMPORT
|
||||
#endif
|
||||
|
||||
#endif // BASELIB_BASE_H
|
39
common.h
Normal file
39
common.h
Normal file
@ -0,0 +1,39 @@
|
||||
/***********************************************************************************************************************
|
||||
**
|
||||
** Copyright (C) 2021 BaseALT Ltd. <org@basealt.ru>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or
|
||||
** modify it under the terms of the GNU General Public License
|
||||
** as published by the Free Software Foundation; either version 2
|
||||
** of the License, or (at your option) any later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful,
|
||||
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
** GNU General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU General Public License
|
||||
** along with this program; if not, write to the Free Software
|
||||
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
***********************************************************************************************************************/
|
||||
|
||||
#ifndef BASELIB_COMMON_H
|
||||
#define BASELIB_COMMON_H
|
||||
|
||||
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__CYGWIN__)
|
||||
#define BASELIB_SYMBOL_EXPORT __attribute__((__dllexport__))
|
||||
#define BASELIB_SYMBOL_IMPORT __attribute__((__dllimport__))
|
||||
#else
|
||||
|
||||
#if defined (__LCC__)
|
||||
#define BASELIB_SYMBOL_EXPORT
|
||||
#else
|
||||
#define BASELIB_SYMBOL_EXPORT __attribute__((__visibility__("default")))
|
||||
#endif
|
||||
|
||||
#define BASELIB_SYMBOL_IMPORT
|
||||
#endif
|
||||
|
||||
#endif // BASELIB_COMMON_H
|
||||
|
28
logger/CMakeLists.txt
Normal file
28
logger/CMakeLists.txt
Normal file
@ -0,0 +1,28 @@
|
||||
find_package(Qt5 COMPONENTS Core REQUIRED)
|
||||
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
set(HEADERS
|
||||
loggermanager.h
|
||||
loggermessage.h
|
||||
logger.h
|
||||
consolelogger.h
|
||||
filelogger.h
|
||||
sysloglogger.h
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
loggermanager.cpp
|
||||
logger.cpp
|
||||
consolelogger.cpp
|
||||
filelogger.cpp
|
||||
sysloglogger.cpp
|
||||
)
|
||||
|
||||
add_definitions(
|
||||
-DBASELIB_CORE_LIBRARY
|
||||
)
|
||||
|
||||
add_library(baselib-qt-logger STATIC ${SOURCES} ${HEADERS})
|
||||
target_include_directories(baselib-qt-logger PUBLIC ${Qt5Gui_INCLUDE_DIRS})
|
||||
target_link_libraries(baselib-qt-logger Qt5::Core)
|
82
logger/consolelogger.cpp
Normal file
82
logger/consolelogger.cpp
Normal file
@ -0,0 +1,82 @@
|
||||
/***********************************************************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 BaseALT Ltd. <org@basealt.ru>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or
|
||||
** modify it under the terms of the GNU General Public License
|
||||
** as published by the Free Software Foundation; either version 2
|
||||
** of the License, or (at your option) any later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful,
|
||||
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
** GNU General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU General Public License
|
||||
** along with this program; if not, write to the Free Software
|
||||
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
***********************************************************************************************************************/
|
||||
|
||||
#include "consolelogger.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
|
||||
static bool checkColorSupport(int fd)
|
||||
{
|
||||
// TODO(mchernigin): use `tput colors`, and use method bellow only if `tput` returns !0
|
||||
|
||||
bool is_tty = isatty(fd);
|
||||
const char *TERM = std::getenv("TERM");
|
||||
|
||||
return is_tty && TERM != NULL && strcmp(TERM, "dumb") != 0;
|
||||
}
|
||||
|
||||
static std::string colorize(const std::string &text, const char *params)
|
||||
{
|
||||
return std::string("\033[") + params + "m" + text + "\033[0m";
|
||||
}
|
||||
|
||||
namespace base
|
||||
{
|
||||
namespace logger
|
||||
{
|
||||
ConsoleLogger::ConsoleLogger()
|
||||
{
|
||||
this->hasColorSupport = checkColorSupport(STDERR_FILENO);
|
||||
}
|
||||
|
||||
void ConsoleLogger::log(const LoggerMessage &message)
|
||||
{
|
||||
std::string prefix = this->logLevelMap.at(message.msgType);
|
||||
|
||||
if (this->hasColorSupport)
|
||||
{
|
||||
switch (message.msgType)
|
||||
{
|
||||
case QtDebugMsg:
|
||||
prefix = this->hasColorSupport ? colorize(prefix, "1;96") : prefix;
|
||||
break;
|
||||
case QtInfoMsg:
|
||||
prefix = this->hasColorSupport ? colorize(prefix, "1;34") : prefix;
|
||||
break;
|
||||
case QtWarningMsg:
|
||||
prefix = this->hasColorSupport ? colorize(prefix, "1;33") : prefix;
|
||||
break;
|
||||
case QtCriticalMsg:
|
||||
prefix = this->hasColorSupport ? colorize(prefix, "1;31") : prefix;
|
||||
break;
|
||||
case QtFatalMsg:
|
||||
prefix = this->hasColorSupport ? colorize(prefix, "1;91") : prefix;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::clog << message.getTimeFormatted("%H:%M:%S") << " | " << prefix << ": " << message.message << " ("
|
||||
<< message.filePath << ":" << message.line << ")" << std::endl;
|
||||
}
|
||||
} // namespace logger
|
||||
} // namespace base
|
45
logger/consolelogger.h
Normal file
45
logger/consolelogger.h
Normal file
@ -0,0 +1,45 @@
|
||||
/***********************************************************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 BaseALT Ltd. <org@basealt.ru>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or
|
||||
** modify it under the terms of the GNU General Public License
|
||||
** as published by the Free Software Foundation; either version 2
|
||||
** of the License, or (at your option) any later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful,
|
||||
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
** GNU General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU General Public License
|
||||
** along with this program; if not, write to the Free Software
|
||||
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
***********************************************************************************************************************/
|
||||
|
||||
#ifndef BASELIB_CONSOLE_LOGGER_H
|
||||
#define BASELIB_CONSOLE_LOGGER_H
|
||||
|
||||
#include "logger.h"
|
||||
#include "loggermessage.h"
|
||||
#include "../base.h"
|
||||
|
||||
namespace base
|
||||
{
|
||||
namespace logger
|
||||
{
|
||||
class BASELIB_CORE_EXPORT ConsoleLogger : public Logger
|
||||
{
|
||||
public:
|
||||
ConsoleLogger();
|
||||
|
||||
private:
|
||||
void log(const LoggerMessage &message) override;
|
||||
|
||||
bool hasColorSupport = false;
|
||||
};
|
||||
} // namespace logger
|
||||
} // namespace base
|
||||
|
||||
#endif // BASELIB_CONSOLE_LOGGER_H
|
103
logger/filelogger.cpp
Normal file
103
logger/filelogger.cpp
Normal file
@ -0,0 +1,103 @@
|
||||
/***********************************************************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 BaseALT Ltd. <org@basealt.ru>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or
|
||||
** modify it under the terms of the GNU General Public License
|
||||
** as published by the Free Software Foundation; either version 2
|
||||
** of the License, or (at your option) any later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful,
|
||||
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
** GNU General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU General Public License
|
||||
** along with this program; if not, write to the Free Software
|
||||
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
***********************************************************************************************************************/
|
||||
|
||||
#include "filelogger.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <pwd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace base
|
||||
{
|
||||
namespace logger
|
||||
{
|
||||
FileLogger::FileLogger(const char* folderName, const char *fileName)
|
||||
{
|
||||
// TODO(mchernigin): timestamp on creation/editing log file is UTC and not local timezone
|
||||
std::string homeDir = getHomeDir();
|
||||
std::string logDir = homeDir + "/.local/share/" + folderName + "/";
|
||||
ensureDir(logDir.c_str());
|
||||
|
||||
std::string logFile = logDir + fileName;
|
||||
this->logFileStream.open(logFile, std::fstream::out | std::fstream::app);
|
||||
}
|
||||
|
||||
FileLogger::~FileLogger()
|
||||
{
|
||||
this->logFileStream.close();
|
||||
}
|
||||
|
||||
void FileLogger::log(const LoggerMessage &message)
|
||||
{
|
||||
const char *prefix = this->logLevelMap.at(message.msgType);
|
||||
this->logFileStream << message.getTimeFormatted("%H:%M:%S") << " | " << prefix << ": " << message.message << " ("
|
||||
<< message.filePath << ":" << message.line << ")" << std::endl;
|
||||
}
|
||||
|
||||
std::string FileLogger::getHomeDir()
|
||||
{
|
||||
const char *HOME = getenv("HOME");
|
||||
if (HOME != NULL)
|
||||
{
|
||||
return HOME;
|
||||
}
|
||||
|
||||
long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
|
||||
if (bufsize == -1)
|
||||
{
|
||||
bufsize = 1 << 14;
|
||||
}
|
||||
|
||||
char *resultBuf = new char[bufsize];
|
||||
passwd pwd;
|
||||
passwd *result;
|
||||
getpwuid_r(getuid(), &pwd, resultBuf, bufsize, &result);
|
||||
|
||||
std::string homeDir;
|
||||
if (result != NULL)
|
||||
{
|
||||
homeDir = result->pw_dir;
|
||||
}
|
||||
else
|
||||
{
|
||||
homeDir = "/root";
|
||||
std::cerr << "FileLogger Error: cannot determine home directory, defaulting to " << homeDir << std::endl;
|
||||
}
|
||||
|
||||
delete[] resultBuf;
|
||||
|
||||
return homeDir;
|
||||
}
|
||||
|
||||
bool FileLogger::ensureDir(const char *path)
|
||||
{
|
||||
struct stat sb;
|
||||
if (stat(path, &sb) != 0 && mkdir(path, 0750) != 0)
|
||||
{
|
||||
std::cerr << "FileLogger Error: Cannot create log directory (" << path << ")" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
} // namespace logger
|
||||
} // namespace base
|
51
logger/filelogger.h
Normal file
51
logger/filelogger.h
Normal file
@ -0,0 +1,51 @@
|
||||
/***********************************************************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 BaseALT Ltd. <org@basealt.ru>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or
|
||||
** modify it under the terms of the GNU General Public License
|
||||
** as published by the Free Software Foundation; either version 2
|
||||
** of the License, or (at your option) any later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful,
|
||||
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
** GNU General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU General Public License
|
||||
** along with this program; if not, write to the Free Software
|
||||
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
***********************************************************************************************************************/
|
||||
|
||||
#ifndef BASELIB_FILE_LOGGER_H
|
||||
#define BASELIB_FILE_LOGGER_H
|
||||
|
||||
#include "logger.h"
|
||||
#include "loggermessage.h"
|
||||
#include "../base.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace base
|
||||
{
|
||||
namespace logger
|
||||
{
|
||||
class BASELIB_CORE_EXPORT FileLogger : public Logger
|
||||
{
|
||||
public:
|
||||
explicit FileLogger(const char *folderName, const char *fileName);
|
||||
~FileLogger();
|
||||
|
||||
private:
|
||||
void log(const LoggerMessage &message) override;
|
||||
|
||||
static std::string getHomeDir();
|
||||
static bool ensureDir(const char *path);
|
||||
|
||||
std::fstream logFileStream = {};
|
||||
};
|
||||
} // namespace logger
|
||||
} // namespace base
|
||||
|
||||
#endif // BASELIB_FILE_LOGGER_H
|
45
logger/logger.cpp
Normal file
45
logger/logger.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
/***********************************************************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 BaseALT Ltd. <org@basealt.ru>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or
|
||||
** modify it under the terms of the GNU General Public License
|
||||
** as published by the Free Software Foundation; either version 2
|
||||
** of the License, or (at your option) any later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful,
|
||||
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
** GNU General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU General Public License
|
||||
** along with this program; if not, write to the Free Software
|
||||
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
***********************************************************************************************************************/
|
||||
|
||||
#include "logger.h"
|
||||
|
||||
namespace base
|
||||
{
|
||||
namespace logger
|
||||
{
|
||||
void Logger::setLogLevel(QtMsgType level)
|
||||
{
|
||||
this->minLogLevel = level;
|
||||
}
|
||||
|
||||
bool Logger::isLogLevel(QtMsgType level)
|
||||
{
|
||||
return level >= this->minLogLevel;
|
||||
}
|
||||
|
||||
void Logger::logMessage(const LoggerMessage &message)
|
||||
{
|
||||
if (isLogLevel(message.msgType))
|
||||
{
|
||||
log(message);
|
||||
}
|
||||
}
|
||||
} // namespace logger
|
||||
} // namespace base
|
71
logger/logger.h
Normal file
71
logger/logger.h
Normal file
@ -0,0 +1,71 @@
|
||||
/***********************************************************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 BaseALT Ltd. <org@basealt.ru>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or
|
||||
** modify it under the terms of the GNU General Public License
|
||||
** as published by the Free Software Foundation; either version 2
|
||||
** of the License, or (at your option) any later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful,
|
||||
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
** GNU General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU General Public License
|
||||
** along with this program; if not, write to the Free Software
|
||||
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
***********************************************************************************************************************/
|
||||
|
||||
#ifndef BASELIB_ABSTRACT_LOGGER_H
|
||||
#define BASELIB_ABSTRACT_LOGGER_H
|
||||
|
||||
#include "../base.h"
|
||||
#include "loggermessage.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <unordered_map>
|
||||
#include <QtMsgHandler>
|
||||
|
||||
#define LOG_LEVEL_DISABLED static_cast<QtMsgType>(-1)
|
||||
|
||||
namespace base
|
||||
{
|
||||
namespace logger
|
||||
{
|
||||
class BASELIB_CORE_EXPORT Logger
|
||||
{
|
||||
public:
|
||||
Logger() = default;
|
||||
virtual ~Logger() = default;
|
||||
|
||||
void setLogLevel(QtMsgType level);
|
||||
bool isLogLevel(QtMsgType level);
|
||||
|
||||
void logMessage(const LoggerMessage &message);
|
||||
|
||||
private:
|
||||
Logger(const Logger &) = delete; // copy ctor
|
||||
Logger(Logger &&) = delete; // move ctor
|
||||
Logger &operator=(const Logger &) = delete; // copy assignment
|
||||
Logger &operator=(Logger &&) = delete; // move assignment
|
||||
|
||||
private:
|
||||
virtual void log(const LoggerMessage &message) = 0;
|
||||
|
||||
QtMsgType minLogLevel = QtDebugMsg;
|
||||
|
||||
protected:
|
||||
const std::unordered_map<QtMsgType, const char *> logLevelMap = {
|
||||
{QtDebugMsg, "DEBUG"},
|
||||
{QtInfoMsg, "INFO"},
|
||||
{QtWarningMsg, "WARNING"},
|
||||
{QtCriticalMsg, "CRITICAL"},
|
||||
{QtFatalMsg, "FATAL"},
|
||||
};
|
||||
};
|
||||
} // namespace logger
|
||||
} // namespace base
|
||||
|
||||
#endif // BASELIB_ABSTRACT_LOGGER_H
|
122
logger/loggermanager.cpp
Normal file
122
logger/loggermanager.cpp
Normal file
@ -0,0 +1,122 @@
|
||||
/***********************************************************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 BaseALT Ltd. <org@basealt.ru>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or
|
||||
** modify it under the terms of the GNU General Public License
|
||||
** as published by the Free Software Foundation; either version 2
|
||||
** of the License, or (at your option) any later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful,
|
||||
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
** GNU General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU General Public License
|
||||
** along with this program; if not, write to the Free Software
|
||||
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
***********************************************************************************************************************/
|
||||
|
||||
#include "loggermanager.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
static std::tm getCurrentTime()
|
||||
{
|
||||
time_t time;
|
||||
std::time(&time);
|
||||
|
||||
// NOTE: localtime returns a pointer to a statically allocated object
|
||||
return *std::localtime(&time);
|
||||
}
|
||||
|
||||
namespace base
|
||||
{
|
||||
namespace logger
|
||||
{
|
||||
class LoggerManagerPrivate
|
||||
{
|
||||
public:
|
||||
std::vector<std::shared_ptr<Logger>> loggers = {};
|
||||
mutable std::mutex loggerMutex = {};
|
||||
};
|
||||
|
||||
std::shared_ptr<LoggerManager> LoggerManager::instance{nullptr};
|
||||
|
||||
LoggerManager::LoggerManager()
|
||||
: d(new LoggerManagerPrivate)
|
||||
{
|
||||
qInstallMessageHandler(LoggerManager::messageHandler);
|
||||
}
|
||||
|
||||
LoggerManager::~LoggerManager()
|
||||
{
|
||||
delete d;
|
||||
}
|
||||
|
||||
void LoggerManager::destroyInstance()
|
||||
{
|
||||
instance.reset();
|
||||
}
|
||||
|
||||
void LoggerManager::addLogger(std::shared_ptr<Logger> logger)
|
||||
{
|
||||
std::lock_guard<std::mutex> lockGuardLogger(d->loggerMutex);
|
||||
d->loggers.push_back(logger);
|
||||
}
|
||||
|
||||
void LoggerManager::removeLogger(std::shared_ptr<Logger> logger)
|
||||
{
|
||||
std::lock_guard<std::mutex> lockGuardLogger(d->loggerMutex);
|
||||
const auto search = std::find(d->loggers.begin(), d->loggers.end(), logger);
|
||||
|
||||
if (search != d->loggers.end())
|
||||
{
|
||||
d->loggers.erase(search);
|
||||
}
|
||||
}
|
||||
|
||||
void LoggerManager::clearLoggers()
|
||||
{
|
||||
d->loggers.clear();
|
||||
}
|
||||
|
||||
void LoggerManager::log(const QtMsgType &msgType,
|
||||
const std::string &message,
|
||||
const std::string &file,
|
||||
const std::string &function,
|
||||
const uint32_t line)
|
||||
{
|
||||
std::lock_guard<std::mutex> lockGuardLogger(d->loggerMutex);
|
||||
for (const auto &logger : d->loggers)
|
||||
{
|
||||
logger->logMessage(
|
||||
LoggerMessage(msgType, message, file, function, line, getCurrentTime(), std::this_thread::get_id()));
|
||||
}
|
||||
}
|
||||
|
||||
size_t LoggerManager::getLoggerCount() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lockGuardLogger(d->loggerMutex);
|
||||
return d->loggers.size();
|
||||
}
|
||||
|
||||
void LoggerManager::messageHandler(QtMsgType msgType, const QMessageLogContext &context, const QString &msg)
|
||||
{
|
||||
auto logger = globalInstance();
|
||||
std::string file = context.file ? context.file : "";
|
||||
std::string function = context.function ? context.function : "";
|
||||
int line = context.line;
|
||||
|
||||
logger->log(msgType, msg.toStdString(), file, function, line);
|
||||
}
|
||||
|
||||
Q_GLOBAL_STATIC(LoggerManager, loggerInstance)
|
||||
LoggerManager *LoggerManager::globalInstance()
|
||||
{
|
||||
return loggerInstance();
|
||||
}
|
||||
} // namespace logger
|
||||
} // namespace base
|
83
logger/loggermanager.h
Normal file
83
logger/loggermanager.h
Normal file
@ -0,0 +1,83 @@
|
||||
/***********************************************************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 BaseALT Ltd. <org@basealt.ru>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or
|
||||
** modify it under the terms of the GNU General Public License
|
||||
** as published by the Free Software Foundation; either version 2
|
||||
** of the License, or (at your option) any later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful,
|
||||
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
** GNU General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU General Public License
|
||||
** along with this program; if not, write to the Free Software
|
||||
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
***********************************************************************************************************************/
|
||||
|
||||
#ifndef BASELIB_LOGGER_MANAGER_H
|
||||
#define BASELIB_LOGGER_MANAGER_H
|
||||
|
||||
#include "../base.h"
|
||||
#include "logger.h"
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
#include <QString>
|
||||
|
||||
namespace base
|
||||
{
|
||||
namespace logger
|
||||
{
|
||||
class LoggerManagerPrivate;
|
||||
|
||||
class BASELIB_CORE_EXPORT LoggerManager
|
||||
{
|
||||
public:
|
||||
static LoggerManager *globalInstance();
|
||||
static void destroyInstance();
|
||||
|
||||
LoggerManager();
|
||||
~LoggerManager();
|
||||
|
||||
void addLogger(std::shared_ptr<Logger> logger);
|
||||
void removeLogger(std::shared_ptr<Logger> logger);
|
||||
void clearLoggers();
|
||||
size_t getLoggerCount() const;
|
||||
|
||||
void log(const QtMsgType &msgType,
|
||||
const std::string &message,
|
||||
const std::string &file,
|
||||
const std::string &function,
|
||||
const uint32_t line);
|
||||
|
||||
|
||||
template <typename T>
|
||||
void addLogger(const QtMsgType &level)
|
||||
{
|
||||
auto logger = std::make_shared<T>();
|
||||
logger->setLogLevel(level);
|
||||
this->addLogger(logger);
|
||||
}
|
||||
|
||||
private:
|
||||
LoggerManager(const LoggerManager &) = delete; // copy ctor
|
||||
LoggerManager(LoggerManager &&) = delete; // move ctor
|
||||
LoggerManager &operator=(const LoggerManager &) = delete; // copy assignment
|
||||
LoggerManager &operator=(LoggerManager &&) = delete; // move assignment
|
||||
|
||||
private:
|
||||
static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
|
||||
|
||||
static std::shared_ptr<LoggerManager> instance;
|
||||
LoggerManagerPrivate *d;
|
||||
};
|
||||
} // namespace logger
|
||||
} // namespace base
|
||||
|
||||
#endif // BASELIB_LOGGER_MANAGER_H
|
75
logger/loggermessage.h
Normal file
75
logger/loggermessage.h
Normal file
@ -0,0 +1,75 @@
|
||||
/***********************************************************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 BaseALT Ltd. <org@basealt.ru>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or
|
||||
** modify it under the terms of the GNU General Public License
|
||||
** as published by the Free Software Foundation; either version 2
|
||||
** of the License, or (at your option) any later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful,
|
||||
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
** GNU General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU General Public License
|
||||
** along with this program; if not, write to the Free Software
|
||||
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
***********************************************************************************************************************/
|
||||
|
||||
#ifndef BASELIB_LOGGER_MESSAGE_H
|
||||
#define BASELIB_LOGGER_MESSAGE_H
|
||||
|
||||
#include <ctime>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#include <QtMsgHandler>
|
||||
|
||||
namespace base
|
||||
{
|
||||
namespace logger
|
||||
{
|
||||
class LoggerMessage
|
||||
{
|
||||
public:
|
||||
LoggerMessage(const QtMsgType &msgType_,
|
||||
const std::string &message_,
|
||||
const std::string &filePath_,
|
||||
const std::string &functionName_,
|
||||
const uint32_t line_,
|
||||
const std::tm &time_,
|
||||
const std::thread::id &threadId_)
|
||||
: msgType(msgType_)
|
||||
, message(message_)
|
||||
, filePath(filePath_)
|
||||
, functionName(functionName_)
|
||||
, line(line_)
|
||||
, time(time_)
|
||||
, threadId(threadId_)
|
||||
{}
|
||||
|
||||
std::string getTimeFormatted(const char *format) const
|
||||
{
|
||||
char timeString[50];
|
||||
// NOTE(mchernigin): do all compilers actually support this?
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
|
||||
strftime(timeString, 50, format, &time);
|
||||
#pragma GCC diagnostic pop
|
||||
return std::string(timeString);
|
||||
}
|
||||
|
||||
const QtMsgType &msgType;
|
||||
const std::string message;
|
||||
const std::string filePath;
|
||||
const std::string functionName;
|
||||
const uint32_t line;
|
||||
const std::tm time;
|
||||
const std::thread::id threadId;
|
||||
};
|
||||
} // namespace logger
|
||||
} // namespace base
|
||||
|
||||
#endif // BASELIB_LOGGER_MESSAGE_H
|
30
logger/prelude.h
Normal file
30
logger/prelude.h
Normal file
@ -0,0 +1,30 @@
|
||||
/***********************************************************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 BaseALT Ltd. <org@basealt.ru>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or
|
||||
** modify it under the terms of the GNU General Public License
|
||||
** as published by the Free Software Foundation; either version 2
|
||||
** of the License, or (at your option) any later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful,
|
||||
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
** GNU General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU General Public License
|
||||
** along with this program; if not, write to the Free Software
|
||||
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
***********************************************************************************************************************/
|
||||
|
||||
#ifndef BASELIB_LOGGERS_H
|
||||
#define BASELIB_LOGGERS_H
|
||||
|
||||
#include "loggermanager.h"
|
||||
|
||||
#include "consolelogger.h"
|
||||
#include "filelogger.h"
|
||||
#include "sysloglogger.h"
|
||||
|
||||
#endif // BASELIB_LOGGERS_H
|
66
logger/sysloglogger.cpp
Normal file
66
logger/sysloglogger.cpp
Normal file
@ -0,0 +1,66 @@
|
||||
/***********************************************************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 BaseALT Ltd. <org@basealt.ru>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or
|
||||
** modify it under the terms of the GNU General Public License
|
||||
** as published by the Free Software Foundation; either version 2
|
||||
** of the License, or (at your option) any later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful,
|
||||
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
** GNU General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU General Public License
|
||||
** along with this program; if not, write to the Free Software
|
||||
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
***********************************************************************************************************************/
|
||||
|
||||
#include "sysloglogger.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <syslog.h>
|
||||
|
||||
namespace base
|
||||
{
|
||||
namespace logger
|
||||
{
|
||||
SyslogLogger::SyslogLogger()
|
||||
{
|
||||
openlog("gpui-main", (LOG_CONS | LOG_PERROR | LOG_PID), LOG_DAEMON);
|
||||
}
|
||||
|
||||
SyslogLogger::~SyslogLogger()
|
||||
{
|
||||
closelog();
|
||||
}
|
||||
|
||||
void SyslogLogger::log(const LoggerMessage &message)
|
||||
{
|
||||
const char *prefix = this->logLevelMap.at(message.msgType);
|
||||
|
||||
int logFlag = LOG_DEBUG;
|
||||
switch (message.msgType)
|
||||
{
|
||||
case QtInfoMsg:
|
||||
logFlag = LOG_INFO;
|
||||
break;
|
||||
case QtWarningMsg:
|
||||
logFlag = LOG_WARNING;
|
||||
break;
|
||||
case QtCriticalMsg:
|
||||
logFlag = LOG_ERR;
|
||||
break;
|
||||
case QtFatalMsg:
|
||||
logFlag = LOG_CRIT;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
syslog(logFlag, "%s: %s (%s:%u)", prefix, message.message.c_str(), message.filePath.c_str(), message.line);
|
||||
}
|
||||
} // namespace logger
|
||||
} // namespace base
|
46
logger/sysloglogger.h
Normal file
46
logger/sysloglogger.h
Normal file
@ -0,0 +1,46 @@
|
||||
/***********************************************************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 BaseALT Ltd. <org@basealt.ru>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or
|
||||
** modify it under the terms of the GNU General Public License
|
||||
** as published by the Free Software Foundation; either version 2
|
||||
** of the License, or (at your option) any later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful,
|
||||
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
** GNU General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU General Public License
|
||||
** along with this program; if not, write to the Free Software
|
||||
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
***********************************************************************************************************************/
|
||||
|
||||
#ifndef BASELIB_SYSLOG_LOGGER_H
|
||||
#define BASELIB_SYSLOG_LOGGER_H
|
||||
|
||||
#include "logger.h"
|
||||
#include "loggermessage.h"
|
||||
#include "../base.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace base
|
||||
{
|
||||
namespace logger
|
||||
{
|
||||
class BASELIB_CORE_EXPORT SyslogLogger : public Logger
|
||||
{
|
||||
public:
|
||||
SyslogLogger();
|
||||
~SyslogLogger();
|
||||
|
||||
private:
|
||||
void log(const LoggerMessage &message) override;
|
||||
};
|
||||
} // namespace logger
|
||||
} // namespace base
|
||||
|
||||
#endif // BASELIB_SYSLOG_LOGGER_H
|
Loading…
Reference in New Issue
Block a user