From 8a980e3317a4be00123ab688d827086e35a0bbc9 Mon Sep 17 00:00:00 2001 From: Michael Chernigin <59616661+mchernigin@users.noreply.github.com> Date: Tue, 19 Sep 2023 13:03:45 +0400 Subject: [PATCH] chore: logger refactoring to comply with clang-tidy (#2) * fix: fix file logger constructor * refactor: clang-tidy fixes * fix: fix gear --- .gear/baselib.spec | 5 ++-- CMakeLists.txt | 1 - example/main.cpp | 7 +++-- include/logger/filelogger.h | 8 ++++- include/logger/logger.h | 3 +- include/logger/loggermanager.h | 9 +++--- include/logger/loggermessage.h | 33 +++++---------------- include/logger/sysloglogger.h | 10 +++++-- src/logger/CMakeLists.txt | 1 + src/logger/consolelogger.cpp | 7 ++--- src/logger/filelogger.cpp | 10 +++---- src/logger/loggermanager.cpp | 2 +- src/logger/loggermessage.cpp | 54 ++++++++++++++++++++++++++++++++++ src/logger/sysloglogger.cpp | 4 +-- 14 files changed, 102 insertions(+), 52 deletions(-) create mode 100644 src/logger/loggermessage.cpp diff --git a/.gear/baselib.spec b/.gear/baselib.spec index ae9e98e..6dc898e 100644 --- a/.gear/baselib.spec +++ b/.gear/baselib.spec @@ -30,9 +30,8 @@ Common BaseALT projects library %files %_includedir/%name/ -%_libdir/libbaselib.so -%_libdir/libbaselib.so.0.0.1 -%_libdir/libbaselib.so.1 +%_libdir/liblogger.so +%_libdir/cmake %changelog * Wed Sep 13 2023 Michael Chernigin 0.0.1-alt1 diff --git a/CMakeLists.txt b/CMakeLists.txt index 662bb5c..7977736 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,6 @@ cmake_minimum_required(VERSION 3.15) project(baselib - LANGUAGES CXX VERSION 0.0.1) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) diff --git a/example/main.cpp b/example/main.cpp index f1bbdb6..7e864ae 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -7,10 +7,13 @@ int main(int argc, char *argv[]) auto loggerManager = base::logger::LoggerManager::globalInstance(); loggerManager->addLogger(QtDebugMsg); - loggerManager->addLogger(QtWarningMsg); - loggerManager->addLogger(LOG_LEVEL_DISABLED); + loggerManager->addLogger(QtWarningMsg, "appname", "appname.log"); + loggerManager->addLogger(LOG_LEVEL_DISABLED, "appname"); + qDebug() << "Hello?"; + qInfo() << "Hello?"; qWarning() << "Hello?"; + qCritical() << "Hello?"; return 0; } diff --git a/include/logger/filelogger.h b/include/logger/filelogger.h index 2f75795..301fdc4 100644 --- a/include/logger/filelogger.h +++ b/include/logger/filelogger.h @@ -35,7 +35,13 @@ class BASELIB_CORE_EXPORT FileLogger : public Logger { public: 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: void log(const LoggerMessage &message) override; diff --git a/include/logger/logger.h b/include/logger/logger.h index 1a61630..4603b63 100644 --- a/include/logger/logger.h +++ b/include/logger/logger.h @@ -26,6 +26,7 @@ #include #include + #include #define LOG_LEVEL_DISABLED static_cast(-1) @@ -45,7 +46,7 @@ public: void logMessage(const LoggerMessage &message); -private: +public: Logger(const Logger &) = delete; // copy ctor Logger(Logger &&) = delete; // move ctor Logger &operator=(const Logger &) = delete; // copy assignment diff --git a/include/logger/loggermanager.h b/include/logger/loggermanager.h index 76dbfae..162fa5a 100644 --- a/include/logger/loggermanager.h +++ b/include/logger/loggermanager.h @@ -56,16 +56,15 @@ public: const std::string &function, const uint32_t line); - - template - void addLogger(const QtMsgType &level) + template + void addLogger(const QtMsgType &level, Args... args) { - auto logger = std::make_shared(); + auto logger = std::make_shared(args...); logger->setLogLevel(level); this->addLogger(logger); } -private: +public: LoggerManager(const LoggerManager &) = delete; // copy ctor LoggerManager(LoggerManager &&) = delete; // move ctor LoggerManager &operator=(const LoggerManager &) = delete; // copy assignment diff --git a/include/logger/loggermessage.h b/include/logger/loggermessage.h index f2a878c..3fad034 100644 --- a/include/logger/loggermessage.h +++ b/include/logger/loggermessage.h @@ -34,32 +34,15 @@ 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_) - {} + 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); - 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); - } + std::string getTimeFormatted(const char *format) const; const QtMsgType &msgType; const std::string message; diff --git a/include/logger/sysloglogger.h b/include/logger/sysloglogger.h index b9a7d1a..68b7d47 100644 --- a/include/logger/sysloglogger.h +++ b/include/logger/sysloglogger.h @@ -34,8 +34,14 @@ namespace logger class BASELIB_CORE_EXPORT SyslogLogger : public Logger { public: - SyslogLogger(); - ~SyslogLogger(); + SyslogLogger(const char *appName); + ~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: void log(const LoggerMessage &message) override; diff --git a/src/logger/CMakeLists.txt b/src/logger/CMakeLists.txt index 266d6c1..67b934b 100644 --- a/src/logger/CMakeLists.txt +++ b/src/logger/CMakeLists.txt @@ -2,6 +2,7 @@ find_package(Qt5 COMPONENTS Core REQUIRED) add_library(logger SHARED loggermanager.cpp + loggermessage.cpp logger.cpp consolelogger.cpp filelogger.cpp diff --git a/src/logger/consolelogger.cpp b/src/logger/consolelogger.cpp index eb59c13..e7088e3 100644 --- a/src/logger/consolelogger.cpp +++ b/src/logger/consolelogger.cpp @@ -32,7 +32,7 @@ static bool checkColorSupport(int fd) bool is_tty = isatty(fd); 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) @@ -45,9 +45,8 @@ namespace base namespace logger { ConsoleLogger::ConsoleLogger() -{ - this->hasColorSupport = checkColorSupport(STDERR_FILENO); -} + : hasColorSupport(checkColorSupport(STDERR_FILENO)) +{} void ConsoleLogger::log(const LoggerMessage &message) { diff --git a/src/logger/filelogger.cpp b/src/logger/filelogger.cpp index b2fbca8..8135243 100644 --- a/src/logger/filelogger.cpp +++ b/src/logger/filelogger.cpp @@ -56,7 +56,7 @@ void FileLogger::log(const LoggerMessage &message) std::string FileLogger::getHomeDir() { const char *HOME = getenv("HOME"); - if (HOME != NULL) + if (HOME != nullptr) { return HOME; } @@ -68,12 +68,12 @@ std::string FileLogger::getHomeDir() } char *resultBuf = new char[bufsize]; - passwd pwd; - passwd *result; + passwd pwd{}; + passwd *result = nullptr; getpwuid_r(getuid(), &pwd, resultBuf, bufsize, &result); std::string homeDir; - if (result != NULL) + if (result != nullptr) { homeDir = result->pw_dir; } @@ -90,7 +90,7 @@ std::string FileLogger::getHomeDir() bool FileLogger::ensureDir(const char *path) { - struct stat sb; + struct stat sb{}; if (stat(path, &sb) != 0 && mkdir(path, 0750) != 0) { std::cerr << "FileLogger Error: Cannot create log directory (" << path << ")" << std::endl; diff --git a/src/logger/loggermanager.cpp b/src/logger/loggermanager.cpp index 22f121d..ef260b6 100644 --- a/src/logger/loggermanager.cpp +++ b/src/logger/loggermanager.cpp @@ -25,7 +25,7 @@ static std::tm getCurrentTime() { - time_t time; + time_t time = 0; std::time(&time); // NOTE: localtime returns a pointer to a statically allocated object diff --git a/src/logger/loggermessage.cpp b/src/logger/loggermessage.cpp new file mode 100644 index 0000000..c09fb71 --- /dev/null +++ b/src/logger/loggermessage.cpp @@ -0,0 +1,54 @@ +/*********************************************************************************************************************** +** +** Copyright (C) 2022 BaseALT Ltd. +** +** 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 + +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 + diff --git a/src/logger/sysloglogger.cpp b/src/logger/sysloglogger.cpp index f66a46e..ee62fe3 100644 --- a/src/logger/sysloglogger.cpp +++ b/src/logger/sysloglogger.cpp @@ -27,9 +27,9 @@ namespace base { 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()