SHA256
.
This commit is contained in:
@@ -108,6 +108,8 @@ CMakeUserPresets.json
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
.idea/
|
||||
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
@@ -732,6 +734,7 @@ $RECYCLE.BIN/
|
||||
# Icon must end with two \r
|
||||
Icon
|
||||
|
||||
|
||||
# Thumbnails
|
||||
._*
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
cmake_minimum_required(VERSION 4.0.2)
|
||||
project(FeatherLog)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
|
||||
if (MSVC)
|
||||
add_compile_options(/Wall /utf-8)
|
||||
else ()
|
||||
add_compile_options(-Wextra -Wpedantic -Wall)
|
||||
endif ()
|
||||
|
||||
set(CMAKE_BINARY_DIR "out")
|
||||
|
||||
set(sources
|
||||
src/featherlog/log_manager.hpp
|
||||
src/featherlog/log_outputter.hpp
|
||||
src/featherlog/logger.hpp
|
||||
src/featherlog/log_formatter.hpp
|
||||
src/featherlog/log_level.hpp
|
||||
src/featherlog/log_message.hpp
|
||||
src/featherlog/log_manager.cpp)
|
||||
|
||||
# Shared Lib:
|
||||
if (MSVC)
|
||||
add_compile_options(/DFEATHER_LOG_EXPORT_DLL)
|
||||
else ()
|
||||
add_compile_options(-DFEATHER_LOG_EXPORT_DLL)
|
||||
endif ()
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/shared")
|
||||
add_library(${PROJECT_NAME} SHARED ${sources})
|
||||
|
||||
# Shared Lib End
|
||||
|
||||
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/static")
|
||||
#add_library(${PROJECT_NAME} STATIC ${sources})
|
||||
|
||||
# Static Lib:
|
||||
# Static Lib End
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef FEATHERLOG_LOG_FORMATTER_HPP
|
||||
#define FEATHERLOG_LOG_FORMATTER_HPP
|
||||
#include <string>
|
||||
|
||||
namespace flog
|
||||
{
|
||||
class LogFormatter
|
||||
{
|
||||
public:
|
||||
virtual ~LogFormatter() = default;
|
||||
virtual std::string format() = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef FEATHERLOG_LOG_LEVEL_HPP
|
||||
#define FEATHERLOG_LOG_LEVEL_HPP
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace flog
|
||||
{
|
||||
enum class LogLevel : uint8_t
|
||||
{
|
||||
TRACE = 0,
|
||||
DEBUG = 1,
|
||||
INFO = 2,
|
||||
WARN = 3,
|
||||
ERROR = 4,
|
||||
FATAL = 5,
|
||||
|
||||
OFF = 0xff,
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,103 @@
|
||||
#include "log_manager.hpp"
|
||||
|
||||
namespace flog
|
||||
{
|
||||
|
||||
|
||||
inline std::shared_ptr<LogManager> LogManager::create()
|
||||
{
|
||||
// ReSharper disable once CppDFAMemoryLeak
|
||||
return std::shared_ptr<LogManager>(new LogManager());
|
||||
}
|
||||
|
||||
inline LogManager::~LogManager()
|
||||
{
|
||||
{
|
||||
std::lock_guard lock(m_loggersMutex);
|
||||
m_loggers.clear();
|
||||
}
|
||||
{
|
||||
std::lock_guard lock(m_logOutputtersMutex);
|
||||
m_logOutputters.clear();
|
||||
}
|
||||
}
|
||||
|
||||
inline LogLevel LogManager::get_min_log_level() const noexcept
|
||||
{
|
||||
return m_minLogLevel.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
inline void LogManager::set_min_log_level(const LogLevel level) noexcept
|
||||
{
|
||||
m_minLogLevel.store(level, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
inline void LogManager::add_outputter(std::unique_ptr<LogOutputter> outputter)
|
||||
{
|
||||
std::lock_guard lock(m_logOutputtersMutex);
|
||||
m_logOutputters.push_back(std::move(outputter));
|
||||
}
|
||||
|
||||
inline std::shared_ptr<Logger> LogManager::get_logger(const std::string& name)
|
||||
{
|
||||
std::lock_guard lock(m_loggersMutex);
|
||||
if (const auto it = m_loggers.find(name); it != m_loggers.end())
|
||||
{
|
||||
return it->second;
|
||||
}
|
||||
|
||||
const auto logger = std::shared_ptr<Logger>(new Logger(name, weak_from_this()));
|
||||
m_loggers.emplace(name, logger);
|
||||
return logger;
|
||||
}
|
||||
|
||||
inline size_t LogManager::clean_unused_loggers()
|
||||
{
|
||||
std::lock_guard lock(m_loggersMutex);
|
||||
size_t count = 0;
|
||||
for (auto it = m_loggers.begin(); it != m_loggers.end();)
|
||||
{
|
||||
if (it->second.use_count() == 1)
|
||||
{
|
||||
it = m_loggers.erase(it);
|
||||
count += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
++it;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
inline void LogManager::print_log(const LogMessage& message) const
|
||||
{
|
||||
std::shared_lock lock(m_logOutputtersMutex);
|
||||
for (auto& outputter : m_logOutputters)
|
||||
{
|
||||
outputter->write(message);
|
||||
}
|
||||
}
|
||||
|
||||
inline LoggerCallback::LoggerCallback(const std::weak_ptr<LogManager>& logManager) noexcept : m_logManager(
|
||||
logManager)
|
||||
{
|
||||
}
|
||||
|
||||
inline void LoggerCallback::print_log(const LogMessage& message) const
|
||||
{
|
||||
if (const auto mgr = m_logManager.lock())
|
||||
{
|
||||
mgr->print_log(message);
|
||||
}
|
||||
}
|
||||
|
||||
inline LogLevel LoggerCallback::get_min_log_level() const noexcept
|
||||
{
|
||||
if (const auto mgr = m_logManager.lock())
|
||||
{
|
||||
return mgr->get_min_log_level();
|
||||
}
|
||||
return LogLevel::OFF;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#ifndef FEATHERLOG_LOG_MANAGER_HPP
|
||||
#define FEATHERLOG_LOG_MANAGER_HPP
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <shared_mutex>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "logger.hpp"
|
||||
#include "log_formatter.hpp"
|
||||
#include "log_level.hpp"
|
||||
#include "log_message.hpp"
|
||||
#include "log_outputter.hpp"
|
||||
|
||||
namespace flog
|
||||
{
|
||||
class LogManager;
|
||||
|
||||
class LogManager : public std::enable_shared_from_this<LogManager>
|
||||
{
|
||||
friend class LoggerCallback;
|
||||
|
||||
public:
|
||||
LogManager(const LogManager&) = delete;
|
||||
LogManager& operator=(const LogManager&) = delete;
|
||||
LogManager(LogManager&&) = delete;
|
||||
LogManager& operator=(LogManager&&) = delete;
|
||||
|
||||
static std::shared_ptr<LogManager> create();
|
||||
|
||||
~LogManager();
|
||||
|
||||
LogLevel get_min_log_level() const noexcept;
|
||||
void set_min_log_level(LogLevel level) noexcept;
|
||||
|
||||
void add_outputter(std::unique_ptr<LogOutputter> outputter);
|
||||
|
||||
std::shared_ptr<Logger> get_logger(const std::string& name);
|
||||
|
||||
size_t clean_unused_loggers();
|
||||
|
||||
private:
|
||||
LogManager() noexcept = default;
|
||||
|
||||
void print_log(const LogMessage& message) const;
|
||||
|
||||
std::atomic<LogLevel> m_minLogLevel{LogLevel::DEBUG};
|
||||
|
||||
mutable std::shared_mutex m_logOutputtersMutex;
|
||||
std::vector<std::unique_ptr<LogOutputter>> m_logOutputters;
|
||||
|
||||
mutable std::mutex m_loggersMutex;
|
||||
std::unordered_map<std::string, std::shared_ptr<Logger>> m_loggers;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef FEATHERLOG_LOG_MESSAGE_HPP
|
||||
#define FEATHERLOG_LOG_MESSAGE_HPP
|
||||
|
||||
|
||||
#include <chrono>
|
||||
#include <string_view>
|
||||
|
||||
#include "log_level.hpp"
|
||||
|
||||
namespace flog
|
||||
{
|
||||
struct LogMessage {
|
||||
std::chrono::system_clock::time_point timestamp;
|
||||
LogLevel level;
|
||||
std::string_view logger_name;
|
||||
std::string text;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef FEATHERLOG_LOG_OUTPUTER_HPP
|
||||
#define FEATHERLOG_LOG_OUTPUTER_HPP
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "log_formatter.hpp"
|
||||
#include "log_message.hpp"
|
||||
|
||||
namespace flog
|
||||
{
|
||||
class LogOutputter
|
||||
{
|
||||
public:
|
||||
virtual ~LogOutputter() = default;
|
||||
|
||||
std::unique_ptr<LogFormatter> set_formatter(const std::unique_ptr<LogFormatter>& formatter);
|
||||
std::unique_ptr<LogFormatter>& get_formatter();
|
||||
|
||||
virtual void write(const LogMessage& message) = 0;
|
||||
|
||||
protected:
|
||||
std::unique_ptr<LogFormatter> m_logFormatter;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef FEATHERLOG_LOGGER_HPP
|
||||
#define FEATHERLOG_LOGGER_HPP
|
||||
|
||||
#include <format>
|
||||
#include <source_location>
|
||||
|
||||
#include "log_manager.hpp"
|
||||
|
||||
namespace flog
|
||||
{
|
||||
class LoggerCallback;
|
||||
|
||||
class Logger
|
||||
{
|
||||
friend class LogManager;
|
||||
|
||||
public:
|
||||
~Logger() = default;
|
||||
|
||||
template <typename... Args>
|
||||
void info(std::format_string<Args...> fmt, Args&&... args,
|
||||
std::source_location loc = std::source_location::current());
|
||||
|
||||
private:
|
||||
Logger(const std::string& name, std::weak_ptr<LogManager> manager) : m_name(name), m_logManager(manager)
|
||||
{ }
|
||||
|
||||
std::string m_name;
|
||||
std::weak_ptr<LogManager> m_logManager;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user