LogSystem beta 1.4
This is a logging system project
 
载入中...
搜索中...
未找到
config.hpp
浏览该文件的文档.
1
3#pragma once
4#include "../config/INIReader.h"
5#include "../logs/Xulog.h"
6#include <unordered_map>
7#include <string>
8namespace XuServer
9{
12 class Config
13 {
14 public:
17 Config(const std::string &filename)
18 {
19 INIReader reader(filename);
20 if (reader.ParseError() < 0)
21 {
22 ERROR("配置解析失败!");
23 }
24 init(reader);
25 }
26 using ptr = std::shared_ptr<Config>;
27 using config_map = std::unordered_map<std::string, std::unordered_map<std::string, std::string>>;
28
32 static Config::ptr getInstance(const std::string &filename)
33 {
34 if (_instance == nullptr)
35 _instance = std::make_shared<Config>(filename);
36 return _instance;
37 }
41 {
42 if (_instance == nullptr)
43 ERROR(" 请传入配置文件路径!");
44 return _instance;
45 }
50 std::string get(const std::string &section, const std::string &name)
51 {
52 auto sec_it = _config_data.find(section);
53 if (sec_it != _config_data.end())
54 {
55 auto name_it = sec_it->second.find(name);
56 if (name_it != sec_it->second.end())
57 {
58 return name_it->second;
59 }
60 }
61 return "";
62 }
63
64 private:
67 void init(INIReader &reader)
68 {
69 std::string ret = reader.Get("Server", "port", "8888");
70 _config_data["Server"]["port"] = ret;
71 ret = reader.Get("StdoutSink", "color", "");
72 if (!ret.empty())
73 {
74 _config_data["StdoutSink"]["color"] = ret;
75 }
76
77 ret = reader.Get("FileSink", "path", "");
78 if (!ret.empty())
79 {
80 _config_data["FileSink"]["path"] = ret;
81 }
82
83 ret = reader.Get("RollBySize", "path", "");
84 if (!ret.empty())
85 {
86 std::string r = reader.Get("RollBySize", "size", "1024");
87 _config_data["RollBySize"]["path"] = ret;
88 _config_data["RollBySize"]["size"] = r;
89 }
90
91 ret = reader.Get("RollByTime", "path", "");
92 if (!ret.empty())
93 {
94 std::string r = reader.Get("RollByTime", "type", "GAP_SECOND");
95 _config_data["RollByTime"]["path"] = ret;
96 _config_data["RollByTime"]["type"] = r;
97 }
98
99 ret = reader.Get("DataBaseSink", "path", "");
100 if (!ret.empty())
101 {
102 _config_data["DataBaseSink"]["path"] = ret;
103 }
104 }
105
106 private:
108 static ptr _instance;
109 };
111}
#define ERROR(fmt,...)
使用默认日志器打印错误信息
Definition Xulog.h:104
Definition INIReader.h:18
std::string Get(std::string section, std::string name, std::string default_value)
Definition INIReader.cpp:28
int ParseError()
Definition INIReader.cpp:23
用于读取和存储配置文件的类
Definition config.hpp:13
static ptr _instance
配置文件操作句柄
Definition config.hpp:108
void init(INIReader &reader)
读取配置文件并写入到映射表中
Definition config.hpp:67
Config(const std::string &filename)
构造函数
Definition config.hpp:17
std::unordered_map< std::string, std::unordered_map< std::string, std::string > > config_map
从section和name获取value的映射表
Definition config.hpp:27
std::string get(const std::string &section, const std::string &name)
获取配置文件的值
Definition config.hpp:50
std::shared_ptr< Config > ptr
配置管理句柄
Definition config.hpp:26
static Config::ptr getInstance()
获取配置文件操作句柄
Definition config.hpp:40
config_map _config_data
从section和name获取value的映射表
Definition config.hpp:107
static Config::ptr getInstance(const std::string &filename)
获取配置文件操作句柄
Definition config.hpp:32
Definition config.hpp:9