日志文件Log.hpp
- 日志文件的作用
- 简单的日志文件编写
日志文件的作用
日志文件可以很好的帮我们显示出程序运行的信息,例如,进程pid,运行时间,运行状况等,通过日志记录程序的执行路径、变量值、函数调用等,可以帮助我们快速定位和修复代码中的错误。
简单的日志文件编写
下面是用C/C++编写的一段日志文件: Log.hpp
#pragma once
//日志文件
#include<iostream>
#include<cstdio>
#include<stdarg.h>
#include<ctime>
#include<fstream>
#include <sys/stat.h>
#include <sys/types.h>
enum //日志等级设置
{
Debug = 0, //调试等级
Info, //常规
Warning,
Error,
Fatal //服务器
};
enum //设置打印风格
{
Screen = 0, //打印到显示器
OneFile, //打印到一个文件里
ClassFile //分文件打印
};
const int defauleStyle = Screen; //默认打印到屏幕上
const std::string defaultFilename = "log.";
const std::string logdir = "log";
std::string LevelToString(int level)
{
switch(level)
{
case Debug:
return "Debug";
case Info:
return "Info";
case Warning:
return "Warning";
case Error:
return "Error";
case Fatal:
return "Fatal";
default:
return "Unknow";
}
}
std::string localTime()
{
time_t curtime = time(0);
struct tm *t = localtime(&curtime);
char time_buffer[128];
snprintf(time_buffer,sizeof(time_buffer),"%d-%d-%d %d:%d:%d",\
t->tm_year+1900,t->tm_mon+1,t->tm_mday,\
t->tm_hour,t->tm_min,t->tm_sec);
return time_buffer;
}
class Log
{
public:
Log():style(defauleStyle),filename(defaultFilename)
{
mkdir(logdir.c_str(),0775); //创建log目录用以记录日志文件
}
//设置打印风格,默认打印到屏幕
void Enable(int sty)
{
style = sty;
}
void WriteOnefile(const std::string& logname, const std::string &message)
{
std::ofstream out(logname,std::ios::app); //创建文件,并以追加的方式打印内容
if(!out.is_open()) return;
out.write(message.c_str(),message.size()); //向目标文件中输出内容
out.close();
}
void WriteClassfile(const std::string& level,const std::string &message)
{
std::string logname = logdir; //写入文件名的时候加上路径,就会直接在所写路径下进行输出
logname += '/';
logname += filename;
logname += level;
WriteOnefile(logname,message);
}
void Writelog(const std::string &level,const std::string &message)
{
switch (style)
{
case Screen: //默认打印到屏幕
std::cout<<message<<std::endl;
break;
case OneFile: //默认打印到log.all文件中
WriteClassfile("all",message);
break;
case ClassFile: //默认打印到各类日志文件中
WriteClassfile(level,message);
break;
default:
break;
}
}
//使用可变参数
void LogMessage(int level,const char* format,...)
{
char leftbuffer[1024]; // 这里打印日志等级,时间 到leftguffer
snprintf(leftbuffer, sizeof(leftbuffer), "[%s] [%s]", LevelToString(level).c_str(), localTime().c_str());
char rightbuffer[1024]; // 这里打印输入参数的内容 到rightbuffer
va_list args;
va_start(args,format); //初始化可变参数列表
//此时args指向了可变参数部分 , 将参数中的信息输入到字符串mess中
vsnprintf(rightbuffer,sizeof(rightbuffer),format,args);
va_end(args);
//将信息合体
std::string message(leftbuffer);
message += rightbuffer;
//打印内容
Writelog(LevelToString(level),message);
}
~Log()
{}
public:
int style;//打印风格 上面默认设置为打印到屏幕
std::string filename; //文件名默认为: log.
};
调试代码:
test.cc
#include<iostream>
#include"Log.hpp"
#include <unistd.h>
using namespace std;
void testLog()
{
Log log;
log.Enable(ClassFile);
log.LogMessage(Debug," i am %d \n",getpid());
log.LogMessage(Info," i am %d \n",getpid());
log.LogMessage(Warning," i am %d \n",getpid());
log.LogMessage(Error," i am %d \n",getpid());
log.LogMessage(Fatal," i am %d \n",getpid());
log.LogMessage(Debug," i am %d \n",getpid());
log.LogMessage(Info," i am %d \n",getpid());
log.LogMessage(Warning," i am %d \n",getpid());
log.LogMessage(Error," i am %d \n",getpid());
log.LogMessage(Fatal," i am %d \n",getpid());
log.LogMessage(Debug," i am %d \n",getpid());
log.LogMessage(Info," i am %d \n",getpid());
log.LogMessage(Warning," i am %d \n",getpid());
log.LogMessage(Error," i am %d \n",getpid());
log.LogMessage(Fatal," i am %d \n",getpid());
}
int main()
{
testLog();
return 0;
}
测试结果:
可以看到,log目录下有各个日志文件,每个日志文件中有不同的内容
如:log.Debug