winlin

for #319, raw api support update srs log tank, level and file.

@@ -951,39 +951,24 @@ int SrsConfig::reload_conf(SrsConfig* conf) @@ -951,39 +951,24 @@ int SrsConfig::reload_conf(SrsConfig* conf)
951 951
952 // merge config: srs_log_tank 952 // merge config: srs_log_tank
953 if (!srs_directive_equals(root->get("srs_log_tank"), old_root->get("srs_log_tank"))) { 953 if (!srs_directive_equals(root->get("srs_log_tank"), old_root->get("srs_log_tank"))) {
954 - for (it = subscribes.begin(); it != subscribes.end(); ++it) {  
955 - ISrsReloadHandler* subscribe = *it;  
956 - if ((ret = subscribe->on_reload_log_tank()) != ERROR_SUCCESS) {  
957 - srs_error("notify subscribes reload srs_log_tank failed. ret=%d", ret); 954 + if ((ret = do_reload_srs_log_tank()) != ERROR_SUCCESS) {
958 return ret; 955 return ret;
959 } 956 }
960 } 957 }
961 - srs_trace("reload srs_log_tank success.");  
962 - }  
963 958
964 // merge config: srs_log_level 959 // merge config: srs_log_level
965 if (!srs_directive_equals(root->get("srs_log_level"), old_root->get("srs_log_level"))) { 960 if (!srs_directive_equals(root->get("srs_log_level"), old_root->get("srs_log_level"))) {
966 - for (it = subscribes.begin(); it != subscribes.end(); ++it) {  
967 - ISrsReloadHandler* subscribe = *it;  
968 - if ((ret = subscribe->on_reload_log_level()) != ERROR_SUCCESS) {  
969 - srs_error("notify subscribes reload srs_log_level failed. ret=%d", ret); 961 + if ((ret = do_reload_srs_log_level()) != ERROR_SUCCESS) {
970 return ret; 962 return ret;
971 } 963 }
972 } 964 }
973 - srs_trace("reload srs_log_level success.");  
974 - }  
975 965
976 // merge config: srs_log_file 966 // merge config: srs_log_file
977 if (!srs_directive_equals(root->get("srs_log_file"), old_root->get("srs_log_file"))) { 967 if (!srs_directive_equals(root->get("srs_log_file"), old_root->get("srs_log_file"))) {
978 - for (it = subscribes.begin(); it != subscribes.end(); ++it) {  
979 - ISrsReloadHandler* subscribe = *it;  
980 - if ((ret = subscribe->on_reload_log_file()) != ERROR_SUCCESS) {  
981 - srs_error("notify subscribes reload srs_log_file failed. ret=%d", ret); 968 + if ((ret = do_reload_srs_log_file()) != ERROR_SUCCESS) {
982 return ret; 969 return ret;
983 } 970 }
984 } 971 }
985 - srs_trace("reload srs_log_file success.");  
986 - }  
987 972
988 // merge config: pithy_print_ms 973 // merge config: pithy_print_ms
989 if (!srs_directive_equals(root->get("pithy_print_ms"), old_root->get("pithy_print_ms"))) { 974 if (!srs_directive_equals(root->get("pithy_print_ms"), old_root->get("pithy_print_ms"))) {
@@ -2316,6 +2301,81 @@ int SrsConfig::raw_set_ff_log_dir(string ff_log_dir, bool& applied) @@ -2316,6 +2301,81 @@ int SrsConfig::raw_set_ff_log_dir(string ff_log_dir, bool& applied)
2316 return ret; 2301 return ret;
2317 } 2302 }
2318 2303
  2304 +int SrsConfig::raw_set_srs_log_tank(string srs_log_tank, bool& applied)
  2305 +{
  2306 + int ret = ERROR_SUCCESS;
  2307 +
  2308 + applied = false;
  2309 +
  2310 +
  2311 + SrsConfDirective* conf = root->get_or_create("srs_log_tank");
  2312 +
  2313 + if (conf->arg0() == srs_log_tank) {
  2314 + return ret;
  2315 + }
  2316 +
  2317 + conf->args.clear();
  2318 + conf->args.push_back(srs_log_tank);
  2319 +
  2320 + if ((ret = do_reload_srs_log_tank()) != ERROR_SUCCESS) {
  2321 + return ret;
  2322 + }
  2323 +
  2324 + applied = true;
  2325 +
  2326 + return ret;
  2327 +}
  2328 +
  2329 +int SrsConfig::raw_set_srs_log_level(string srs_log_level, bool& applied)
  2330 +{
  2331 + int ret = ERROR_SUCCESS;
  2332 +
  2333 + applied = false;
  2334 +
  2335 +
  2336 + SrsConfDirective* conf = root->get_or_create("srs_log_level");
  2337 +
  2338 + if (conf->arg0() == srs_log_level) {
  2339 + return ret;
  2340 + }
  2341 +
  2342 + conf->args.clear();
  2343 + conf->args.push_back(srs_log_level);
  2344 +
  2345 + if ((ret = do_reload_srs_log_level()) != ERROR_SUCCESS) {
  2346 + return ret;
  2347 + }
  2348 +
  2349 + applied = true;
  2350 +
  2351 + return ret;
  2352 +}
  2353 +
  2354 +int SrsConfig::raw_set_srs_log_file(string srs_log_file, bool& applied)
  2355 +{
  2356 + int ret = ERROR_SUCCESS;
  2357 +
  2358 + applied = false;
  2359 +
  2360 +
  2361 + SrsConfDirective* conf = root->get_or_create("srs_log_file");
  2362 +
  2363 + if (conf->arg0() == srs_log_file) {
  2364 + return ret;
  2365 + }
  2366 +
  2367 + conf->args.clear();
  2368 + conf->args.push_back(srs_log_file);
  2369 +
  2370 + if ((ret = do_reload_srs_log_file()) != ERROR_SUCCESS) {
  2371 + return ret;
  2372 + }
  2373 +
  2374 + applied = true;
  2375 +
  2376 + return ret;
  2377 +}
  2378 +
2319 int SrsConfig::do_reload_listen() 2379 int SrsConfig::do_reload_listen()
2320 { 2380 {
2321 int ret = ERROR_SUCCESS; 2381 int ret = ERROR_SUCCESS;
@@ -2350,6 +2410,57 @@ int SrsConfig::do_reload_pid() @@ -2350,6 +2410,57 @@ int SrsConfig::do_reload_pid()
2350 return ret; 2410 return ret;
2351 } 2411 }
2352 2412
  2413 +int SrsConfig::do_reload_srs_log_tank()
  2414 +{
  2415 + int ret = ERROR_SUCCESS;
  2416 +
  2417 + vector<ISrsReloadHandler*>::iterator it;
  2418 + for (it = subscribes.begin(); it != subscribes.end(); ++it) {
  2419 + ISrsReloadHandler* subscribe = *it;
  2420 + if ((ret = subscribe->on_reload_log_tank()) != ERROR_SUCCESS) {
  2421 + srs_error("notify subscribes reload srs_log_tank failed. ret=%d", ret);
  2422 + return ret;
  2423 + }
  2424 + }
  2425 + srs_trace("reload srs_log_tank success.");
  2426 +
  2427 + return ret;
  2428 +}
  2429 +
  2430 +int SrsConfig::do_reload_srs_log_level()
  2431 +{
  2432 + int ret = ERROR_SUCCESS;
  2433 +
  2434 + vector<ISrsReloadHandler*>::iterator it;
  2435 + for (it = subscribes.begin(); it != subscribes.end(); ++it) {
  2436 + ISrsReloadHandler* subscribe = *it;
  2437 + if ((ret = subscribe->on_reload_log_level()) != ERROR_SUCCESS) {
  2438 + srs_error("notify subscribes reload srs_log_level failed. ret=%d", ret);
  2439 + return ret;
  2440 + }
  2441 + }
  2442 + srs_trace("reload srs_log_level success.");
  2443 +
  2444 + return ret;
  2445 +}
  2446 +
  2447 +int SrsConfig::do_reload_srs_log_file()
  2448 +{
  2449 + int ret = ERROR_SUCCESS;
  2450 +
  2451 + vector<ISrsReloadHandler*>::iterator it;
  2452 + for (it = subscribes.begin(); it != subscribes.end(); ++it) {
  2453 + ISrsReloadHandler* subscribe = *it;
  2454 + if ((ret = subscribe->on_reload_log_file()) != ERROR_SUCCESS) {
  2455 + srs_error("notify subscribes reload srs_log_file failed. ret=%d", ret);
  2456 + return ret;
  2457 + }
  2458 + }
  2459 + srs_trace("reload srs_log_file success.");
  2460 +
  2461 + return ret;
  2462 +}
  2463 +
2353 string SrsConfig::config() 2464 string SrsConfig::config()
2354 { 2465 {
2355 return config_file; 2466 return config_file;
@@ -349,9 +349,24 @@ public: @@ -349,9 +349,24 @@ public:
349 * raw set the global ffmpeg log dir. 349 * raw set the global ffmpeg log dir.
350 */ 350 */
351 virtual int raw_set_ff_log_dir(std::string ff_log_dir, bool& applied); 351 virtual int raw_set_ff_log_dir(std::string ff_log_dir, bool& applied);
  352 + /**
  353 + * raw set the global log tank.
  354 + */
  355 + virtual int raw_set_srs_log_tank(std::string srs_log_tank, bool& applied);
  356 + /**
  357 + * raw set the global log level.
  358 + */
  359 + virtual int raw_set_srs_log_level(std::string srs_log_level, bool& applied);
  360 + /**
  361 + * raw set the global log file path for file tank.
  362 + */
  363 + virtual int raw_set_srs_log_file(std::string srs_log_file, bool& applied);
352 private: 364 private:
353 virtual int do_reload_listen(); 365 virtual int do_reload_listen();
354 virtual int do_reload_pid(); 366 virtual int do_reload_pid();
  367 + virtual int do_reload_srs_log_tank();
  368 + virtual int do_reload_srs_log_level();
  369 + virtual int do_reload_srs_log_file();
355 public: 370 public:
356 /** 371 /**
357 * get the config file path. 372 * get the config file path.
@@ -989,6 +989,10 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -989,6 +989,10 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
989 // listen 1935,1936 the port list. 989 // listen 1935,1936 the port list.
990 // pid ./objs/srs.pid the pid file of srs. 990 // pid ./objs/srs.pid the pid file of srs.
991 // chunk_size 60000 the global RTMP chunk_size. 991 // chunk_size 60000 the global RTMP chunk_size.
  992 + // ff_log_dir ./objs the dir for ffmpeg log.
  993 + // srs_log_tank file the tank to log, file or console.
  994 + // srs_log_level trace the level of log, verbose, info, trace, warn, error.
  995 + // srs_log_file ./objs/srs.log the log file when tank is file.
992 if (rpc == "update") { 996 if (rpc == "update") {
993 if (!allow_update) { 997 if (!allow_update) {
994 ret = ERROR_SYSTEM_CONFIG_RAW_DISABLED; 998 ret = ERROR_SYSTEM_CONFIG_RAW_DISABLED;
@@ -1004,7 +1008,8 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -1004,7 +1008,8 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
1004 return srs_api_response_code(w, r, ret); 1008 return srs_api_response_code(w, r, ret);
1005 } 1009 }
1006 if (scope != "listen" && scope != "pid" && scope != "chunk_size" 1010 if (scope != "listen" && scope != "pid" && scope != "chunk_size"
1007 - && scope != "ff_log_dir" 1011 + && scope != "ff_log_dir" && scope != "srs_log_tank" && scope != "srs_log_level"
  1012 + && scope != "srs_log_file"
1008 ) { 1013 ) {
1009 ret = ERROR_SYSTEM_CONFIG_RAW_NOT_ALLOWED; 1014 ret = ERROR_SYSTEM_CONFIG_RAW_NOT_ALLOWED;
1010 srs_error("raw api query invalid scope=%s. ret=%d", scope.c_str(), ret); 1015 srs_error("raw api query invalid scope=%s. ret=%d", scope.c_str(), ret);
@@ -1068,6 +1073,39 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -1068,6 +1073,39 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
1068 srs_error("raw api update ff_log_dir=%s failed. ret=%d", value.c_str(), ret); 1073 srs_error("raw api update ff_log_dir=%s failed. ret=%d", value.c_str(), ret);
1069 return srs_api_response_code(w, r, ret); 1074 return srs_api_response_code(w, r, ret);
1070 } 1075 }
  1076 + } else if (scope == "srs_log_tank") {
  1077 + if (value.empty() || (value != "file" && value != "console")) {
  1078 + ret = ERROR_SYSTEM_CONFIG_RAW_PARAMS;
  1079 + srs_error("raw api update check srs_log_tank=%s failed. ret=%d", value.c_str(), ret);
  1080 + return srs_api_response_code(w, r, ret);
  1081 + }
  1082 +
  1083 + if ((ret = _srs_config->raw_set_srs_log_tank(value, applied)) != ERROR_SUCCESS) {
  1084 + srs_error("raw api update srs_log_tank=%s failed. ret=%d", value.c_str(), ret);
  1085 + return srs_api_response_code(w, r, ret);
  1086 + }
  1087 + } else if (scope == "srs_log_level") {
  1088 + if (value != "verbose" && value != "info" && value != "trace" && value != "warn" && value != "error") {
  1089 + ret = ERROR_SYSTEM_CONFIG_RAW_PARAMS;
  1090 + srs_error("raw api update check srs_log_level=%s failed. ret=%d", value.c_str(), ret);
  1091 + return srs_api_response_code(w, r, ret);
  1092 + }
  1093 +
  1094 + if ((ret = _srs_config->raw_set_srs_log_level(value, applied)) != ERROR_SUCCESS) {
  1095 + srs_error("raw api update srs_log_level=%s failed. ret=%d", value.c_str(), ret);
  1096 + return srs_api_response_code(w, r, ret);
  1097 + }
  1098 + } else if (scope == "srs_log_file") {
  1099 + if (value.empty() || !srs_string_starts_with(value, "./", "/tmp/", "/var/") || !srs_string_ends_with(value, ".log")) {
  1100 + ret = ERROR_SYSTEM_CONFIG_RAW_PARAMS;
  1101 + srs_error("raw api update check srs_log_file=%s failed. ret=%d", value.c_str(), ret);
  1102 + return srs_api_response_code(w, r, ret);
  1103 + }
  1104 +
  1105 + if ((ret = _srs_config->raw_set_srs_log_file(value, applied)) != ERROR_SUCCESS) {
  1106 + srs_error("raw api update srs_log_file=%s failed. ret=%d", value.c_str(), ret);
  1107 + return srs_api_response_code(w, r, ret);
  1108 + }
1071 } 1109 }
1072 1110
1073 // whether the config applied. 1111 // whether the config applied.