为了更好地监控项目运行状态,日志记录是开发过程中不可或缺的一环,Server 端则是各类功能模块的最终集成入口。
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;
}



