winlin

refine config, add error line log

@@ -154,6 +154,7 @@ int SrsConfDirective::parse_conf(_srs_internal::SrsConfigBuffer* buffer, SrsDire @@ -154,6 +154,7 @@ int SrsConfDirective::parse_conf(_srs_internal::SrsConfigBuffer* buffer, SrsDire
154 154
155 while (true) { 155 while (true) {
156 std::vector<string> args; 156 std::vector<string> args;
  157 +
157 ret = read_token(buffer, args); 158 ret = read_token(buffer, args);
158 159
159 /** 160 /**
@@ -169,21 +170,21 @@ int SrsConfDirective::parse_conf(_srs_internal::SrsConfigBuffer* buffer, SrsDire @@ -169,21 +170,21 @@ int SrsConfDirective::parse_conf(_srs_internal::SrsConfigBuffer* buffer, SrsDire
169 } 170 }
170 if (ret == ERROR_SYSTEM_CONFIG_BLOCK_END) { 171 if (ret == ERROR_SYSTEM_CONFIG_BLOCK_END) {
171 if (type != parse_block) { 172 if (type != parse_block) {
172 - srs_error("line %d: unexpected \"}\"", buffer->line); 173 + srs_error("line %d: unexpected \"}\"", buffer->line + 1);
173 return ret; 174 return ret;
174 } 175 }
175 return ERROR_SUCCESS; 176 return ERROR_SUCCESS;
176 } 177 }
177 if (ret == ERROR_SYSTEM_CONFIG_EOF) { 178 if (ret == ERROR_SYSTEM_CONFIG_EOF) {
178 if (type == parse_block) { 179 if (type == parse_block) {
179 - srs_error("line %d: unexpected end of file, expecting \"}\"", buffer->line); 180 + srs_error("line %d: unexpected end of file, expecting \"}\"", conf_line + 1);
180 return ret; 181 return ret;
181 } 182 }
182 return ERROR_SUCCESS; 183 return ERROR_SUCCESS;
183 } 184 }
184 185
185 if (args.empty()) { 186 if (args.empty()) {
186 - srs_error("line %d: empty directive.", buffer->line); 187 + srs_error("line %d: empty directive.", conf_line + 1);
187 return ret; 188 return ret;
188 } 189 }
189 190
@@ -258,7 +259,7 @@ int SrsConfDirective::read_token(_srs_internal::SrsConfigBuffer* buffer, vector< @@ -258,7 +259,7 @@ int SrsConfDirective::read_token(_srs_internal::SrsConfigBuffer* buffer, vector<
258 if (ch == '{') { 259 if (ch == '{') {
259 return ERROR_SYSTEM_CONFIG_BLOCK_START; 260 return ERROR_SYSTEM_CONFIG_BLOCK_START;
260 } 261 }
261 - srs_error("line %d: unexpected '%c'", buffer->line, ch); 262 + srs_error("line %d: unexpected '%c'", buffer->line + 1, ch);
262 return ERROR_SYSTEM_CONFIG_INVALID; 263 return ERROR_SYSTEM_CONFIG_INVALID;
263 } 264 }
264 265
@@ -271,19 +272,19 @@ int SrsConfDirective::read_token(_srs_internal::SrsConfigBuffer* buffer, vector< @@ -271,19 +272,19 @@ int SrsConfDirective::read_token(_srs_internal::SrsConfigBuffer* buffer, vector<
271 switch (ch) { 272 switch (ch) {
272 case ';': 273 case ';':
273 if (args.size() == 0) { 274 if (args.size() == 0) {
274 - srs_error("line %d: unexpected ';'", buffer->line); 275 + srs_error("line %d: unexpected ';'", buffer->line + 1);
275 return ERROR_SYSTEM_CONFIG_INVALID; 276 return ERROR_SYSTEM_CONFIG_INVALID;
276 } 277 }
277 return ERROR_SYSTEM_CONFIG_DIRECTIVE; 278 return ERROR_SYSTEM_CONFIG_DIRECTIVE;
278 case '{': 279 case '{':
279 if (args.size() == 0) { 280 if (args.size() == 0) {
280 - srs_error("line %d: unexpected '{'", buffer->line); 281 + srs_error("line %d: unexpected '{'", buffer->line + 1);
281 return ERROR_SYSTEM_CONFIG_INVALID; 282 return ERROR_SYSTEM_CONFIG_INVALID;
282 } 283 }
283 return ERROR_SYSTEM_CONFIG_BLOCK_START; 284 return ERROR_SYSTEM_CONFIG_BLOCK_START;
284 case '}': 285 case '}':
285 if (args.size() != 0) { 286 if (args.size() != 0) {
286 - srs_error("line %d: unexpected '}'", buffer->line); 287 + srs_error("line %d: unexpected '}'", buffer->line + 1);
287 return ERROR_SYSTEM_CONFIG_INVALID; 288 return ERROR_SYSTEM_CONFIG_INVALID;
288 } 289 }
289 return ERROR_SYSTEM_CONFIG_BLOCK_END; 290 return ERROR_SYSTEM_CONFIG_BLOCK_END;
@@ -1068,59 +1069,6 @@ int SrsConfig::parse_options(int argc, char** argv) @@ -1068,59 +1069,6 @@ int SrsConfig::parse_options(int argc, char** argv)
1068 return ret; 1069 return ret;
1069 } 1070 }
1070 1071
1071 -int SrsConfig::parse_file(const char* filename)  
1072 -{  
1073 - int ret = ERROR_SUCCESS;  
1074 -  
1075 - config_file = filename;  
1076 -  
1077 - if (config_file.empty()) {  
1078 - return ERROR_SYSTEM_CONFIG_INVALID;  
1079 - }  
1080 -  
1081 - _srs_internal::SrsConfigBuffer buffer;  
1082 -  
1083 - if ((ret = buffer.fullfill(config_file.c_str())) != ERROR_SUCCESS) {  
1084 - return ret;  
1085 - }  
1086 -  
1087 - if ((ret = root->parse(&buffer)) != ERROR_SUCCESS) {  
1088 - return ret;  
1089 - }  
1090 -  
1091 - SrsConfDirective* conf = NULL;  
1092 - // check rtmp port specified by directive listen.  
1093 - if ((conf = get_listen()) == NULL || conf->args.size() == 0) {  
1094 - ret = ERROR_SYSTEM_CONFIG_INVALID;  
1095 - srs_error("line %d: conf error, "  
1096 - "directive \"listen\" is empty, ret=%d", (conf? conf->conf_line:0), ret);  
1097 - return ret;  
1098 - }  
1099 -  
1100 - // TODO: check the hls.  
1101 - // TODO: check forward.  
1102 - // TODO: check ffmpeg.  
1103 - // TODO: check http.  
1104 - // TODO: check pid.  
1105 -  
1106 - // check log  
1107 - std::string log_filename = this->get_log_file();  
1108 - if (get_log_tank_file() && log_filename.empty()) {  
1109 - ret = ERROR_SYSTEM_CONFIG_INVALID;  
1110 - srs_error("must specifies the file to write log to. ret=%d", ret);  
1111 - return ret;  
1112 - }  
1113 - if (get_log_tank_file()) {  
1114 - srs_trace("write log to file %s", log_filename.c_str());  
1115 - srs_trace("you can: tailf %s", log_filename.c_str());  
1116 - srs_trace("@see: %s", SRS_WIKI_URL_LOG);  
1117 - } else {  
1118 - srs_trace("write log to console");  
1119 - }  
1120 -  
1121 - return ret;  
1122 -}  
1123 -  
1124 int SrsConfig::parse_argv(int& i, char** argv) 1072 int SrsConfig::parse_argv(int& i, char** argv)
1125 { 1073 {
1126 int ret = ERROR_SUCCESS; 1074 int ret = ERROR_SUCCESS;
@@ -1197,6 +1145,59 @@ void SrsConfig::print_help(char** argv) @@ -1197,6 +1145,59 @@ void SrsConfig::print_help(char** argv)
1197 argv[0], argv[0], argv[0], argv[0]); 1145 argv[0], argv[0], argv[0], argv[0]);
1198 } 1146 }
1199 1147
  1148 +int SrsConfig::parse_file(const char* filename)
  1149 +{
  1150 + int ret = ERROR_SUCCESS;
  1151 +
  1152 + config_file = filename;
  1153 +
  1154 + if (config_file.empty()) {
  1155 + return ERROR_SYSTEM_CONFIG_INVALID;
  1156 + }
  1157 +
  1158 + _srs_internal::SrsConfigBuffer buffer;
  1159 +
  1160 + if ((ret = buffer.fullfill(config_file.c_str())) != ERROR_SUCCESS) {
  1161 + return ret;
  1162 + }
  1163 +
  1164 + if ((ret = root->parse(&buffer)) != ERROR_SUCCESS) {
  1165 + return ret;
  1166 + }
  1167 +
  1168 + SrsConfDirective* conf = NULL;
  1169 + // check rtmp port specified by directive listen.
  1170 + if ((conf = get_listen()) == NULL || conf->args.size() == 0) {
  1171 + ret = ERROR_SYSTEM_CONFIG_INVALID;
  1172 + srs_error("line %d: conf error, "
  1173 + "directive \"listen\" is empty, ret=%d", (conf? conf->conf_line:0), ret);
  1174 + return ret;
  1175 + }
  1176 +
  1177 + // TODO: check the hls.
  1178 + // TODO: check forward.
  1179 + // TODO: check ffmpeg.
  1180 + // TODO: check http.
  1181 + // TODO: check pid.
  1182 +
  1183 + // check log
  1184 + std::string log_filename = this->get_log_file();
  1185 + if (get_log_tank_file() && log_filename.empty()) {
  1186 + ret = ERROR_SYSTEM_CONFIG_INVALID;
  1187 + srs_error("must specifies the file to write log to. ret=%d", ret);
  1188 + return ret;
  1189 + }
  1190 + if (get_log_tank_file()) {
  1191 + srs_trace("write log to file %s", log_filename.c_str());
  1192 + srs_trace("you can: tailf %s", log_filename.c_str());
  1193 + srs_trace("@see: %s", SRS_WIKI_URL_LOG);
  1194 + } else {
  1195 + srs_trace("write log to console");
  1196 + }
  1197 +
  1198 + return ret;
  1199 +}
  1200 +
1200 string SrsConfig::cwd() 1201 string SrsConfig::cwd()
1201 { 1202 {
1202 return _cwd; 1203 return _cwd;
@@ -245,9 +245,9 @@ private: @@ -245,9 +245,9 @@ private:
245 public: 245 public:
246 virtual int parse_options(int argc, char** argv); 246 virtual int parse_options(int argc, char** argv);
247 private: 247 private:
248 - virtual int parse_file(const char* filename);  
249 virtual int parse_argv(int& i, char** argv); 248 virtual int parse_argv(int& i, char** argv);
250 virtual void print_help(char** argv); 249 virtual void print_help(char** argv);
  250 + virtual int parse_file(const char* filename);
251 public: 251 public:
252 virtual std::string cwd(); 252 virtual std::string cwd();
253 virtual std::string argv(); 253 virtual std::string argv();