赞
踩
下面代码定义了一个 Log
类,用于记录日志信息。这个类支持将日志信息输出到屏幕、单个文件或者按日志级别分类的多个文件。
主要功能:
#pragma once #include <iostream> #include <time.h> #include <stdarg.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #define SIZE 1024 #define Info 0 #define Debug 1 #define Warning 2 #define Error 3 #define Fatal 4 #define Screen 1 #define Onefile 2 #define Classfile 3 #define LogFile "log.txt" class Log { public: Log() { printMethod = Screen; path = "./log/"; } void Enable(int method) { printMethod = method; } std::string levelToString(int level) { switch (level) { case Info: return "Info"; case Debug: return "Debug"; case Warning: return "Warning"; case Error: return "Error"; case Fatal: return "Fatal"; default: return "None"; } } void printLog(int level, const std::string &logtxt) { switch (printMethod) { case Screen: std::cout << logtxt << std::endl; break; case Onefile: printOneFile(LogFile, logtxt); break; case Classfile: printClassFile(level, logtxt); break; default: break; } } void printOneFile(const std::string &logname, const std::string &logtxt) { // ./log/ + log.txt std::string _logname = path + logname; int fd = open(_logname.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0666); if (fd < 0) return; write(fd, logtxt.c_str(), logtxt.size()); close(fd); } void printClassFile(int level, const std::string &logtxt) { std::string filename = LogFile; filename += "."; // log.txt.Debug/Warning/Fatal filename += levelToString(level); printOneFile(filename, logtxt); } ~Log() { } void operator()(int level, const char *format, ...) { time_t t = time(nullptr); struct tm *ctime = localtime(&t); char leftbuffer[SIZE]; snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d:%d]", levelToString(level).c_str(), ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec); va_list s; va_start(s, format); char rightbuffer[SIZE]; vsnprintf(rightbuffer, sizeof(rightbuffer), format, s); va_end(s); // 格式: 默认部分 + 自定义部分 char logtxt[SIZE * 2]; snprintf(logtxt, sizeof(logtxt), "%s %s", leftbuffer, rightbuffer); printLog(level, logtxt); } private: int printMethod; std::string path; };
宏定义:
SIZE
: 定义缓冲区大小为1024。Info
, Debug
, Warning
, Error
, Fatal
: 定义日志级别。Screen
, Onefile
, Classfile
: 定义日志输出方式。LogFile
: 定义默认日志文件名为 “log.txt”。类的构造函数 Log()
:
Screen
(输出到屏幕)。Enable
函数:
levelToString
函数:
printLog
函数:
Screen
、Onefile
、Classfile
)。printOneFile
函数:
logname
,实际路径为 path
+ logname
。printClassFile
函数:
析构函数 ~Log()
:
重载的 operator()
:
printLog
打印日志。#pragma once #include <iostream> #include <time.h> #include <stdarg.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #define SIZE 1024 #define Info 0 #define Debug 1 #define Warning 2 #define Error 3 #define Fatal 4 #define Screen 1 #define Onefile 2 #define Classfile 3 #define LogFile "log.txt"
#pragma once
:确保头文件只被编译一次。#include
:引入了标准库和系统头文件。#define
:定义了一些常量,用于日志级别和输出方式。class Log { public: Log() { printMethod = Screen; path = "./log/"; } void Enable(int method) { printMethod = method; } std::string levelToString(int level) { switch (level) { case Info: return "Info"; case Debug: return "Debug"; case Warning: return "Warning"; case Error: return "Error"; case Fatal: return "Fatal"; default: return "None"; } }
Log()
:构造函数,初始化日志输出方式为屏幕输出(Screen
),并设置日志文件路径为./log/
。Enable(int method)
:设置日志输出方法。levelToString(int level)
:将日志级别转换为字符串。void printLog(int level, const std::string &logtxt) { switch (printMethod) { case Screen: std::cout << logtxt << std::endl; break; case Onefile: printOneFile(LogFile, logtxt); break; case Classfile: printClassFile(level, logtxt); break; default: break; } } void printOneFile(const std::string &logname, const std::string &logtxt) { std::string _logname = path + logname; int fd = open(_logname.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0666); if (fd < 0) return; write(fd, logtxt.c_str(), logtxt.size()); close(fd); } void printClassFile(int level, const std::string &logtxt) { std::string filename = LogFile; filename += "."; filename += levelToString(level); printOneFile(filename, logtxt); }
printLog(int level, const std::string &logtxt)
:根据不同的输出方式打印日志。printOneFile(const std::string &logname, const std::string &logtxt)
:将日志写入单个文件。printClassFile(int level, const std::string &logtxt)
:根据日志级别写入分类文件。~Log() { } void operator()(int level, const char *format, ...) { time_t t = time(nullptr); struct tm *ctime = localtime(&t); char leftbuffer[SIZE]; snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d:%d]", levelToString(level).c_str(), ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec); va_list s; va_start(s, format); char rightbuffer[SIZE]; vsnprintf(rightbuffer, sizeof(rightbuffer), format, s); va_end(s); char logtxt[SIZE * 2]; snprintf(logtxt, sizeof(logtxt), "%s %s", leftbuffer, rightbuffer); printLog(level, logtxt); }
operator()
:重载了函数调用操作符,使得Log
类的对象可以像函数一样被调用。
time_t t = time(nullptr);
获取当前时间。struct tm *ctime = localtime(&t);
将时间转换为本地时间结构。snprintf()
格式化时间和日志级别。va_list s;
定义一个可变参数列表。va_start(s, format);
初始化可变参数列表。vsnprintf(rightbuffer, sizeof(rightbuffer), format, s);
将可变参数格式化为字符串。va_end(args);
结束可变参数处理。leftbuffer
和 rightbuffer
拼接到 logtxt
中。printLog(level, logtxt)
输出日志。C++ 的可变参数函数使用 stdarg.h
中定义的一组宏来处理任意数量的参数。这些宏包括:
va_list
: 声明一个变量来存储参数列表。va_start
: 初始化参数列表。va_arg
: 访问参数列表中的下一个参数。va_end
: 结束参数列表的访问。下面是一些测试用例,用于测试多参数输入和不同的日志输出方式。
#include "log.hpp" int main() { Log logger; // 测试屏幕输出 logger.Enable(Screen); logger(Info, "This is an info log with no parameters"); logger(Warning, "This is a warning log with one parameter: %d", 42); logger(Error, "This is an error log with two parameters: %s and %d", "error_code", 404); // 测试单文件输出 logger.Enable(Onefile); logger(Debug, "Debug log to one file with one parameter: %f", 3.14); logger(Fatal, "Fatal log to one file with no parameters"); // 测试分类文件输出 logger.Enable(Classfile); logger(Info, "Info log to class file"); logger(Warning, "Warning log to class file with two parameters: %s and %d", "test", 123); return 0; }
这些测试用例将测试以下功能:
注意:要保证日志目录存在(默认为 ./log/
),日志文件才会自动创建在该目录内。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。