-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cpp
executable file
·128 lines (116 loc) · 3.92 KB
/
Config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// Created by Administrator on 2019/10/8 0008.
//
#include "Config.h"
#include <fstream>
using namespace std;
extern Config global_config;
void Config::ReadConfigFileAgain() {
configs.clear();
Parse(config_filename);
}
void Config::Parse(string file) {
if (file.length() <= 0 && config_filename.length() <= 0) {
throw "Please Provide Invalid Config Filename";
}
if (file.length() > 0) {
config_filename = file;
}
ifstream stream;
stream.open(config_filename, ios::in);
unordered_map<string, string>::iterator iterator;
if (!stream) {
throw "Open Config failed";
} else {
string s;
int pos;
int line = 0;
while (true) {
++line;
s.clear();
getline(stream, s);
if (s.length() > 0) {
if (s[0] == '#') {
continue;
} else {
pos = s.find_first_of('=', 0);
if (pos == string::npos) {
break;
} else {
configs[s.substr(0, pos)] = s.substr(pos + 1);//skip char '='
}
}
} else {
break;
}
}
}
iterator = configs.begin();
int cpu_num;
while (iterator != configs.end()) {
if ((*iterator).first == "port") {
try {
port = stoi((*iterator).second);
} catch (std::invalid_argument) {
throw "Check Port Firstly";
} catch (std::out_of_range) {
throw "Port Too Large";
}
} else if ((*iterator).first == "process_num") {
try {
process_num = stoi((*iterator).second);
} catch (std::invalid_argument) {
throw "Invalid Process Num";
} catch (std::out_of_range) {
throw "Process Num Too Large";
}
//refer to https://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine
#ifdef linux
cpu_num = sysconf(_SC_NPROCESSORS_ONLN);
if (process_num > cpu_num) {
process_num = cpu_num;
}
#endif
#ifdef _WIN32
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
cpu_num = sysinfo.dwNumberOfProcessors;
#endif
} else if ((*iterator).first == "run_as_daemon") {
#ifdef linux
if ((*iterator).second.compare(0,4,"true") !=0 && (*iterator).second.compare(0,4,"false") !=0) {
throw "Config run_as_daemon Only Support true or false";
}
run_as_daemon = (*iterator).second == "true";
#else
run_as_daemon = false;
#endif
} else if ((*iterator).first == "debug_open") {
if ((*iterator).second.compare(0, 4, "true") != 0 && (*iterator).second.compare(0, 4, "false") != 0) {
throw "Config debug_open Only Support true or false";
}
debug_open = (*iterator).second.compare(0, 4, "true") == 0;
} else if ((*iterator).first == "log_file_path") {
log_file_path = (*iterator).second;
}
if (log_file_path.length() <= 0) {
throw "Log File Not Configured";
}
++iterator;
}
if (debug_open) {
cout << "---------------app config as follows-------------" << endl;
cout << "WebSocket Server Port:" << port << endl;
cout << "Worker Process Num:" << process_num << endl;
cout << "Debug Mode:" << (debug_open ? "open" : "close") << endl;
cout << "Run As Daemon:" << (run_as_daemon ? "true" : "false") << endl;
cout << "-------------------------------------------------" << endl;
}
}
void Config::Print() {
auto ib = configs.begin();
auto ie = configs.end();
for (; ib != ie; ++ib) {
cout << (*ib).first << ":" << (*ib).second << endl;
}
}