在工程实践中,完善的日志系统是排查问题的关键。服务端逻辑则承载了所有类的最终调用入口。
1. 日志系统
__FILE__ 和 __LINE__ 是 C/C++ 编译器预定义的特殊宏,能自动捕获当前上下文信息:
__FILE__:被替换为当前源文件的路径或文件名(字符串)。日志中用于标识来源文件。__LINE__:被替换为当前代码所在的行号(整数)。日志中用于定位具体位置。
例如在 test.cpp 第 25 行调用宏时,日志会显示 [test.cpp : 25]。
下面封装一个基础的日志打印函数,配合宏使用:
#pragma once
#include <iostream>
#include <string>
#include <ctime>
#define NORMAL 1
#define WARNING 2
#define DEBUG 3
#define FATAL 4
#define LOG1(LEVEL, MESSAGE) log1(#LEVEL, MESSAGE, __FILE__, __LINE__)
void log1(std::string level, std::string message, std::string file, int line) {
std::cout << "[" << level << "]"
<< "[" << time(nullptr) << "]"
<< "[" << message << "]"
<< "[" << file << " : " << line << "]"
<< std::endl;
}
2. Server 端实现
数据源位于 data/raw_html/raw.txt,静态资源目录为 。



