# sylar
## 开发环境
Centos7
gcc 9.1
cmake
## 项目路径
bin -- 二进制
build -- 中间文件路径
cmake -- cmake函数文件夹
CMakeList.txt -- cmake的定义文件
lib -- 库的输出路径
Makefile
sylar -- 源代码路径
tests -- 测试代码路径
## 日志系统
1)
Log4J
Logger(定义日志类别)
|
|--------Formatter(日志格式)
|
Appender(日志格式)
## 协程库封装
## socket函数库
## http协议开发
## 分布协议
##推荐系统
sylar/log.h文章来源:https://www.toymoban.com/news/detail-538949.html
#ifndef __SYLAR_LOG_H
#define __SYLAR_LOG_H
#include <sstream>
#include <fstream>
#include <string>
#include <stdint.h>
#include <memory>
#include <list>
namespace sylar{
//日志事件
class LogEvent{
public:
typedef std::shared_ptr<LogEvent> ptr;
LogEvent();
private:
const char* m_file = nullptr; //文件名
int32_t m_line = 0; //行号
uint32_t m_elapse = 0; //程序启动开始到现在的毫秒数
int32_t m_thread = 0; //线程 id
uint32_t m_fiberId = 0; //协程 id
uint64_t m_time; //时间戳
std::string m_content;
};
//日志级别
class LogLevel{
public:
enum Level{
DEBUG = 1,
INFO = 2,
WARN = 3,
ERROR = 4,
FATAL = 5
};
};
//日志格式器
class LogFormatter{
public:
typedef std::shared_ptr<LogFormatter> ptr;
std::string format(LogEvent::ptr event);
private:
};
//日志输出地
class LogAppender{
public:
typedef std::shared_ptr<LogAppender> ptr;
virtual ~LogAppender(){}
virtual void log(LogLevel::Level level,LogEvent::ptr event) = 0;//改写为纯虚函数
void setFormatter(LogFormatter::ptr val){m_formatter = val;}
LogFormatter::ptr getFormatter() const { return m_formatter;}
protected://改为保护
LogLevel::Level m_level;
LogFormatter::ptr m_formatter;
};
//日志器
class Logger{
public:
typedef std::shared_ptr<Logger> ptr;
Logger(const std::string& name = "root");
void log(LogLevel::Level level,LogEvent::ptr event);
void debug(LogEvent::ptr event);
void info(LogEvent::ptr event);
void warn(LogEvent::ptr event);
void error(LogEvent::ptr event);
void fatal(LogEvent::ptr event);
void addAppender(LogAppender::ptr appender);
void delAppender(LogAppender::ptr appender);
LogLevel::Level getLevel() const { return m_level;}
void setLevel(LogLevel::Level val){m_level = val;}
private:
std::string m_name; //日志名称
LogLevel::Level m_level; //日志级别
std::list<LogAppender::ptr> m_appenders; //Appender集合
};
//输出到控制台Appender
class StdoutLogAppender:public LogAppender{
public:
typedef std::shared_ptr<StdoutLogAppender> ptr;
void log(LogLevel::Level level,LogEvent::ptr event) override;
};
//定义输出到文件的Appender
class FileLogAppender:public LogAppender{
public:
typedef std::shared_ptr<FileLogAppender> ptr;
FileLogAppender(const std::string& filename);
void log(LogLevel::Level level,LogEvent::ptr event) override;
//重新打开文件,文件打开成功返回true
bool reopen();
private:
std::string m_filename;
std::ofstream m_filestream;
};
}
#endif
sylar/log.cpp文章来源地址https://www.toymoban.com/news/detail-538949.html
#include "log.h"
#include <iostream>
namespace sylar{
Logger::Logger(const std::string& name):m_name(name){}
void Logger::addAppender(LogAppender::ptr appender){
m_appenders.push_back(appender);
}
void Logger::delAppender(LogAppender::ptr appender){
for(auto it = m_appenders.begin();it!=m_appenders.end();it++){
if(*it == appender){
m_appenders.erase(it);
break;
}
}
}
void Logger::log(LogLevel::Level level,LogEvent::ptr event){
if(level >= m_level){
for(auto& i:m_appenders){
i->log(level,event);
}
}
}
void Logger::debug(LogEvent::ptr event){
debug(LogLevel::DEBUG,event);
}
void Logger::info(LogEvent::ptr event){
debug(LogLevel::INFO,event);
}
void Logger::warn(LogEvent::ptr event){
debug(LogLevel::WARN,event);
}
void Logger::error(LogEvent::ptr event){
debug(LogLevel::ERROR,event);
}
void Logger::fatal(LogEvent::ptr event){
debug(LogLevel::FATAL,event);
}
FileLogAppender::FileLogAppender(const std::string& filename):m_filename(filename){
}
void FileLogAppender::log(LogLevel::Level level,LogEvent::ptr event){
if(level >= m_level){
m_filestream << m_formatter->format(event);
}
}
bool FileLogAppender::reopen(){
if(m_filestream){
m_filestream.close();
}
m_filestream.open(m_filename);
return !!m_filestream;
}
void StdoutLogAppender::log(LogLevel::Level level,LogEvent::ptr event){
if(level >= m_level){
std::cout<<m_formatter->format(event);
}
}
}
到了这里,关于C++服务器框架03_日志系统03_appender的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!