SHA256
Compare commits
15
Commits
7eb3331d8e
...
main
| Author | SHA256 | Date | |
|---|---|---|---|
|
|
c3cfc635ee | ||
|
|
27014f192c | ||
|
|
d65184f678 | ||
|
|
267d7953ba | ||
|
|
c7d4b87d3e | ||
|
|
693db85e3b | ||
|
|
4fc7ef37c2 | ||
|
|
3274bf9393 | ||
|
|
999f704b67 | ||
|
|
2085e9f7a5 | ||
|
|
1313cab93e | ||
|
|
e4d834e4a9 | ||
|
|
b2aa61dc09 | ||
|
|
17d63c62c4 | ||
|
|
99404f62d0 |
+12
-6
@@ -4,9 +4,9 @@ project(FeatherLog)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
|
||||
set(CMAKE_BINARY_DIR "out")
|
||||
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/shared")
|
||||
add_compile_definitions(FLOG_IMPL_CONSOLE_OUTPUTTER) # build with default console outputter
|
||||
add_compile_definitions(FLOG_IMPL_FILE_OUTPUTTER) # build with default file outputter
|
||||
add_compile_definitions(FLOG_IMPL_DEFAULT_FORMATTER) # build with default formatter
|
||||
|
||||
# 定义源文件
|
||||
set(${PROJECT_NAME}_SOURCES
|
||||
@@ -16,7 +16,14 @@ set(${PROJECT_NAME}_SOURCES
|
||||
include/featherlog/log_formatter.hpp
|
||||
include/featherlog/log_level.hpp
|
||||
include/featherlog/log_message.hpp
|
||||
src/log_manager.cpp)
|
||||
include/featherlog/flog_definitions.h
|
||||
include/featherlog/push_result.hpp
|
||||
include/featherlog/log_message_flag.hpp
|
||||
include/featherlog/featherlog.hpp
|
||||
src/log_manager.cpp
|
||||
src/log_message_flag.cpp
|
||||
include/featherlog/impl/flog_impl.hpp
|
||||
include/featherlog/impl/console_outputter.hpp)
|
||||
|
||||
# 生成静态库
|
||||
add_library(${PROJECT_NAME}_static STATIC ${${PROJECT_NAME}_SOURCES})
|
||||
@@ -29,8 +36,7 @@ add_compile_definitions(FLOG_DLL_MODE)
|
||||
add_compile_definitions(FLOG_BUILDING_DLL)
|
||||
|
||||
# 生成动态库
|
||||
add_library(${PROJECT_NAME}_shared SHARED ${${PROJECT_NAME}_SOURCES}
|
||||
include/featherlog/flog_definitions.h)
|
||||
add_library(${PROJECT_NAME}_shared SHARED ${${PROJECT_NAME}_SOURCES})
|
||||
target_include_directories(${PROJECT_NAME}_shared PUBLIC include)
|
||||
set_target_properties(${PROJECT_NAME}_shared PROPERTIES
|
||||
OUTPUT_NAME ${PROJECT_NAME}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef FEATHERLOG_FEATHERLOG_HPP
|
||||
#define FEATHERLOG_FEATHERLOG_HPP
|
||||
|
||||
#include <featherlog/flog_definitions.h>
|
||||
#include <featherlog/logger.hpp>
|
||||
#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/push_result.hpp>
|
||||
|
||||
#include <featherlog/impl/flog_impl.hpp>
|
||||
|
||||
#endif
|
||||
@@ -27,4 +27,12 @@
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef NODISCARD
|
||||
# ifdef _NODISCARD
|
||||
# define NODISCARD _NODISCARD
|
||||
# else
|
||||
# define NODISCARD [[nodiscard]]
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef FLOG_IMPL_CONSOLE_OUTPUTTER
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,7 @@
|
||||
#ifndef FEATHERLOG_IMPL_FLOG_IMPL_HPP
|
||||
#define FEATHERLOG_IMPL_FLOG_IMPL_HPP
|
||||
|
||||
#include <featherlog/impl/console_outputter.hpp>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
#ifndef FEATHERLOG_LOG_FORMATTER_HPP
|
||||
#define FEATHERLOG_LOG_FORMATTER_HPP
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <featherlog/flog_definitions.h>
|
||||
|
||||
namespace flog {
|
||||
class FLOG_API LogFormatter {
|
||||
namespace flog
|
||||
{
|
||||
class FLOG_API LogFormatter
|
||||
{
|
||||
public:
|
||||
virtual ~LogFormatter() = default;
|
||||
|
||||
virtual std::string FLOG_CALL format() = 0;
|
||||
NODISCARD virtual std::string FLOG_CALL format(const LogMessage& message) = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#ifndef FEATHERLOG_LOG_LEVEL_HPP
|
||||
#define FEATHERLOG_LOG_LEVEL_HPP
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace flog {
|
||||
namespace flog
|
||||
{
|
||||
using LogLevelType = uint8_t;
|
||||
enum class LogLevel : LogLevelType {
|
||||
enum class LogLevel : LogLevelType
|
||||
{
|
||||
TRACE = 0,
|
||||
DEBUG = 1,
|
||||
INFO = 2,
|
||||
@@ -16,5 +17,3 @@ namespace flog {
|
||||
OFF = std::numeric_limits<LogLevelType>::max()
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,55 +1,64 @@
|
||||
#ifndef FEATHERLOG_LOG_MANAGER_HPP
|
||||
#define FEATHERLOG_LOG_MANAGER_HPP
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <shared_mutex>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include <featherlog/logger.hpp>
|
||||
#include <featherlog/log_level.hpp>
|
||||
#include <featherlog/log_message.hpp>
|
||||
#include <featherlog/log_outputter.hpp>
|
||||
#include <featherlog/flog_definitions.h>
|
||||
#include <featherlog/push_result.hpp>
|
||||
#include <utility>
|
||||
|
||||
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;
|
||||
LogManager(LogManager &&) = delete;
|
||||
LogManager &operator=(LogManager &&) = delete;
|
||||
|
||||
static std::shared_ptr<LogManager> FLOG_CALL create();
|
||||
NODISCARD static std::shared_ptr<LogManager> FLOG_CALL create() noexcept { return {}; }
|
||||
NODISCARD static std::shared_ptr<LogManager> FLOG_CALL create(LogMessageFlagManager flagManager);
|
||||
|
||||
~LogManager();
|
||||
|
||||
LogLevel FLOG_CALL get_min_log_level() const noexcept;
|
||||
void FLOG_CALL set_min_log_level(LogLevel level) noexcept;
|
||||
NODISCARD LogLevel FLOG_CALL get_min_log_level() const noexcept;
|
||||
void FLOG_CALL set_min_log_level(LogLevel level) noexcept;
|
||||
|
||||
void FLOG_CALL add_outputter(std::unique_ptr<LogOutputter> outputter);
|
||||
NODISCARD PushResult<std::unique_ptr<LogOutputter>> FLOG_CALL
|
||||
add_outputter(std::unique_ptr<LogOutputter> outputter);
|
||||
NODISCARD bool FLOG_CALL contains_outputter(std::string_view name) const;
|
||||
std::optional<std::unique_ptr<LogOutputter>> FLOG_CALL try_delete_outputter(std::string_view name);
|
||||
|
||||
std::shared_ptr<Logger> FLOG_CALL get_logger(const std::string &name);
|
||||
NODISCARD std::shared_ptr<Logger> FLOG_CALL get_logger(const std::string &name);
|
||||
|
||||
size_t FLOG_CALL clean_unused_loggers();
|
||||
|
||||
private:
|
||||
LogManager() noexcept = default;
|
||||
|
||||
void FLOG_CALL print_log(const LogMessage &message) const;
|
||||
explicit LogManager(LogMessageFlagManager flagManager) noexcept :
|
||||
m_logMessageFlagManager(std::move(flagManager))
|
||||
{ }
|
||||
|
||||
void FLOG_CALL write_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;
|
||||
std::unordered_map<std::string, std::unique_ptr<LogOutputter>> m_logOutputters;
|
||||
|
||||
mutable std::mutex m_loggersMutex;
|
||||
std::unordered_map<std::string, std::shared_ptr<Logger>> m_loggers;
|
||||
|
||||
mutable std::shared_mutex m_logMessageFlagManagerMutex;
|
||||
LogMessageFlagManager m_logMessageFlagManager;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
#ifndef FEATHERLOG_LOG_MESSAGE_HPP
|
||||
#define FEATHERLOG_LOG_MESSAGE_HPP
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <string_view>
|
||||
|
||||
#include "log_level.hpp"
|
||||
#include <featherlog/log_level.hpp>
|
||||
#include <featherlog/log_message_flag.hpp>
|
||||
|
||||
namespace flog
|
||||
{
|
||||
struct LogMessage {
|
||||
struct LogMessage
|
||||
{
|
||||
std::chrono::system_clock::time_point timestamp;
|
||||
LogLevel level;
|
||||
std::string_view logger_name;
|
||||
std::string text;
|
||||
LogLevel level;
|
||||
MessageFlag flag;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <limits>
|
||||
|
||||
namespace flog
|
||||
{
|
||||
typedef uint32_t LogMessageFlag;
|
||||
typedef LogMessageFlag MessageFlag;
|
||||
|
||||
typedef class LogMessageFlagManager
|
||||
{
|
||||
public:
|
||||
static constexpr MessageFlag ADD_FAILED = 0;
|
||||
|
||||
static constexpr size_t FLAG_ARRAY_SIZE = std::numeric_limits<MessageFlag>::digits;
|
||||
|
||||
LogMessageFlagManager() noexcept = default;
|
||||
~LogMessageFlagManager() = default;
|
||||
|
||||
LogMessageFlagManager(const LogMessageFlagManager&) noexcept = default;
|
||||
LogMessageFlagManager& operator=(const LogMessageFlagManager&) noexcept = default;
|
||||
LogMessageFlagManager(LogMessageFlagManager&&) noexcept = default;
|
||||
LogMessageFlagManager& operator=(LogMessageFlagManager&&) noexcept = default;
|
||||
|
||||
NODISCARD bool isFull() const noexcept { return flagCount >= FLAG_ARRAY_SIZE; }
|
||||
NODISCARD bool pushable() const noexcept { return !isFull(); }
|
||||
NODISCARD bool isEmpty() const noexcept { return flagCount == 0; }
|
||||
NODISCARD bool empty() const noexcept { return isEmpty(); }
|
||||
|
||||
NODISCARD bool contains(std::string_view name) const noexcept;
|
||||
|
||||
NODISCARD MessageFlag addFlag (std::string_view name);
|
||||
NODISCARD MessageFlag findFlag(std::string_view name) const;
|
||||
|
||||
private:
|
||||
std::array<std::string, FLAG_ARRAY_SIZE> m_flagMap{};
|
||||
size_t flagCount { 0 };
|
||||
} MessageFlagManager;
|
||||
}
|
||||
@@ -1,26 +1,32 @@
|
||||
#ifndef FEATHERLOG_LOG_OUTPUTTER_HPP
|
||||
#define FEATHERLOG_LOG_OUTPUTTER_HPP
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "log_formatter.hpp"
|
||||
#include "log_message.hpp"
|
||||
#include <featherlog/log_formatter.hpp>
|
||||
#include <featherlog/log_message.hpp>
|
||||
#include <featherlog/flog_definitions.h>
|
||||
|
||||
namespace flog
|
||||
{
|
||||
class LogOutputter
|
||||
class FLOG_API LogOutputter
|
||||
{
|
||||
public:
|
||||
LogOutputter(std::string name, std::unique_ptr<LogFormatter> formatter) noexcept
|
||||
: m_name(std::move(name)), m_logFormatter(std::move(formatter))
|
||||
{ }
|
||||
|
||||
virtual ~LogOutputter() = default;
|
||||
|
||||
std::unique_ptr<LogFormatter> set_formatter(const std::unique_ptr<LogFormatter>& formatter);
|
||||
std::unique_ptr<LogFormatter>& get_formatter();
|
||||
NODISCARD std::string FLOG_CALL getName() const noexcept { return m_name; }
|
||||
|
||||
virtual void write(const LogMessage& message) = 0;
|
||||
std::unique_ptr<LogFormatter> FLOG_CALL set_formatter(const std::unique_ptr<LogFormatter> &formatter);
|
||||
NODISCARD const std::unique_ptr<LogFormatter> & FLOG_CALL get_formatter() const;
|
||||
|
||||
virtual void FLOG_CALL write_log(const LogMessage &message) = 0;
|
||||
|
||||
protected:
|
||||
const std::string m_name;
|
||||
|
||||
std::unique_ptr<LogFormatter> m_logFormatter;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,33 +1,37 @@
|
||||
#ifndef FEATHERLOG_LOGGER_HPP
|
||||
#define FEATHERLOG_LOGGER_HPP
|
||||
#pragma once
|
||||
|
||||
#include <format>
|
||||
#include <source_location>
|
||||
#include <utility>
|
||||
|
||||
#include "log_manager.hpp"
|
||||
#include <featherlog/flog_definitions.h>
|
||||
|
||||
namespace flog
|
||||
{
|
||||
class LoggerCallback;
|
||||
class LogManager;
|
||||
|
||||
class Logger
|
||||
class FLOG_API Logger final
|
||||
{
|
||||
friend class LogManager;
|
||||
|
||||
public:
|
||||
Logger(const Logger&) = delete;
|
||||
Logger operator=(const Logger&) = delete;
|
||||
Logger(const Logger&&) = delete;
|
||||
Logger operator=(const Logger&&) = delete;
|
||||
|
||||
~Logger() = default;
|
||||
|
||||
template <typename... Args>
|
||||
void info(std::format_string<Args...> fmt, Args&&... args,
|
||||
std::source_location loc = std::source_location::current());
|
||||
template<typename... Args>
|
||||
void FLOG_CALL 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)
|
||||
Logger(std::string name, const std::weak_ptr<LogManager> &manager) noexcept : m_name(std::move(name)),
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include <functional>
|
||||
|
||||
namespace flog
|
||||
{
|
||||
template <typename T>
|
||||
class PushResult
|
||||
{
|
||||
public:
|
||||
NODISCARD static PushResult success() noexcept { return PushResult(); }
|
||||
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
|
||||
{
|
||||
if (isFail()) {
|
||||
func(m_value.value());
|
||||
}
|
||||
}
|
||||
|
||||
NODISCARD T getValue() const { return m_value.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)) {}
|
||||
|
||||
const std::optional<T> m_value = std::nullopt;
|
||||
};
|
||||
}
|
||||
+38
-39
@@ -1,16 +1,13 @@
|
||||
#include "../include/featherlog/log_manager.hpp"
|
||||
#include <featherlog/log_manager.hpp>
|
||||
#include <featherlog/push_result.hpp>
|
||||
|
||||
namespace flog
|
||||
{
|
||||
|
||||
|
||||
inline std::shared_ptr<LogManager> LogManager::create()
|
||||
{
|
||||
// ReSharper disable once CppDFAMemoryLeak
|
||||
return std::shared_ptr<LogManager>(new LogManager());
|
||||
std::shared_ptr<LogManager> LogManager::create(MessageFlagManager flagManager) {
|
||||
return std::shared_ptr<LogManager>(new LogManager(std::move(flagManager)));
|
||||
}
|
||||
|
||||
inline LogManager::~LogManager()
|
||||
LogManager::~LogManager()
|
||||
{
|
||||
{
|
||||
std::lock_guard lock(m_loggersMutex);
|
||||
@@ -22,23 +19,47 @@ namespace flog
|
||||
}
|
||||
}
|
||||
|
||||
inline LogLevel LogManager::get_min_log_level() const noexcept
|
||||
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
|
||||
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)
|
||||
PushResult<std::unique_ptr<LogOutputter>> LogManager::add_outputter(std::unique_ptr<LogOutputter> outputter)
|
||||
{
|
||||
std::lock_guard lock(m_logOutputtersMutex);
|
||||
m_logOutputters.push_back(std::move(outputter));
|
||||
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();
|
||||
}
|
||||
|
||||
inline std::shared_ptr<Logger> LogManager::get_logger(const std::string& name)
|
||||
bool LogManager::contains_outputter(const std::string_view name) const
|
||||
{
|
||||
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)
|
||||
{
|
||||
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);
|
||||
m_logOutputters.erase(it);
|
||||
return std::move(outputter);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
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())
|
||||
@@ -51,7 +72,7 @@ namespace flog
|
||||
return logger;
|
||||
}
|
||||
|
||||
inline size_t LogManager::clean_unused_loggers()
|
||||
size_t LogManager::clean_unused_loggers()
|
||||
{
|
||||
std::lock_guard lock(m_loggersMutex);
|
||||
size_t count = 0;
|
||||
@@ -70,34 +91,12 @@ namespace flog
|
||||
return count;
|
||||
}
|
||||
|
||||
inline void LogManager::print_log(const LogMessage& message) const
|
||||
void LogManager::write_log(const LogMessage &message) const
|
||||
{
|
||||
std::shared_lock lock(m_logOutputtersMutex);
|
||||
for (auto& outputter : m_logOutputters)
|
||||
for (const std::unique_ptr<LogOutputter> &outputter: m_logOutputters | std::views::values)
|
||||
{
|
||||
outputter->write(message);
|
||||
outputter->write_log(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 @@
|
||||
#include <featherlog/log_message_flag.hpp>
|
||||
Reference in New Issue
Block a user