This commit is contained in:
CupWater
2026-08-17 11:29:05 +08:00
parent c196ffb6d8
commit 835c556a58
9 changed files with 43 additions and 9 deletions
+30
View File
@@ -0,0 +1,30 @@
#ifndef FEATHERLOG_FLOG_DEFINITIONS_H
#define FEATHERLOG_FLOG_DEFINITIONS_H
#ifndef FLOG_CALL
# define FLOG_CALL __stdcall
#endif
#ifndef FLOG_API
#if defined(__unix) || defined(__unix__)
#define FLOG_API
#elif defined(_WIN32) || defined(__WIN32__)
# ifdef FLOG_DLL_MODE
# ifdef FLOG_BUILDING_DLL
# define FLOG_API __declspec(dllexport)
# else
# define FLOG_API __declspec(dllimport)
# endif
# else
# define FLOG_API
# endif
#else
# error Unknow platform!
#endif
#endif
#endif
+18
View File
@@ -0,0 +1,18 @@
#ifndef FEATHERLOG_LOG_FORMATTER_HPP
#define FEATHERLOG_LOG_FORMATTER_HPP
#include <string>
#include
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
+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
+26
View File
@@ -0,0 +1,26 @@
#ifndef FEATHERLOG_LOG_OUTPUTTER_HPP
#define FEATHERLOG_LOG_OUTPUTTER_HPP
#include <memory>
#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