Files
UNSWebServerCore_WindowsDLL/UNSWebServerCore/TestLogger.cpp
T

90 lines
3.5 KiB
C++

#include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <functional>
// 确保引入包含日志宏和 GlobalServerLogger 的头文件
#include "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 的宏)
// =================================================================
std::cout << "\n--- [Part 1: FMT Style Macros Test] ---" << std::endl;
{
// 基础类型
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={}, string={}",
1024, "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);
// 混合复杂编排
std::vector<int> scores = {99, 95, 88};
SCLOGF_FATAL("Critical failure! Operator: '{}', Cluster Nodes: {}, Error Code: {}",
"RootAdmin", scores, 5005);
}
// =================================================================
// 2. 传统 printf 风格测试 (不带 F 的宏)
// =================================================================
std::cout << "\n--- [Part 2: Printf Style Macros Test] ---" << std::endl;
{
// 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);
}
std::cout << "\n" << std::endl;
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】退出前关闭日志流
SCLOG_CLOSE();
return 0;
}