modify something

This commit is contained in:
CupWater
2026-08-17 21:01:29 +08:00
parent 3274bf9393
commit 4fc7ef37c2
12 changed files with 133 additions and 47 deletions
+3 -2
View File
@@ -18,8 +18,9 @@ set(${PROJECT_NAME}_SOURCES
include/featherlog/log_message.hpp include/featherlog/log_message.hpp
include/featherlog/flog_definitions.h include/featherlog/flog_definitions.h
include/featherlog/push_result.hpp include/featherlog/push_result.hpp
src/log_manager.cpp include/featherlog/log_message_flag.hpp
src/log_outputter.cpp) include/featherlog/featherlog.hpp
src/log_manager.cpp)
# 生成静态库 # 生成静态库
add_library(${PROJECT_NAME}_static STATIC ${${PROJECT_NAME}_SOURCES}) add_library(${PROJECT_NAME}_static STATIC ${${PROJECT_NAME}_SOURCES})
+14
View File
@@ -0,0 +1,14 @@
#ifndef FEATHERLOG_FEATHERLOG_HPP
#define FEATHERLOG_FEATHERLOG_HPP
#include <featherlog/flog_definitions.h>
#include <featherlog/log_formatter.hpp>
#include <featherlog/log_level.hpp>
#include <featherlog/log_manager.hpp>
#include <featherlog/log_message.hpp>
#include <featherlog/log_message_flag.hpp>
#include <featherlog/log_outputter.hpp>
#include <featherlog/logger.hpp>
#include <featherlog/push_result.hpp>
#endif
+4 -2
View File
@@ -4,8 +4,10 @@
#include <string> #include <string>
#include <featherlog/flog_definitions.h> #include <featherlog/flog_definitions.h>
namespace flog { namespace flog
class FLOG_API LogFormatter { {
class FLOG_API LogFormatter
{
public: public:
virtual ~LogFormatter() = default; virtual ~LogFormatter() = default;
+4 -2
View File
@@ -3,9 +3,11 @@
#include <cstdint> #include <cstdint>
namespace flog { namespace flog
{
using LogLevelType = uint8_t; using LogLevelType = uint8_t;
enum class LogLevel : LogLevelType { enum class LogLevel : LogLevelType
{
TRACE = 0, TRACE = 0,
DEBUG = 1, DEBUG = 1,
INFO = 2, INFO = 2,
+4 -4
View File
@@ -14,10 +14,10 @@
#include <featherlog/flog_definitions.h> #include <featherlog/flog_definitions.h>
#include <featherlog/push_result.hpp> #include <featherlog/push_result.hpp>
namespace flog { namespace flog
class FLOG_API LogManager : public std::enable_shared_from_this<LogManager> { {
friend class LoggerCallback; class FLOG_API LogManager : public std::enable_shared_from_this<LogManager>
{
public: public:
LogManager(const LogManager &) = delete; LogManager(const LogManager &) = delete;
LogManager &operator=(const LogManager &) = delete; LogManager &operator=(const LogManager &) = delete;
+7 -3
View File
@@ -6,13 +6,17 @@
#include <string_view> #include <string_view>
#include <featherlog/log_level.hpp> #include <featherlog/log_level.hpp>
#include <featherlog/log_message_flag.hpp>
namespace flog { namespace flog
struct LogMessage { {
struct LogMessage
{
std::chrono::system_clock::time_point timestamp; std::chrono::system_clock::time_point timestamp;
LogLevel level;
std::string_view logger_name; std::string_view logger_name;
std::string text; std::string text;
LogLevel level;
MessageFlag flag;
}; };
} }
+32
View File
@@ -0,0 +1,32 @@
#ifndef FEATHERLOG_LOG_MESSAGE_FLAG_HPP
#define FEATHERLOG_LOG_MESSAGE_FLAG_HPP
#include <array>
namespace flog
{
typedef uint32_t MessageFlag;
class MessageFlagManager
{
public:
MessageFlagManager() noexcept = default;
~MessageFlagManager() = default;
MessageFlagManager(const MessageFlagManager&) noexcept = default;
MessageFlagManager& operator=(const MessageFlagManager&) noexcept = default;
MessageFlagManager(MessageFlagManager&&) noexcept = default;
MessageFlagManager& operator=(MessageFlagManager&&) noexcept = default;
_NODISCARD bool isFull() const noexcept { return flagCount >= std::numeric_limits<MessageFlag>::digits; }
_NODISCARD bool pushable() const noexcept { return !isFull(); }
_NODISCARD bool isEmpty() const noexcept { return flagCount == 0; }
// _NODISCARD
private:
std::array<std::string, std::numeric_limits<MessageFlag>::digits> m_flagMap{};
size_t flagCount { 0 };
};
}
#endif
+1 -1
View File
@@ -23,7 +23,7 @@ namespace flog {
virtual void FLOG_CALL write_log(const LogMessage &message) = 0; virtual void FLOG_CALL write_log(const LogMessage &message) = 0;
protected: protected:
std::string m_name; const std::string m_name;
std::unique_ptr<LogFormatter> m_logFormatter; std::unique_ptr<LogFormatter> m_logFormatter;
}; };
+7 -5
View File
@@ -7,10 +7,12 @@
#include <featherlog/flog_definitions.h> #include <featherlog/flog_definitions.h>
namespace flog { namespace flog
class LoggerManager; {
class LogManager;
class FLOG_API Logger { class FLOG_API Logger final
{
friend class LogManager; friend class LogManager;
public: public:
@@ -25,8 +27,8 @@ namespace flog {
m_logManager(manager) m_logManager(manager)
{ } { }
std::string m_name; const std::string m_name;
std::weak_ptr<LogManager> m_logManager; const std::weak_ptr<LogManager> m_logManager;
}; };
} }
+11 -11
View File
@@ -4,37 +4,37 @@
#include <optional> #include <optional>
#include <functional> #include <functional>
namespace flog { namespace flog
{
template <typename T> template <typename T>
class PushResult { class PushResult
{
public: public:
_NODISCARD static PushResult success() noexcept { return PushResult(); } _NODISCARD static PushResult success() noexcept { return PushResult(); }
_NODISCARD static PushResult failWithValue(T value) noexcept { _NODISCARD static PushResult failWithValue(T value) noexcept
{
return PushResult(std::move(std::optional<T>(std::move(value)))); return PushResult(std::move(std::optional<T>(std::move(value))));
} }
_NODISCARD bool isSuccessful() const noexcept { return !m_value.has_value(); } _NODISCARD bool isSuccessful() const noexcept { return !m_value.has_value(); }
_NODISCARD bool isFail() const noexcept { return m_value.has_value(); } _NODISCARD bool isFail() const noexcept { return m_value.has_value(); }
void getValueIfFail(const std::function<void(const T& value)> func) const { void getValueIfFail(const std::function<void(const T& value)> func) const
{
if (isFail()) { if (isFail()) {
func(m_value.value()); func(m_value.value());
} }
} }
_NODISCARD T getValue() const { _NODISCARD T getValue() const { return m_value.value(); }
return m_value.value();
}
PushResult(const PushResult &other) noexcept { PushResult(const PushResult &other) noexcept { m_value = other.m_value; }
m_value = other.m_value;
}
private: private:
PushResult() noexcept = default; PushResult() noexcept = default;
explicit PushResult(std::optional<T> value) noexcept : m_value(std::move(value)) {} explicit PushResult(std::optional<T> value) noexcept : m_value(std::move(value)) {}
std::optional<T> m_value = std::nullopt; const std::optional<T> m_value = std::nullopt;
}; };
} }
+35 -17
View File
@@ -2,13 +2,16 @@
#include <featherlog/log_manager.hpp> #include <featherlog/log_manager.hpp>
#include <featherlog/push_result.hpp> #include <featherlog/push_result.hpp>
namespace flog { namespace flog
std::shared_ptr<LogManager> LogManager::create() { {
std::shared_ptr<LogManager> LogManager::create()
{
// ReSharper disable once CppDFAMemoryLeak // ReSharper disable once CppDFAMemoryLeak
return std::shared_ptr<LogManager>(new LogManager()); return std::shared_ptr<LogManager>(new LogManager());
} }
LogManager::~LogManager() { LogManager::~LogManager()
{
{ {
std::lock_guard lock(m_loggersMutex); std::lock_guard lock(m_loggersMutex);
m_loggers.clear(); m_loggers.clear();
@@ -19,31 +22,37 @@ namespace flog {
} }
} }
LogLevel LogManager::get_min_log_level() const noexcept { LogLevel LogManager::get_min_log_level() const noexcept
{
return m_minLogLevel.load(std::memory_order_relaxed); return m_minLogLevel.load(std::memory_order_relaxed);
} }
void LogManager::set_min_log_level(const LogLevel level) noexcept { void LogManager::set_min_log_level(const LogLevel level) noexcept
{
m_minLogLevel.store(level, std::memory_order_relaxed); m_minLogLevel.store(level, std::memory_order_relaxed);
} }
PushResult<std::unique_ptr<LogOutputter>> LogManager::add_outputter(std::unique_ptr<LogOutputter> outputter) PushResult<std::unique_ptr<LogOutputter>> LogManager::add_outputter(std::unique_ptr<LogOutputter> outputter)
{ {
std::lock_guard lock(m_logOutputtersMutex); std::lock_guard lock(m_logOutputtersMutex);
if (m_logOutputters.contains(outputter->getName())) { if (m_logOutputters.contains(outputter->getName()))
{
return PushResult<std::unique_ptr<LogOutputter>>::failWithValue(std::move(outputter)); return PushResult<std::unique_ptr<LogOutputter>>::failWithValue(std::move(outputter));
} }
m_logOutputters.emplace(outputter->getName(), std::move(outputter)); m_logOutputters.emplace(outputter->getName(), std::move(outputter));
return PushResult<std::unique_ptr<LogOutputter>>::success(); return PushResult<std::unique_ptr<LogOutputter>>::success();
} }
bool LogManager::contains_outputter(const std::string_view name) const noexcept { bool LogManager::contains_outputter(const std::string_view name) const noexcept
{
std::shared_lock lock(m_logOutputtersMutex); std::shared_lock lock(m_logOutputtersMutex);
return m_logOutputters.contains(name.data()); return m_logOutputters.contains(name.data());
} }
std::optional<std::unique_ptr<LogOutputter>> LogManager::try_delete_outputter(const std::string_view name) noexcept { std::optional<std::unique_ptr<LogOutputter>> LogManager::try_delete_outputter(const std::string_view name) noexcept
if (contains_outputter(name)) { {
if (contains_outputter(name))
{
std::lock_guard lock(m_logOutputtersMutex); std::lock_guard lock(m_logOutputtersMutex);
const auto it = m_logOutputters.find(name.data()); const auto it = m_logOutputters.find(name.data());
std::optional outputter = std::move(it->second); std::optional outputter = std::move(it->second);
@@ -53,9 +62,11 @@ namespace flog {
return std::nullopt; return std::nullopt;
} }
std::shared_ptr<Logger> LogManager::get_logger(const std::string &name) { std::shared_ptr<Logger> LogManager::get_logger(const std::string &name)
{
std::lock_guard lock(m_loggersMutex); std::lock_guard lock(m_loggersMutex);
if (const auto it = m_loggers.find(name); it != m_loggers.end()) { if (const auto it = m_loggers.find(name); it != m_loggers.end())
{
return it->second; return it->second;
} }
@@ -64,23 +75,30 @@ namespace flog {
return logger; return logger;
} }
size_t LogManager::clean_unused_loggers() { size_t LogManager::clean_unused_loggers()
{
std::lock_guard lock(m_loggersMutex); std::lock_guard lock(m_loggersMutex);
size_t count = 0; size_t count = 0;
for (auto it = m_loggers.begin(); it != m_loggers.end();) { for (auto it = m_loggers.begin(); it != m_loggers.end();)
if (it->second.use_count() == 1) { {
if (it->second.use_count() == 1)
{
it = m_loggers.erase(it); it = m_loggers.erase(it);
count += 1; count += 1;
} else { }
else
{
++it; ++it;
} }
} }
return count; return count;
} }
void LogManager::write_log(const LogMessage &message) const { void LogManager::write_log(const LogMessage &message) const
{
std::shared_lock lock(m_logOutputtersMutex); std::shared_lock lock(m_logOutputtersMutex);
for (const std::unique_ptr<LogOutputter> &outputter: m_logOutputters | std::views::values) { for (const std::unique_ptr<LogOutputter> &outputter: m_logOutputters | std::views::values)
{
outputter->write_log(message); outputter->write_log(message);
} }
} }
+11
View File
@@ -0,0 +1,11 @@
#include <featherlog/featherlog.hpp>
int main() {
auto manager = flog::LogManager::create();
// std::unique_ptr<flog::LogFormatter> formatter =...
manager->add_outputter(/* outputter("name", formatter) */);
auto /*std::shared_ptr<flog::Logger>*/ logger = manager->get_logger("name");
logger->info("fmt", ...);
return 0;
}