为了监控项目状态,日志记录是开发中的必要环节。Server 部分则是各类功能的集成入口。
1. 日志
__FILE__ 和 __LINE__ 是 C/C++ 编译器预定义的特殊宏:
__FILE__:
它会被编译器自动替换为当前代码所在源文件的路径或文件名(字符串类型)。在日志函数中,它的作用是记录这条日志是从哪个文件输出的。
__LINE__:
它会被编译器自动替换为当前代码所在的行号(整数类型)。在日志函数中,它的作用是记录这条日志是从文件的哪一行输出的。
以下是一个基础的日志打印函数示例:
#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,前端资源目录为 ./wwwroot。Queue 代表搜索关键字,json_string 用于返回搜索结果。
主程序中实例化 Searcher 类并调用 InitSearcher 初始化。使用 fgets 而非 cin 读取输入,因为 cin 会忽略空格,而 fgets 可整行读取。输入缓冲区末尾的换行符需手动移除。处理后的结果交给 query,调用 Search 函数生成 json_string 并输出。


