XNSim/XNCore/XNLogger.h

255 lines
5.6 KiB
C
Raw Normal View History

2025-04-28 12:25:20 +08:00
/**
* @file XNLogger.h
* @author jinchao
* @brief
* @version 1.0
* @date 2025-01-08
*
* @copyright Copyright (c) 2025 COMAC
*
*/
#pragma once
2025-05-20 15:39:40 +08:00
#include <string>
#include <mutex>
#include <fstream>
#include <chrono>
#include <filesystem>
2025-04-28 12:25:20 +08:00
#include <type_traits>
2025-05-20 15:39:40 +08:00
#include <iostream>
#include <iomanip>
#include <sstream>
2025-04-28 12:25:20 +08:00
/**
* @brief
*/
class XNLogger
{
public:
/**
* @brief
*/
enum LogLevel { Debug, Info, Warning, Error, Time };
/**
* @brief
* @return
*/
static XNLogger &instance()
{
static XNLogger instance;
return instance;
}
/**
* @brief
* @param level
* @param message
*/
2025-05-20 15:39:40 +08:00
void log(LogLevel level, const std::string &message);
2025-04-28 12:25:20 +08:00
/**
* @brief
* @param level
* @param enable
*/
void enableConsoleOutput(LogLevel level, bool enable);
/**
* @brief
* @param level
* @param enable
*/
void enableFileOutput(LogLevel level, bool enable);
private:
/**
* @brief
*/
2025-05-20 15:39:40 +08:00
XNLogger();
2025-04-28 12:25:20 +08:00
/**
* @brief
*/
~XNLogger();
/**
* @brief
*/
2025-05-20 15:39:40 +08:00
XNLogger(const XNLogger &) = delete;
2025-04-28 12:25:20 +08:00
/**
* @brief
*/
2025-05-20 15:39:40 +08:00
XNLogger &operator=(const XNLogger &) = delete;
2025-04-28 12:25:20 +08:00
/**
* @brief
*/
2025-05-20 15:39:40 +08:00
std::string logFilePath;
2025-04-28 12:25:20 +08:00
/**
* @brief
*/
bool consoleOutputEnabled[5];
/**
* @brief
*/
bool fileOutputEnabled[5];
/**
* @brief
*/
2025-05-20 15:39:40 +08:00
std::ofstream logFile;
2025-04-28 12:25:20 +08:00
/**
* @brief
*/
2025-05-20 15:39:40 +08:00
std::mutex mutex;
2025-04-28 12:25:20 +08:00
/**
* @brief
* @param level
* @return
*/
2025-05-20 15:39:40 +08:00
std::string logLevelToString(LogLevel level) const;
/**
* @brief
* @return
*/
std::string getCurrentTimeString() const;
2025-04-28 12:25:20 +08:00
/**
* @brief
*/
2025-05-20 15:39:40 +08:00
const std::string COLOR_RESET = "\033[0m";
2025-04-28 12:25:20 +08:00
/**
* @brief
*/
2025-05-20 15:39:40 +08:00
const std::string COLOR_DEBUG = "\033[34m"; // 蓝色
2025-04-28 12:25:20 +08:00
/**
* @brief
*/
2025-05-20 15:39:40 +08:00
const std::string COLOR_INFO = "\033[32m"; // 绿色
2025-04-28 12:25:20 +08:00
/**
* @brief
*/
2025-05-20 15:39:40 +08:00
const std::string COLOR_WARNING = "\033[33m"; // 黄色
2025-04-28 12:25:20 +08:00
/**
* @brief
*/
2025-05-20 15:39:40 +08:00
const std::string COLOR_ERROR = "\033[31m"; // 红色
2025-04-28 12:25:20 +08:00
};
/**
* @brief
*/
class XNLoggerHelper
{
public:
/**
* @brief
* @tparam Args
* @param level
* @param message
* @param args
*/
template <typename... Args>
inline static typename std::enable_if<(sizeof...(Args) > 0), void>::type
2025-05-20 15:39:40 +08:00
log(XNLogger::LogLevel level, const std::string &message, Args... args)
2025-04-28 12:25:20 +08:00
{
2025-05-20 15:39:40 +08:00
std::string formattedMessage = formatMessage(message, args...);
2025-04-28 12:25:20 +08:00
XNLogger::instance().log(level, formattedMessage);
}
/**
* @brief
* @param level
* @param message
*/
2025-05-20 15:39:40 +08:00
inline static void log(XNLogger::LogLevel level, const std::string &message)
2025-04-28 12:25:20 +08:00
{
XNLogger::instance().log(level, message);
}
private:
/**
* @brief
* @tparam T
* @param arg
* @return
*/
2025-04-28 12:25:20 +08:00
template <typename T>
2025-05-20 15:39:40 +08:00
static std::string convertToString(const T &arg)
2025-04-28 12:25:20 +08:00
{
if constexpr (std::is_arithmetic_v<T>) {
2025-05-20 15:39:40 +08:00
return std::to_string(arg); // 处理数值类型
} else if constexpr (std::is_same_v<T, std::string>) {
return arg;
} else if constexpr (std::is_convertible_v<T, std::string>) {
return std::string(arg);
} else if constexpr (std::is_same_v<T, char *> || std::is_same_v<T, const char *>) {
2025-05-20 15:39:40 +08:00
return std::string(arg);
} else {
static_assert(std::is_arithmetic_v<T> || std::is_same_v<T, std::string>
|| std::is_convertible_v<T, std::string> || std::is_same_v<T, char *>
|| std::is_same_v<T, const char *>,
"A01021001: 不支持的类型转换详见XNLogger.cppline 199");
2025-04-28 12:25:20 +08:00
}
}
/**
* @brief %1%2%3
* @tparam Args
* @param message
* @param args
* @return
*/
template <typename... Args>
static std::string formatMessage(const std::string &message, Args &&...args)
2025-04-28 12:25:20 +08:00
{
static_assert(sizeof...(Args) <= 9,
"A01021002: 单条日志参数数量超过限制详见XNLogger.cppline 216");
2025-05-20 15:39:40 +08:00
std::string result = message;
// 使用初始化列表展开参数包
int index = 1;
// 使用lambda和std::initializer_list展开参数包
(void)std::initializer_list<int>{(
[&result, &index](const auto &value) {
std::string placeholder = "%" + std::to_string(index++);
size_t pos = result.find(placeholder);
if (pos != std::string::npos) {
result.replace(pos, placeholder.length(), convertToString(value));
}
}(args),
0)...};
return result;
2025-04-28 12:25:20 +08:00
}
};
/**
* @brief
*/
#define LOG_DEBUG(message, ...) XNLoggerHelper::log(XNLogger::Debug, message, ##__VA_ARGS__)
/**
* @brief
*/
#define LOG_INFO(message, ...) XNLoggerHelper::log(XNLogger::Info, message, ##__VA_ARGS__)
/**
* @brief
*/
#define LOG_WARNING(message, ...) XNLoggerHelper::log(XNLogger::Warning, message, ##__VA_ARGS__)
/**
* @brief
*/
#define LOG_ERROR(message, ...) XNLoggerHelper::log(XNLogger::Error, message, ##__VA_ARGS__)