Files
UNSWebServerCore_WindowsDLL/TestLogger/TestLogger.cpp
T
2026-06-30 17:46:34 +08:00

156 lines
5.8 KiB
C++

#include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <chrono>
#include <functional>
// 确保引入包含日志宏和 GlobalServerLogger 的头文件
#include "../UNSWebServerCore/ServerLogger.h"
#ifndef __FILENAME__
#define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
#endif
// 仅用于提供函数指针地址
void DummyPureFunction() {}
int main() {
// 【步骤 1】初始化控制台日志,开启最低级别
SCLOG_CONSOLE_INIT(uns::llTrace);
SCLOG_SHORT_INFO("=== ServerLogger Macro-based Test Started ===");
// =================================================================
// 1. C++ fmt 风格测试 (带 F 的宏)
// =================================================================
SCLOG_SHORT_TRACE("\n--- [Part 1: FMT Style Macros Test] ---");
{
// 基础类型
SCLOGF_INFO("Base values (Long): int={}, double={}, string={}, bool={}, wstring={}",
42, 3.14159, std::string("UNS_Core日志测试"), true, L"wstring防乱码测试");
SCLOGF_SHORT_DEBUG("Base values (Short): int={}, long={}, string={}",
1024, 2147483648, "ShortFormatTest");
// 容器 (Range)
std::vector<int> dummy_vector = {10, 20, 30, 40};
SCLOGF_DEBUG("Vector lazy contents: {}", dummy_vector);
// 键值对 (Pair)
std::pair<std::string, double> dummy_pair = {"Database_Connections", 12.0};
SCLOGF_WARNING("Metric data pair: {}", dummy_pair);
// 函数/闭包 (Function)
std::function<void()> func1(DummyPureFunction);
auto dummy_closure = [status = 500]() { return status; };
std::function func2 = dummy_closure;
SCLOGF_ERROR("Registered handlers - Pure: {}, Closure: {}", func1, func2);
// 时间
using Clock = std::chrono::system_clock;
// 1. 获取当前系统时间
Clock::time_point current_time = Clock::now();
// 2. 测试不同精度的时间点
Clock::time_point future_time = current_time + std::chrono::hours(24);
// 3. 纯粹的时间格式化打印测试
SCLOGF_INFO("--- Time Wrapper Verification ---");
SCLOGF_INFO("Current System Time : {:%Y-%m-%d %H:%M:%S}", current_time);
SCLOGF_INFO("Future Task Due Time: {:%H:%M:%S}", future_time);
SCLOGF_INFO("Custom Date Format : {:%Y/%m/%d}", current_time);
std::time_t raw_time = std::time(nullptr);
std::tm* time_info = std::localtime(&raw_time);
if (time_info != nullptr)
{
// 复制一份值对象传给日志,确保生命周期安全
std::tm current_tm = *time_info;
SCLOGF_INFO("--- TM Struct Verification ---");
SCLOGF_INFO("Current Time (TM) : {:%Y-%m-%d %H:%M:%S}", current_tm);
SCLOGF_INFO("Custom Date (TM) : {:%Y/%m/%d}", current_tm);
SCLOGF_INFO("Pure Time (TM) : {:%H:%M:%S}", current_tm);
}
// 4. Duration 格式化测试(基础单位)
SCLOGF_INFO("--- Duration Wrapper Verification ---");
std::chrono::nanoseconds dur_ns(123);
std::chrono::microseconds dur_us(4567);
std::chrono::milliseconds dur_ms(8901);
std::chrono::seconds dur_s(65);
// 基础单位测试
SCLOGF_INFO("Nanoseconds : {}", dur_ns);
SCLOGF_INFO("Microseconds : {}", dur_us);
SCLOGF_INFO("Milliseconds : {}", dur_ms);
SCLOGF_INFO("Seconds : {}", dur_s);
// 5. Duration 自动升档测试(关键)
std::chrono::seconds dur_s2(3600 + 120 + 5); // 1h 2m 5s
std::chrono::minutes dur_m(90); // 1h 30m
std::chrono::hours dur_h(48); // 2d
SCLOGF_INFO("--- Duration Auto Scaling ---");
SCLOGF_INFO("Mixed Seconds : {}", dur_s2);
SCLOGF_INFO("Minutes Overflow : {}", dur_m);
SCLOGF_INFO("Hours Overflow : {}", dur_h);
// 6. 高精度 duration 测试(纳秒级)
std::chrono::nanoseconds high_ns(1234567890123LL);
SCLOGF_INFO("--- High Precision Duration ---");
SCLOGF_INFO("Large Nanosec : {}", high_ns);
// 7. 负载/极值测试(防止溢出或异常)
std::chrono::nanoseconds zero_ns(0);
std::chrono::nanoseconds near_us(999);
std::chrono::nanoseconds near_ms(999999);
SCLOGF_INFO("--- Edge Cases ---");
SCLOGF_INFO("Zero Duration : {}", zero_ns);
SCLOGF_INFO("Near Micro Bound : {}", near_us);
SCLOGF_INFO("Near Milli Bound : {}", near_ms);
// 混合复杂编排
std::vector<int> scores = {99, 95, 88};
SCLOGF_FATAL("Critical failure! Operator: '{}', Cluster Nodes: {}, Error Code: {}",
"RootAdmin", scores, 5005);
}
// =================================================================
// 2. 传统 printf 风格测试 (不带 F 的宏)
// =================================================================
SCLOG_SHORT_TRACE("\n--- [Part 2: Printf Style Macros Test] ---");
{
// 2.1 测试带文件行号的长日志宏 (SCLOG_xxx)
SCLOG_TRACE("Printf Trace log: msg=%s, val=%d", "TraceMessage", 100);
SCLOG_DEBUG("Printf Debug log: score=%.2f", 98.5);
SCLOG_INFO("Printf Info log: hex=0x%X", 0xAB);
SCLOG_WARNING("Printf Warning log: active=%s", "true");
SCLOG_ERROR("Printf Error log: connection count=%d", 5);
SCLOG_FATAL("Printf Fatal log: system exit code=%d", -1);
// 2.2 测试不带文件行号的短日志宏 (SCLOG_SHORT_xxx)
SCLOG_SHORT_TRACE("Printf Short Trace: code=%d", 1);
SCLOG_SHORT_DEBUG("Printf Short Debug: name=%s", "WorkerA");
SCLOG_SHORT_INFO("Printf Short Info: load=%d%%", 85);
SCLOG_SHORT_WARNING("Printf Short Warning: threshold=%.1f", 90.0);
SCLOG_SHORT_ERROR("Printf Short Error: mask=0x%x", 0xFF00);
SCLOG_SHORT_FATAL("Printf Short Fatal: panic id=%d", 999);
}
SCLOG_SHORT_TRACE("");
SCLOG_SHORT_INFO("=== ServerLogger Macro-based Test Completed ===");
for(int i = 5; i > 0; i--)
{
using namespace std::chrono;
SCLOGF_SHORT_INFO("Close Log Stream in {} Seconds", i);
std::this_thread::sleep_for(1s);
}
// 【步骤 2】退出前关闭日志流
SCLOGF_SHORT_INFO("Log Stream Closed");
SCLOG_CLOSE();
return 0;
}