SHA256
modify something
This commit is contained in:
+3
-2
@@ -18,8 +18,9 @@ set(${PROJECT_NAME}_SOURCES
|
||||
include/featherlog/log_message.hpp
|
||||
include/featherlog/flog_definitions.h
|
||||
include/featherlog/push_result.hpp
|
||||
src/log_manager.cpp
|
||||
src/log_outputter.cpp)
|
||||
include/featherlog/log_message_flag.hpp
|
||||
include/featherlog/featherlog.hpp
|
||||
src/log_manager.cpp)
|
||||
|
||||
# 生成静态库
|
||||
add_library(${PROJECT_NAME}_static STATIC ${${PROJECT_NAME}_SOURCES})
|
||||
|
||||
@@ -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,8 +4,10 @@
|
||||
#include <string>
|
||||
#include <featherlog/flog_definitions.h>
|
||||
|
||||
namespace flog {
|
||||
class FLOG_API LogFormatter {
|
||||
namespace flog
|
||||
{
|
||||
class FLOG_API LogFormatter
|
||||
{
|
||||
public:
|
||||
virtual ~LogFormatter() = default;
|
||||
|
||||
|
||||
@@ -3,9 +3,11 @@
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace flog {
|
||||
namespace flog
|
||||
{
|
||||
using LogLevelType = uint8_t;
|
||||
enum class LogLevel : LogLevelType {
|
||||
enum class LogLevel : LogLevelType
|
||||
{
|
||||
TRACE = 0,
|
||||
DEBUG = 1,
|
||||
INFO = 2,
|
||||
|
||||
@@ -14,10 +14,10 @@
|
||||
#include <featherlog/flog_definitions.h>
|
||||
#include <featherlog/push_result.hpp>
|
||||
|
||||
namespace flog {
|
||||
class FLOG_API LogManager : public std::enable_shared_from_this<LogManager> {
|
||||
friend class LoggerCallback;
|
||||
|
||||
namespace flog
|
||||
{
|
||||
class FLOG_API LogManager : public std::enable_shared_from_this<LogManager>
|
||||
{
|
||||
public:
|
||||
LogManager(const LogManager &) = delete;
|
||||
LogManager &operator=(const LogManager &) = delete;
|
||||
|
||||
@@ -6,13 +6,17 @@
|
||||
#include <string_view>
|
||||
|
||||
#include <featherlog/log_level.hpp>
|
||||
#include <featherlog/log_message_flag.hpp>
|
||||
|
||||
namespace flog {
|
||||
struct LogMessage {
|
||||
namespace flog
|
||||
{
|
||||
struct LogMessage
|
||||
{
|
||||
std::chrono::system_clock::time_point timestamp;
|
||||
LogLevel level;
|
||||
std::string_view logger_name;
|
||||
std::string text;
|
||||
LogLevel level;
|
||||
MessageFlag flag;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -23,7 +23,7 @@ namespace flog {
|
||||
virtual void FLOG_CALL write_log(const LogMessage &message) = 0;
|
||||
|
||||
protected:
|
||||
std::string m_name;
|
||||
const std::string m_name;
|
||||
|
||||
std::unique_ptr<LogFormatter> m_logFormatter;
|
||||
};
|
||||
|
||||
@@ -7,10 +7,12 @@
|
||||
|
||||
#include <featherlog/flog_definitions.h>
|
||||
|
||||
namespace flog {
|
||||
class LoggerManager;
|
||||
namespace flog
|
||||
{
|
||||
class LogManager;
|
||||
|
||||
class FLOG_API Logger {
|
||||
class FLOG_API Logger final
|
||||
{
|
||||
friend class LogManager;
|
||||
|
||||
public:
|
||||
@@ -25,8 +27,8 @@ namespace flog {
|
||||
m_logManager(manager)
|
||||
{ }
|
||||
|
||||
std::string m_name;
|
||||
std::weak_ptr<LogManager> m_logManager;
|
||||
const std::string m_name;
|
||||
const std::weak_ptr<LogManager> m_logManager;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -4,37 +4,37 @@
|
||||
#include <optional>
|
||||
#include <functional>
|
||||
|
||||
namespace flog {
|
||||
namespace flog
|
||||
{
|
||||
template <typename T>
|
||||
class PushResult {
|
||||
class PushResult
|
||||
{
|
||||
public:
|
||||
_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))));
|
||||
}
|
||||
|
||||
_NODISCARD bool isSuccessful() 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()) {
|
||||
func(m_value.value());
|
||||
}
|
||||
}
|
||||
|
||||
_NODISCARD T getValue() const {
|
||||
return m_value.value();
|
||||
}
|
||||
_NODISCARD T getValue() const { return m_value.value(); }
|
||||
|
||||
PushResult(const PushResult &other) noexcept {
|
||||
m_value = other.m_value;
|
||||
}
|
||||
PushResult(const PushResult &other) noexcept { m_value = other.m_value; }
|
||||
|
||||
private:
|
||||
PushResult() noexcept = default;
|
||||
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
@@ -2,13 +2,16 @@
|
||||
#include <featherlog/log_manager.hpp>
|
||||
#include <featherlog/push_result.hpp>
|
||||
|
||||
namespace flog {
|
||||
std::shared_ptr<LogManager> LogManager::create() {
|
||||
namespace flog
|
||||
{
|
||||
std::shared_ptr<LogManager> LogManager::create()
|
||||
{
|
||||
// ReSharper disable once CppDFAMemoryLeak
|
||||
return std::shared_ptr<LogManager>(new LogManager());
|
||||
}
|
||||
|
||||
LogManager::~LogManager() {
|
||||
LogManager::~LogManager()
|
||||
{
|
||||
{
|
||||
std::lock_guard lock(m_loggersMutex);
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
PushResult<std::unique_ptr<LogOutputter>> LogManager::add_outputter(std::unique_ptr<LogOutputter> outputter)
|
||||
{
|
||||
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));
|
||||
}
|
||||
m_logOutputters.emplace(outputter->getName(), std::move(outputter));
|
||||
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);
|
||||
return m_logOutputters.contains(name.data());
|
||||
}
|
||||
|
||||
std::optional<std::unique_ptr<LogOutputter>> LogManager::try_delete_outputter(const std::string_view name) noexcept {
|
||||
if (contains_outputter(name)) {
|
||||
std::optional<std::unique_ptr<LogOutputter>> LogManager::try_delete_outputter(const std::string_view name) noexcept
|
||||
{
|
||||
if (contains_outputter(name))
|
||||
{
|
||||
std::lock_guard lock(m_logOutputtersMutex);
|
||||
const auto it = m_logOutputters.find(name.data());
|
||||
std::optional outputter = std::move(it->second);
|
||||
@@ -53,9 +62,11 @@ namespace flog {
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -64,23 +75,30 @@ namespace flog {
|
||||
return logger;
|
||||
}
|
||||
|
||||
size_t LogManager::clean_unused_loggers() {
|
||||
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) {
|
||||
for (auto it = m_loggers.begin(); it != m_loggers.end();)
|
||||
{
|
||||
if (it->second.use_count() == 1)
|
||||
{
|
||||
it = m_loggers.erase(it);
|
||||
count += 1;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
++it;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void LogManager::write_log(const LogMessage &message) const {
|
||||
void LogManager::write_log(const LogMessage &message) const
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user