基本Json格式化。
This commit is contained in:
39
util/axc.util.cpp
Normal file
39
util/axc.util.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "axc.util.h"
|
||||
|
||||
#include <boost/nowide/iostream.hpp>
|
||||
#include <spdlog/common.h>
|
||||
#include <spdlog/sinks/rotating_file_sink.h>
|
||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||
#include <vector>
|
||||
|
||||
static std::shared_ptr<spdlog::logger> logger;
|
||||
|
||||
bool AxcUtil::initLogger(const std::string& logFile)
|
||||
{
|
||||
if (logger) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
auto fileSink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("aux_cmd.log", 1024 * 1024 * 5, 3);
|
||||
auto consoleSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
|
||||
fileSink->set_pattern("[%Y-%m-%d %H:%M:%S.%e][%l]: %v");
|
||||
consoleSink->set_pattern("[%H:%M:%S.%e] %^[%l] %v%$");
|
||||
std::vector<spdlog::sink_ptr> sinks{fileSink, consoleSink};
|
||||
logger = std::make_shared<spdlog::logger>("aux_cmd", sinks.begin(), sinks.end());
|
||||
logger->set_level(spdlog::level::debug);
|
||||
logger->flush_on(spdlog::level::debug);
|
||||
spdlog::register_logger(logger);
|
||||
} catch (const spdlog::spdlog_ex& ex) {
|
||||
boost::nowide::cerr << "Error creating logger: " << ex.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::shared_ptr<spdlog::logger> AxcUtil::getLogger()
|
||||
{
|
||||
if (!logger) {
|
||||
logger = spdlog::get("aux_cmd");
|
||||
}
|
||||
return logger;
|
||||
}
|
||||
19
util/axc.util.h
Normal file
19
util/axc.util.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef AXC_UTIL_H
|
||||
#define AXC_UTIL_H
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <string>
|
||||
|
||||
struct FormatterArg {
|
||||
int indent{4};
|
||||
std::string file;
|
||||
};
|
||||
|
||||
class AxcUtil
|
||||
{
|
||||
public:
|
||||
static bool initLogger(const std::string& logFile);
|
||||
static std::shared_ptr<spdlog::logger> getLogger();
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user