chore: logger refactoring to comply with clang-tidy (#2)

* fix: fix file logger constructor
* refactor: clang-tidy fixes
* fix: fix gear
This commit is contained in:
Michael Chernigin 2023-09-19 13:03:45 +04:00 committed by GitHub
parent 4ad7b7e0b0
commit 8a980e3317
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 102 additions and 52 deletions

View File

@ -30,9 +30,8 @@ Common BaseALT projects library
%files %files
%_includedir/%name/ %_includedir/%name/
%_libdir/libbaselib.so %_libdir/liblogger.so
%_libdir/libbaselib.so.0.0.1 %_libdir/cmake
%_libdir/libbaselib.so.1
%changelog %changelog
* Wed Sep 13 2023 Michael Chernigin <chernigin@altlinux.org> 0.0.1-alt1 * Wed Sep 13 2023 Michael Chernigin <chernigin@altlinux.org> 0.0.1-alt1

View File

@ -1,7 +1,6 @@
cmake_minimum_required(VERSION 3.15) cmake_minimum_required(VERSION 3.15)
project(baselib project(baselib
LANGUAGES CXX
VERSION 0.0.1) VERSION 0.0.1)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

View File

@ -7,10 +7,13 @@ int main(int argc, char *argv[])
auto loggerManager = base::logger::LoggerManager::globalInstance(); auto loggerManager = base::logger::LoggerManager::globalInstance();
loggerManager->addLogger<base::logger::ConsoleLogger>(QtDebugMsg); loggerManager->addLogger<base::logger::ConsoleLogger>(QtDebugMsg);
loggerManager->addLogger<base::logger::FileLogger>(QtWarningMsg); loggerManager->addLogger<base::logger::FileLogger>(QtWarningMsg, "appname", "appname.log");
loggerManager->addLogger<base::logger::SyslogLogger>(LOG_LEVEL_DISABLED); loggerManager->addLogger<base::logger::SyslogLogger>(LOG_LEVEL_DISABLED, "appname");
qDebug() << "Hello?";
qInfo() << "Hello?";
qWarning() << "Hello?"; qWarning() << "Hello?";
qCritical() << "Hello?";
return 0; return 0;
} }

View File

@ -35,7 +35,13 @@ class BASELIB_CORE_EXPORT FileLogger : public Logger
{ {
public: public:
explicit FileLogger(const char *folderName, const char *fileName); explicit FileLogger(const char *folderName, const char *fileName);
~FileLogger(); ~FileLogger() override;
public:
FileLogger(const FileLogger &) = delete; // copy ctor
FileLogger(FileLogger &&) = delete; // move ctor
FileLogger &operator=(const FileLogger &) = delete; // copy assignment
FileLogger &operator=(FileLogger &&) = delete; // move assignment
private: private:
void log(const LoggerMessage &message) override; void log(const LoggerMessage &message) override;

View File

@ -26,6 +26,7 @@
#include <fstream> #include <fstream>
#include <unordered_map> #include <unordered_map>
#include <QtMsgHandler> #include <QtMsgHandler>
#define LOG_LEVEL_DISABLED static_cast<QtMsgType>(-1) #define LOG_LEVEL_DISABLED static_cast<QtMsgType>(-1)
@ -45,7 +46,7 @@ public:
void logMessage(const LoggerMessage &message); void logMessage(const LoggerMessage &message);
private: public:
Logger(const Logger &) = delete; // copy ctor Logger(const Logger &) = delete; // copy ctor
Logger(Logger &&) = delete; // move ctor Logger(Logger &&) = delete; // move ctor
Logger &operator=(const Logger &) = delete; // copy assignment Logger &operator=(const Logger &) = delete; // copy assignment

View File

@ -56,16 +56,15 @@ public:
const std::string &function, const std::string &function,
const uint32_t line); const uint32_t line);
template <typename T, typename... Args>
template <typename T> void addLogger(const QtMsgType &level, Args... args)
void addLogger(const QtMsgType &level)
{ {
auto logger = std::make_shared<T>(); auto logger = std::make_shared<T>(args...);
logger->setLogLevel(level); logger->setLogLevel(level);
this->addLogger(logger); this->addLogger(logger);
} }
private: public:
LoggerManager(const LoggerManager &) = delete; // copy ctor LoggerManager(const LoggerManager &) = delete; // copy ctor
LoggerManager(LoggerManager &&) = delete; // move ctor LoggerManager(LoggerManager &&) = delete; // move ctor
LoggerManager &operator=(const LoggerManager &) = delete; // copy assignment LoggerManager &operator=(const LoggerManager &) = delete; // copy assignment

View File

@ -34,32 +34,15 @@ namespace logger
class LoggerMessage class LoggerMessage
{ {
public: public:
LoggerMessage(const QtMsgType &msgType_, LoggerMessage(const QtMsgType &msgType,
const std::string &message_, std::string message,
const std::string &filePath_, std::string filePath,
const std::string &functionName_, std::string functionName,
const uint32_t line_, const uint32_t line,
const std::tm &time_, const std::tm &time,
const std::thread::id &threadId_) 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 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 QtMsgType &msgType;
const std::string message; const std::string message;

View File

@ -34,8 +34,14 @@ namespace logger
class BASELIB_CORE_EXPORT SyslogLogger : public Logger class BASELIB_CORE_EXPORT SyslogLogger : public Logger
{ {
public: public:
SyslogLogger(); SyslogLogger(const char *appName);
~SyslogLogger(); ~SyslogLogger() override;
public:
SyslogLogger(const SyslogLogger &) = delete; // copy ctor
SyslogLogger(SyslogLogger &&) = delete; // move ctor
SyslogLogger &operator=(const SyslogLogger &) = delete; // copy assignment
SyslogLogger &operator=(SyslogLogger &&) = delete; // move assignment
private: private:
void log(const LoggerMessage &message) override; void log(const LoggerMessage &message) override;

View File

@ -2,6 +2,7 @@ find_package(Qt5 COMPONENTS Core REQUIRED)
add_library(logger SHARED add_library(logger SHARED
loggermanager.cpp loggermanager.cpp
loggermessage.cpp
logger.cpp logger.cpp
consolelogger.cpp consolelogger.cpp
filelogger.cpp filelogger.cpp

View File

@ -32,7 +32,7 @@ static bool checkColorSupport(int fd)
bool is_tty = isatty(fd); bool is_tty = isatty(fd);
const char *TERM = std::getenv("TERM"); const char *TERM = std::getenv("TERM");
return is_tty && TERM != NULL && strcmp(TERM, "dumb") != 0; return is_tty && TERM != nullptr && strcmp(TERM, "dumb") != 0;
} }
static std::string colorize(const std::string &text, const char *params) static std::string colorize(const std::string &text, const char *params)
@ -45,9 +45,8 @@ namespace base
namespace logger namespace logger
{ {
ConsoleLogger::ConsoleLogger() ConsoleLogger::ConsoleLogger()
{ : hasColorSupport(checkColorSupport(STDERR_FILENO))
this->hasColorSupport = checkColorSupport(STDERR_FILENO); {}
}
void ConsoleLogger::log(const LoggerMessage &message) void ConsoleLogger::log(const LoggerMessage &message)
{ {

View File

@ -56,7 +56,7 @@ void FileLogger::log(const LoggerMessage &message)
std::string FileLogger::getHomeDir() std::string FileLogger::getHomeDir()
{ {
const char *HOME = getenv("HOME"); const char *HOME = getenv("HOME");
if (HOME != NULL) if (HOME != nullptr)
{ {
return HOME; return HOME;
} }
@ -68,12 +68,12 @@ std::string FileLogger::getHomeDir()
} }
char *resultBuf = new char[bufsize]; char *resultBuf = new char[bufsize];
passwd pwd; passwd pwd{};
passwd *result; passwd *result = nullptr;
getpwuid_r(getuid(), &pwd, resultBuf, bufsize, &result); getpwuid_r(getuid(), &pwd, resultBuf, bufsize, &result);
std::string homeDir; std::string homeDir;
if (result != NULL) if (result != nullptr)
{ {
homeDir = result->pw_dir; homeDir = result->pw_dir;
} }
@ -90,7 +90,7 @@ std::string FileLogger::getHomeDir()
bool FileLogger::ensureDir(const char *path) bool FileLogger::ensureDir(const char *path)
{ {
struct stat sb; struct stat sb{};
if (stat(path, &sb) != 0 && mkdir(path, 0750) != 0) if (stat(path, &sb) != 0 && mkdir(path, 0750) != 0)
{ {
std::cerr << "FileLogger Error: Cannot create log directory (" << path << ")" << std::endl; std::cerr << "FileLogger Error: Cannot create log directory (" << path << ")" << std::endl;

View File

@ -25,7 +25,7 @@
static std::tm getCurrentTime() static std::tm getCurrentTime()
{ {
time_t time; time_t time = 0;
std::time(&time); std::time(&time);
// NOTE: localtime returns a pointer to a statically allocated object // NOTE: localtime returns a pointer to a statically allocated object

View File

@ -0,0 +1,54 @@
/***********************************************************************************************************************
**
** 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 "loggermessage.h"
#include <iomanip>
namespace base
{
namespace logger
{
LoggerMessage::LoggerMessage(const QtMsgType &msgType_,
std::string message_,
std::string filePath_,
std::string functionName_,
const uint32_t line_,
const std::tm &time_,
const std::thread::id &threadId_)
: msgType(msgType_)
, message(std::move(message_))
, filePath(std::move(filePath_))
, functionName(std::move(functionName_))
, line(line_)
, time(time_)
, threadId(threadId_)
{}
std::string LoggerMessage::getTimeFormatted(const char *format) const
{
std::stringstream buffer;
buffer << std::put_time(&this->time, format);
return buffer.str();
}
} // namespace logger
} // namespace base

View File

@ -27,9 +27,9 @@ namespace base
{ {
namespace logger namespace logger
{ {
SyslogLogger::SyslogLogger() SyslogLogger::SyslogLogger(const char *appName)
{ {
openlog("gpui-main", (LOG_CONS | LOG_PERROR | LOG_PID), LOG_DAEMON); openlog(appName, (LOG_CONS | LOG_PID), LOG_DAEMON);
} }
SyslogLogger::~SyslogLogger() SyslogLogger::~SyslogLogger()