Skip to content

Commit

Permalink
refactor(liblogging): Make Logger the singleton class
Browse files Browse the repository at this point in the history
  • Loading branch information
hparzych committed Oct 10, 2023
1 parent 07a9faf commit c15ac42
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 43 deletions.
13 changes: 7 additions & 6 deletions ebpfdiscoverysrv/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "ebpfdiscovery/Discovery.h"
#include "ebpfdiscovery/DiscoveryBpf.h"
#include "ebpfdiscovery/DiscoveryBpfLoader.h"
#include "logging/Global.h"
#include "logging/Logger.h"

#include <boost/program_options.hpp>

Expand All @@ -15,6 +15,7 @@
#include <unistd.h>

namespace po = boost::program_options;
using logging::Logger;
using logging::LogLevel;

enum class ProgramStatus {
Expand Down Expand Up @@ -56,8 +57,8 @@ static std::string getProgramVersion() {
*/

static void setupLogging(logging::LogLevel logLevel, bool enableStdout, const std::filesystem::path& logDir) {
logging::Global::getInstance().setup("eBPF-Discovery", enableStdout, logDir);
logging::Global::getInstance().setLevel(logLevel);
Logger::getInstance().setup("eBPF-Discovery", enableStdout, logDir);
Logger::getInstance().setLevel(logLevel);
LOG_TRACE("Logging has been set up. (logDir: {})", logDir.string());
}

Expand Down Expand Up @@ -98,13 +99,13 @@ static void runUnixSignalHandlerLoop() {
static int libbpfPrintFn(enum libbpf_print_level level, const char* format, va_list args) {
switch (level) {
case LIBBPF_WARN:
logging::Global::getInstance().vlogf(logging::LogLevel::Warn, format, args);
Logger::getInstance().vlogf(logging::LogLevel::Warn, format, args);
return 0;
case LIBBPF_INFO:
logging::Global::getInstance().vlogf(logging::LogLevel::Info, format, args);
Logger::getInstance().vlogf(logging::LogLevel::Info, format, args);
return 0;
case LIBBPF_DEBUG:
logging::Global::getInstance().vlogf(logging::LogLevel::Debug, format, args);
Logger::getInstance().vlogf(logging::LogLevel::Debug, format, args);
return 0;
}
return 0;
Expand Down
2 changes: 1 addition & 1 deletion libebpfdiscovery/src/Discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "StringFunctions.h"
#include "ebpfdiscovery/Session.h"
#include "logging/Global.h"
#include "logging/Logger.h"

#include <algorithm>
#include <arpa/inet.h>
Expand Down
2 changes: 1 addition & 1 deletion libebpfdiscovery/src/DiscoveryBpfLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
#include "ebpfdiscovery/DiscoveryBpfLoader.h"

#include "logging/Global.h"
#include "logging/Logger.h"

extern "C" {
#include "bpfload/btf_helpers.h"
Expand Down
2 changes: 1 addition & 1 deletion liblogging/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
list(APPEND SOURCES src/Logger.cpp src/Global.cpp)
list(APPEND SOURCES src/Logger.cpp)
set(TARGET logging)

add_library(${TARGET} SHARED ${SOURCES})
Expand Down
23 changes: 0 additions & 23 deletions liblogging/headers/logging/Global.h

This file was deleted.

9 changes: 9 additions & 0 deletions liblogging/headers/logging/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Logger {
Logger(const Logger&) = delete;
Logger& operator=(const Logger&) = delete;

static Logger& getInstance();

void setLevel(enum LogLevel level);
void setup(std::string name, bool logToStdout = true, std::filesystem::path logDir = {});

Expand Down Expand Up @@ -116,3 +118,10 @@ class Logger {
};

} // namespace logging

#define LOG_TRACE(...) logging::Logger::getInstance().trace(__VA_ARGS__)
#define LOG_DEBUG(...) logging::Logger::getInstance().debug(__VA_ARGS__)
#define LOG_INFO(...) logging::Logger::getInstance().info(__VA_ARGS__)
#define LOG_WARN(...) logging::Logger::getInstance().warn(__VA_ARGS__)
#define LOG_ERROR(...) logging::Logger::getInstance().error(__VA_ARGS__)
#define LOG_CRITICAL(...) logging::Logger::getInstance().critical(__VA_ARGS__)
11 changes: 0 additions & 11 deletions liblogging/src/Global.cpp

This file was deleted.

5 changes: 5 additions & 0 deletions liblogging/src/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ Logger::Logger() : spdLogger("") {
loggerSetDefaults();
}

Logger& Logger::getInstance() {
static Logger instance;
return instance;
}

void Logger::setLevel(enum LogLevel level) {
spdLogger.set_level(static_cast<spdlog::level::level_enum>(level));
}
Expand Down

0 comments on commit c15ac42

Please sign in to comment.