std::string_view 的使用
This commit is contained in:
+31
-27
@@ -8,12 +8,13 @@
|
||||
#include "Core/Base/global_include.h"
|
||||
#include "spdlog/sinks/basic_file_sink.h"
|
||||
#include "spdlog/spdlog.h"
|
||||
#include <string_view>
|
||||
#ifdef WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include "Windows.h"
|
||||
|
||||
#else
|
||||
std::string handle_fileName(const std::string &utf8_str) { return utf8_str; }
|
||||
std::string handle_fileName(std::string_view utf8_str) { return std::string(utf8_str); }
|
||||
#endif
|
||||
#include "../system/export.h"
|
||||
#include "spdlog/async.h"
|
||||
@@ -54,8 +55,9 @@ std::string get_current_date_string() {
|
||||
}
|
||||
|
||||
std::shared_ptr<spdlog::logger>
|
||||
create_rotating_logger(const std::string &key, size_t byte_size, size_t num) {
|
||||
std::string fileName = "@/logs/" + key + ".log";
|
||||
create_rotating_logger(std::string_view key, size_t byte_size, size_t num) {
|
||||
auto key_string = std::string(key);
|
||||
std::string fileName = "@/logs/" + key_string + ".log";
|
||||
fileName = Psc::get_abs_path(fileName);
|
||||
fileName = Psc::utf8_2_platform(fileName);
|
||||
Psc::create_file_if_not_exists(fileName);
|
||||
@@ -64,7 +66,7 @@ create_rotating_logger(const std::string &key, size_t byte_size, size_t num) {
|
||||
|
||||
std::vector<spdlog::sink_ptr> sinks = {rotating_sink};
|
||||
auto logger = std::make_shared<spdlog::async_logger>(
|
||||
key, // Logger 名称
|
||||
key_string, // Logger 名称
|
||||
sinks.begin(), sinks.end(), // Sinks 列表
|
||||
spdlog::thread_pool(), // 使用初始化的线程池
|
||||
spdlog::async_overflow_policy::block // 当队列满时,阻塞写入
|
||||
@@ -72,13 +74,14 @@ create_rotating_logger(const std::string &key, size_t byte_size, size_t num) {
|
||||
return logger;
|
||||
}
|
||||
std::map<std::string, std::shared_ptr<spdlog::logger>> day_log_map;
|
||||
void rotating_log(const std::string &key, const std::string &info, int size,
|
||||
void rotating_log(std::string_view key, std::string_view info, int size,
|
||||
int num) {
|
||||
try {
|
||||
if (day_log_map.find(key) == day_log_map.end()) {
|
||||
day_log_map.insert({key, create_rotating_logger(key, size, num)});
|
||||
auto key_string = std::string(key);
|
||||
if (day_log_map.find(key_string) == day_log_map.end()) {
|
||||
day_log_map.insert({key_string, create_rotating_logger(key_string, size, num)});
|
||||
}
|
||||
std::shared_ptr<spdlog::logger> daily_logger = day_log_map[key];
|
||||
std::shared_ptr<spdlog::logger> daily_logger = day_log_map[key_string];
|
||||
daily_logger->set_pattern("%Y-%m-%d %H:%M:%S [%l] %v");
|
||||
daily_logger->info(info);
|
||||
daily_logger->flush();
|
||||
@@ -87,15 +90,16 @@ void rotating_log(const std::string &key, const std::string &info, int size,
|
||||
}
|
||||
}
|
||||
|
||||
void pure_log(const std::string &pure_path, const std::string &info,
|
||||
void pure_log(std::string_view pure_path, std::string_view info,
|
||||
bool close_after_write) {
|
||||
auto path = Psc::get_abs_path(pure_path);
|
||||
static std::map<std::string, std::shared_ptr<spdlog::logger>> file_logger_map;
|
||||
static std::mutex mtx; // 用于保护file_logger_map
|
||||
static std::mutex mtx2; // 用于保护file_logger_map
|
||||
static auto create_pure_logger = [](const std::string &fileName) {
|
||||
Psc::create_file_if_not_exists(fileName);
|
||||
auto c = spdlog::basic_logger_mt(fileName, fileName);
|
||||
static auto create_pure_logger = [](std::string_view fileName) {
|
||||
auto file_name_string = std::string(fileName);
|
||||
Psc::create_file_if_not_exists(file_name_string);
|
||||
auto c = spdlog::basic_logger_mt(file_name_string, file_name_string);
|
||||
auto formatter = std::make_unique<spdlog::pattern_formatter>(
|
||||
"%v", spdlog::pattern_time_type::local, "",
|
||||
spdlog::pattern_formatter::custom_flags());
|
||||
@@ -125,13 +129,13 @@ void pure_log(const std::string &pure_path, const std::string &info,
|
||||
}
|
||||
}
|
||||
|
||||
Log_Type &Log_Type::append(std::string key) {
|
||||
list.push_back(key);
|
||||
Log_Type &Log_Type::append(std::string_view key) {
|
||||
list.push_back(std::string(key));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Log_Type &Log_Type::append(std::string key, std::string val) {
|
||||
map.push_back({key, val});
|
||||
Log_Type &Log_Type::append(std::string_view key, std::string_view val) {
|
||||
map.push_back({std::string(key), std::string(val)});
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -149,7 +153,7 @@ std::string Log_Type::to_string() const {
|
||||
return base;
|
||||
}
|
||||
|
||||
Log_Type &Log_Type::set(const std::string &key, std::string val) {
|
||||
Log_Type &Log_Type::set(std::string_view key, std::string_view val) {
|
||||
append(key, val);
|
||||
return *this;
|
||||
}
|
||||
@@ -171,7 +175,7 @@ struct my_daily_filename_calculator {
|
||||
};
|
||||
} // namespace spdlog
|
||||
|
||||
BaseLogger::BaseLogger(const std::string &log_path, size_t byte_size,
|
||||
BaseLogger::BaseLogger(std::string_view log_path, size_t byte_size,
|
||||
size_t piece_num)
|
||||
: log_path(log_path), byte_size(byte_size), piece_num(piece_num) {
|
||||
std::string path = Psc::get_abs_path(log_path);
|
||||
@@ -249,18 +253,18 @@ void BaseLogger::log_main(const Log_Info &log_info) const {
|
||||
}
|
||||
}
|
||||
|
||||
void BaseLogger::c_debug(const std::string &func_pattern,
|
||||
const Log_Type &log_type, const std::string &content) {
|
||||
log_main({true, spdlog::level::debug, func_pattern, log_type, content});
|
||||
void BaseLogger::c_debug(std::string_view func_pattern,
|
||||
const Log_Type &log_type, std::string_view content) {
|
||||
log_main({true, spdlog::level::debug, std::string(func_pattern), log_type, std::string(content)});
|
||||
}
|
||||
|
||||
void BaseLogger::debug(const std::string &func_pattern,
|
||||
const Log_Type &log_type, const std::string &content) {
|
||||
log_main({false, spdlog::level::debug, func_pattern, log_type, content});
|
||||
void BaseLogger::debug(std::string_view func_pattern,
|
||||
const Log_Type &log_type, std::string_view content) {
|
||||
log_main({false, spdlog::level::debug, std::string(func_pattern), log_type, std::string(content)});
|
||||
}
|
||||
|
||||
void BaseLogger::error(const std::string &func_pattern,
|
||||
const Log_Type &log_type, const std::string &content) {
|
||||
log_main({true, spdlog::level::err, func_pattern, log_type, content});
|
||||
void BaseLogger::error(std::string_view func_pattern,
|
||||
const Log_Type &log_type, std::string_view content) {
|
||||
log_main({true, spdlog::level::err, std::string(func_pattern), log_type, std::string(content)});
|
||||
logger->flush();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user