1. 日志
FILE和LINE是 C/C++ 编译器预定义的特殊宏:
FILE: 它会被编译器自动替换为当前代码所在源文件的路径或文件名(字符串类型)。 在日志函数中,它的作用是记录这条日志是从哪个文件输出的。 例如:如果在 test.cpp 中调用 LOG1 宏,FILE 就会被替换为 "test.cpp"(具体可能包含路径,取决于编译器),最终日志中会显示 [test.cpp : ...]。
LINE: 它会被编译器自动替换为当前代码所在的行号(整数类型)。 在日志函数中,它的作用是记录这条日志是从文件的哪一行输出的。 例如:如果 LOG1 宏调用写在 test.cpp 的第 25 行,LINE 就会被替换为 25,最终日志中会显示 [test.cpp : 25]。
创建一个 log1 函数,打印所需信息。
#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.cc
数据源路径 data/raw_html/raw.txt,前端网页代码位于 ./wwwroot/。 queue 是搜索关键字,json_string 是返回给用户的搜索结果。 实例化 Searcher 类,调用 InitSearcher 函数。使用 fgets 而非 cin,因为 cin 会忽略空格,而 fgets 可整行读取。


