79 lines
2.5 KiB
C++
79 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include "../Base/global_include.h"
|
|
|
|
#include "spdlog/common.h"
|
|
#include "spdlog/logger.h"
|
|
|
|
#include <map>
|
|
#include <string>
|
|
|
|
#include <functional>
|
|
#include <vector>
|
|
|
|
void rotating_log(const std::string& fileName, const std::string& info, int size = 10 * 1024 * 1024, int num = 10);
|
|
|
|
// 自带换行符
|
|
void pure_log(const std::string& path, const std::string& info, bool close_after_write = false);
|
|
|
|
|
|
|
|
struct Log_Type {
|
|
Log_Type() = default;
|
|
explicit Log_Type(std::vector<std::string> list);
|
|
explicit Log_Type(std::vector<std::pair<std::string, std::string>> map);
|
|
Log_Type(std::vector<std::string> list, std::vector<std::pair<std::string, std::string>> map);
|
|
Log_Type& append(std::string key);
|
|
Log_Type& append(std::string key, std::string val);
|
|
[[nodiscard]] std::string to_string() const;
|
|
Log_Type& set(const std::string& key, std::string val);
|
|
bool new_line = false;
|
|
protected:
|
|
std::vector<std::string> list;
|
|
std::vector<std::pair<std::string, std::string>> map;
|
|
};
|
|
|
|
class BaseLogger {
|
|
public:
|
|
BaseLogger(const std::string& path, size_t byte_size, size_t piece_num);
|
|
void c_debug(const std::string& func_pattern, const Log_Type& log_type, const std::string& content);
|
|
void debug(const std::string& func_pattern, const Log_Type& log_type, const std::string& content);
|
|
void error(const std::string& func_pattern, const Log_Type& log_type, const std::string& content);
|
|
struct Log_Info{
|
|
bool console;
|
|
spdlog::level::level_enum level;
|
|
std::string func_pattern;
|
|
Log_Type log_type;
|
|
std::string content;
|
|
};
|
|
void set_redirect(const std::function<void(const Log_Info&)>& t) {
|
|
redirect = t;
|
|
}
|
|
static std::string to_log(const Log_Info& log_info);
|
|
|
|
protected:
|
|
std::string log_path;
|
|
size_t byte_size;
|
|
size_t piece_num;
|
|
std::shared_ptr<spdlog::logger> logger;
|
|
std::function<void(const Log_Info&)> redirect = nullptr;
|
|
void log_main(const Log_Info& log_info) const;
|
|
};
|
|
|
|
class Base_Logger_Manager : public Psc::Singleton<Base_Logger_Manager> {
|
|
public:
|
|
Base_Logger_Manager();
|
|
Base_Logger_Manager& set_thread_num(size_t thread_num);
|
|
Base_Logger_Manager& set_flush_seconds(size_t flush_seconds);
|
|
Base_Logger_Manager& set_buffer_size(size_t buffer_size);
|
|
void init() const;
|
|
~Base_Logger_Manager();
|
|
protected:
|
|
size_t thread_num = 1;
|
|
size_t flush_seconds = 3;
|
|
size_t buffer_size = 8192 * 8;
|
|
};
|
|
std::string get_current_date_string();
|
|
|
|
|