This commit is contained in:
CupWater
2026-08-17 09:28:06 +08:00
parent ee4fa58544
commit d54d2fcff5
9 changed files with 321 additions and 1 deletions
+15
View File
@@ -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
+21
View File
@@ -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
+103
View File
@@ -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;
}
}
+59
View File
@@ -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
+20
View File
@@ -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
+27
View File
@@ -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
+33
View File
@@ -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