diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 5d3c539be..3e6c33908 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -133,16 +133,16 @@ namespace _srs_internal srs_freepa(start); } - int SrsConfigBuffer::fullfill(const char* filename) + srs_error_t SrsConfigBuffer::fullfill(const char* filename) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsFileReader reader; // open file reader. if ((ret = reader.open(filename)) != ERROR_SUCCESS) { - srs_error("open conf file error. ret=%d", ret); - return ret; + return srs_error_new(ret, "open file=%s", filename); } // read all. @@ -156,12 +156,10 @@ namespace _srs_internal // read total content from file. ssize_t nread = 0; if ((ret = reader.read(start, filesize, &nread)) != ERROR_SUCCESS) { - srs_error("read file read error. expect %d, actual %d bytes, ret=%d", - filesize, nread, ret); - return ret; + return srs_error_new(ret, "read %d only %d bytes", filesize, nread); } - return ret; + return err; } bool SrsConfigBuffer::empty() @@ -324,9 +322,9 @@ string srs_config_bool2switch(const string& sbool) return sbool == "true"? "on":"off"; } -int srs_config_transform_vhost(SrsConfDirective* root) +srs_error_t srs_config_transform_vhost(SrsConfDirective* root) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; for (int i = 0; i < (int)root->directives.size(); i++) { SrsConfDirective* dir = root->directives.at(i); @@ -358,6 +356,7 @@ int srs_config_transform_vhost(SrsConfDirective* root) // vhost { http_static {} } if (n == "http") { conf->name = "http_static"; + srs_warn("transform: vhost.http => vhost.http_static for %s", dir->name.c_str()); ++it; continue; } @@ -386,12 +385,15 @@ int srs_config_transform_vhost(SrsConfDirective* root) if (n == "refer") { SrsConfDirective* all = refer->get_or_create("all"); all->args = conf->args; + srs_warn("transform: vhost.refer to vhost.refer.all for %s", dir->name.c_str()); } else if (n == "refer_play") { SrsConfDirective* play = refer->get_or_create("play"); play->args = conf->args; + srs_warn("transform: vhost.refer_play to vhost.refer.play for %s", dir->name.c_str()); } else if (n == "refer_publish") { SrsConfDirective* publish = refer->get_or_create("publish"); publish->args = conf->args; + srs_warn("transform: vhost.refer_publish to vhost.refer.publish for %s", dir->name.c_str()); } // remove the old directive. @@ -413,12 +415,14 @@ int srs_config_transform_vhost(SrsConfDirective* root) if (enabled) { SrsConfDirective* mr = publish->get_or_create("mr"); mr->args = enabled->args; + srs_warn("transform: vhost.mr.enabled to vhost.publish.mr.enabled for %s", dir->name.c_str()); } SrsConfDirective* latency = conf->get("latency"); if (latency) { SrsConfDirective* mr_latency = publish->get_or_create("mr_latency"); mr_latency->args = latency->args; + srs_warn("transform: vhost.mr.latency to vhost.publish.mr_latency for %s", dir->name.c_str()); } srs_freep(conf); @@ -437,6 +441,7 @@ int srs_config_transform_vhost(SrsConfDirective* root) SrsConfDirective* firstpkt_timeout = publish->get_or_create("firstpkt_timeout"); firstpkt_timeout->args = conf->args; + srs_warn("transform: vhost.publish_1stpkt_timeout to vhost.publish.firstpkt_timeout for %s", dir->name.c_str()); srs_freep(conf); continue; @@ -454,6 +459,7 @@ int srs_config_transform_vhost(SrsConfDirective* root) SrsConfDirective* normal_timeout = publish->get_or_create("normal_timeout"); normal_timeout->args = conf->args; + srs_warn("transform: vhost.publish_normal_timeout to vhost.publish.normal_timeout for %s", dir->name.c_str()); srs_freep(conf); continue; @@ -467,13 +473,13 @@ int srs_config_transform_vhost(SrsConfDirective* root) // vhost { play { shadow; } } if (n == "time_jitter" || n == "mix_correct" || n == "atc" || n == "atc_auto" || n == "mw_latency" || n == "gop_cache" || n == "queue_length" || n == "send_min_interval" - || n == "reduce_sequence_header" - ) { + || n == "reduce_sequence_header") { it = dir->directives.erase(it); SrsConfDirective* play = dir->get_or_create("play"); SrsConfDirective* shadow = play->get_or_create(conf->name); shadow->args = conf->args; + srs_warn("transform: vhost.%s to vhost.play.%s of %s", n.c_str(), n.c_str(), dir->name.c_str()); srs_freep(conf); continue; @@ -481,15 +487,16 @@ int srs_config_transform_vhost(SrsConfDirective* root) // SRS3.0, change the forward. // SRS1/2: - // vhost { forward; } + // vhost { forward target; } // SRS3+: - // vhost { forward { enabled; destination; } } - if (n == "forward" && conf->directives.empty()) { + // vhost { forward { enabled; destination target; } } + if (n == "forward" && conf->directives.empty() && !conf->args.empty()) { conf->get_or_create("enabled")->set_arg0("on"); SrsConfDirective* destination = conf->get_or_create("destination"); destination->args = conf->args; conf->args.clear(); + srs_warn("transform: vhost.forward to vhost.forward.destination for %s", dir->name.c_str()); ++it; continue; @@ -507,6 +514,7 @@ int srs_config_transform_vhost(SrsConfDirective* root) SrsConfDirective* cluster = dir->get_or_create("cluster"); SrsConfDirective* shadow = cluster->get_or_create(conf->name); shadow->args = conf->args; + srs_warn("transform: vhost.%s to vhost.cluster.%s of %s", n.c_str(), n.c_str(), dir->name.c_str()); srs_freep(conf); continue; @@ -516,7 +524,7 @@ int srs_config_transform_vhost(SrsConfDirective* root) } } - return ret; + return err; } int srs_config_dumps_engine(SrsConfDirective* dir, SrsJsonObject* engine) @@ -788,7 +796,7 @@ bool SrsConfDirective::is_stream_caster() return name == "stream_caster"; } -int SrsConfDirective::parse(SrsConfigBuffer* buffer) +srs_error_t SrsConfDirective::parse(SrsConfigBuffer* buffer) { return parse_conf(buffer, parse_file); } @@ -916,14 +924,14 @@ SrsJsonAny* SrsConfDirective::dumps_arg0_to_boolean() } // see: ngx_conf_parse -int SrsConfDirective::parse_conf(SrsConfigBuffer* buffer, SrsDirectiveType type) +srs_error_t SrsConfDirective::parse_conf(SrsConfigBuffer* buffer, SrsDirectiveType type) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; while (true) { std::vector args; int line_start = 0; - ret = read_token(buffer, args, line_start); + err = read_token(buffer, args, line_start); /** * ret maybe: @@ -933,28 +941,28 @@ int SrsConfDirective::parse_conf(SrsConfigBuffer* buffer, SrsDirectiveType type) * ERROR_SYSTEM_CONFIG_BLOCK_END the '}' found * ERROR_SYSTEM_CONFIG_EOF the config file is done */ - if (ret == ERROR_SYSTEM_CONFIG_INVALID) { - return ret; + if (srs_error_code(err) == ERROR_SYSTEM_CONFIG_INVALID) { + return err; } - if (ret == ERROR_SYSTEM_CONFIG_BLOCK_END) { + if (srs_error_code(err) == ERROR_SYSTEM_CONFIG_BLOCK_END) { if (type != parse_block) { - srs_error("line %d: unexpected \"}\", ret=%d", buffer->line, ret); - return ret; + return srs_error_wrap(err, "line %d: unexpected \"}\"", buffer->line); } - return ERROR_SUCCESS; + + srs_freep(err); + return srs_success; } - if (ret == ERROR_SYSTEM_CONFIG_EOF) { + if (srs_error_code(err) == ERROR_SYSTEM_CONFIG_EOF) { if (type == parse_block) { - srs_error("line %d: unexpected end of file, expecting \"}\", ret=%d", conf_line, ret); - return ret; + return srs_error_wrap(err, "line %d: unexpected end of file, expecting \"}\"", conf_line); } - return ERROR_SUCCESS; + + srs_freep(err); + return srs_success; } if (args.empty()) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("line %d: empty directive. ret=%d", conf_line, ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "line %d: empty directive", conf_line); } // build directive tree. @@ -967,20 +975,22 @@ int SrsConfDirective::parse_conf(SrsConfigBuffer* buffer, SrsDirectiveType type) directives.push_back(directive); - if (ret == ERROR_SYSTEM_CONFIG_BLOCK_START) { - if ((ret = directive->parse_conf(buffer, parse_block)) != ERROR_SUCCESS) { - return ret; + if (srs_error_code(err) == ERROR_SYSTEM_CONFIG_BLOCK_START) { + srs_freep(err); + if ((err = directive->parse_conf(buffer, parse_block)) != srs_success) { + return srs_error_wrap(err, "parse dir"); } } + srs_freep(err); } - return ret; + return err; } // see: ngx_conf_read_token -int SrsConfDirective::read_token(SrsConfigBuffer* buffer, vector& args, int& line_start) +srs_error_t SrsConfDirective::read_token(SrsConfigBuffer* buffer, vector& args, int& line_start) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; char* pstart = buffer->pos; @@ -994,15 +1004,14 @@ int SrsConfDirective::read_token(SrsConfigBuffer* buffer, vector& args, while (true) { if (buffer->empty()) { - ret = ERROR_SYSTEM_CONFIG_EOF; - if (!args.empty() || !last_space) { - srs_error("line %d: unexpected end of file, expecting ; or \"}\"", buffer->line); - return ERROR_SYSTEM_CONFIG_INVALID; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, + "line %d: unexpected end of file, expecting ; or \"}\"", + buffer->line); } srs_trace("config parse complete"); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_EOF, "EOF"); } char ch = *buffer->pos++; @@ -1023,13 +1032,12 @@ int SrsConfDirective::read_token(SrsConfigBuffer* buffer, vector& args, continue; } if (ch == ';') { - return ERROR_SYSTEM_CONFIG_DIRECTIVE; + return srs_error_new(ERROR_SYSTEM_CONFIG_DIRECTIVE, "dir"); } if (ch == '{') { - return ERROR_SYSTEM_CONFIG_BLOCK_START; + return srs_error_new(ERROR_SYSTEM_CONFIG_BLOCK_START, "block"); } - srs_error("line %d: unexpected '%c'", buffer->line, ch); - return ERROR_SYSTEM_CONFIG_INVALID; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "line %d: unexpected '%c'", buffer->line, ch); } // last charecter is space. @@ -1041,22 +1049,19 @@ int SrsConfDirective::read_token(SrsConfigBuffer* buffer, vector& args, switch (ch) { case ';': if (args.size() == 0) { - srs_error("line %d: unexpected ';'", buffer->line); - return ERROR_SYSTEM_CONFIG_INVALID; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "line %d: unexpected ';'", buffer->line); } - return ERROR_SYSTEM_CONFIG_DIRECTIVE; + return srs_error_new(ERROR_SYSTEM_CONFIG_DIRECTIVE, "dir"); case '{': if (args.size() == 0) { - srs_error("line %d: unexpected '{'", buffer->line); - return ERROR_SYSTEM_CONFIG_INVALID; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "line %d: unexpected '{'", buffer->line); } - return ERROR_SYSTEM_CONFIG_BLOCK_START; + return srs_error_new(ERROR_SYSTEM_CONFIG_BLOCK_START, "block"); case '}': if (args.size() != 0) { - srs_error("line %d: unexpected '}'", buffer->line); - return ERROR_SYSTEM_CONFIG_INVALID; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "line %d: unexpected '}'", buffer->line); } - return ERROR_SYSTEM_CONFIG_BLOCK_END; + return srs_error_new(ERROR_SYSTEM_CONFIG_BLOCK_END, "block"); case '#': sharp_comment = 1; continue; @@ -1111,16 +1116,16 @@ int SrsConfDirective::read_token(SrsConfigBuffer* buffer, vector& args, srs_freepa(aword); if (ch == ';') { - return ERROR_SYSTEM_CONFIG_DIRECTIVE; + return srs_error_new(ERROR_SYSTEM_CONFIG_DIRECTIVE, "dir"); } if (ch == '{') { - return ERROR_SYSTEM_CONFIG_BLOCK_START; + return srs_error_new(ERROR_SYSTEM_CONFIG_BLOCK_START, "block"); } } } } - return ret; + return err; } SrsConfig::SrsConfig() @@ -1195,18 +1200,18 @@ srs_error_t SrsConfig::reload() SrsConfig conf; - if ((ret = conf.parse_file(config_file.c_str())) != ERROR_SUCCESS) { - return srs_error_new(ret, "parse file"); + if ((err = conf.parse_file(config_file.c_str())) != srs_success) { + return srs_error_wrap(err, "parse file"); } srs_info("config reloader parse file success."); // transform config to compatible with previous style of config. - if ((ret = srs_config_transform_vhost(conf.root)) != ERROR_SUCCESS) { - return srs_error_new(ret, "transform config"); + if ((err = srs_config_transform_vhost(conf.root)) != srs_success) { + return srs_error_wrap(err, "transform config"); } - if ((ret = conf.check_config()) != ERROR_SUCCESS) { - return srs_error_new(ret, "check config"); + if ((err = conf.check_config()) != srs_success) { + return srs_error_wrap(err, "check config"); } if ((ret = reload_conf(&conf)) != ERROR_SUCCESS) { @@ -1921,9 +1926,9 @@ int SrsConfig::reload_ingest(SrsConfDirective* new_vhost, SrsConfDirective* old_ } // see: ngx_get_options -int SrsConfig::parse_options(int argc, char** argv) +srs_error_t SrsConfig::parse_options(int argc, char** argv) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // argv for (int i = 0; i < argc; i++) { @@ -1937,8 +1942,8 @@ int SrsConfig::parse_options(int argc, char** argv) // config show_help = true; for (int i = 1; i < argc; i++) { - if ((ret = parse_argv(i, argv)) != ERROR_SUCCESS) { - return ret; + if ((err = parse_argv(i, argv)) != srs_success) { + return srs_error_wrap(err, "parse argv"); } } @@ -1960,33 +1965,30 @@ int SrsConfig::parse_options(int argc, char** argv) srs_trace(_srs_version); if (config_file.empty()) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("config file not specified, see help: %s -h, ret=%d", argv[0], ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "no config, read help: %s -h", argv[0]); } - ret = parse_file(config_file.c_str()); - - // transform config to compatible with previous style of config. - if ((ret = srs_config_transform_vhost(root)) != ERROR_SUCCESS) { - srs_error("transform config failed. ret=%d", ret); - return ret; - } + err = parse_file(config_file.c_str()); if (test_conf) { // the parse_file never check the config, // we check it when user requires check config file. - if (ret == ERROR_SUCCESS) { - ret = check_config(); + if (err == srs_success && (err = srs_config_transform_vhost(root)) == srs_success) { + if (err == srs_success && (err = check_config()) == srs_success) { + srs_trace("config file is ok"); + exit(0); + } } - if (ret == ERROR_SUCCESS) { - srs_trace("config file is ok"); - exit(0); - } else { - srs_error("config file is invalid"); - exit(ret); - } + srs_error("invalid config, %s", srs_error_desc(err).c_str()); + int ret = srs_error_code(err); + srs_freep(err); + exit(ret); + } + + // transform config to compatible with previous style of config. + if ((err = srs_config_transform_vhost(root)) != srs_success) { + return srs_error_wrap(err, "transform"); } //////////////////////////////////////////////////////////////////////// @@ -1995,9 +1997,7 @@ int SrsConfig::parse_options(int argc, char** argv) if (true) { std::string log_filename = this->get_log_file(); if (get_log_tank_file() && log_filename.empty()) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("must specifies the file to write log to. ret=%d", ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "no log file"); } if (get_log_tank_file()) { srs_trace("write log to file %s", log_filename.c_str()); @@ -2008,19 +2008,17 @@ int SrsConfig::parse_options(int argc, char** argv) } } - return ret; + return err; } -int SrsConfig::initialize_cwd() +srs_error_t SrsConfig::initialize_cwd() { - int ret = ERROR_SUCCESS; - // cwd char cwd[256]; getcwd(cwd, sizeof(cwd)); _cwd = cwd; - return ret; + return srs_success; } srs_error_t SrsConfig::persistence() @@ -3405,15 +3403,15 @@ string SrsConfig::config() return config_file; } -int SrsConfig::parse_argv(int& i, char** argv) +srs_error_t SrsConfig::parse_argv(int& i, char** argv) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; char* p = argv[i]; if (*p++ != '-') { show_help = true; - return ret; + return err; } while (*p) { @@ -3436,9 +3434,7 @@ int SrsConfig::parse_argv(int& i, char** argv) dolphin_rtmp_port = argv[i]; continue; } - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("option \"-p\" requires params, ret=%d", ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "-p requires params"); case 'x': dolphin = true; if (*p) { @@ -3449,9 +3445,7 @@ int SrsConfig::parse_argv(int& i, char** argv) dolphin_http_port = argv[i]; continue; } - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("option \"-x\" requires params, ret=%d", ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "-x requires params"); case 'v': case 'V': show_help = false; @@ -3472,17 +3466,14 @@ int SrsConfig::parse_argv(int& i, char** argv) config_file = argv[i]; continue; } - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("option \"-c\" requires parameter, ret=%d", ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "-c requires params"); default: - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("invalid option: \"%c\", see help: %s -h, ret=%d", *(p - 1), argv[0], ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "invalid option: \"%c\", read help: %s -h", + *(p - 1), argv[0]); } } - return ret; + return err; } void SrsConfig::print_help(char** argv) @@ -3517,43 +3508,47 @@ void SrsConfig::print_help(char** argv) argv[0], argv[0], argv[0], argv[0]); } -int SrsConfig::parse_file(const char* filename) +srs_error_t SrsConfig::parse_file(const char* filename) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; config_file = filename; if (config_file.empty()) { - return ERROR_SYSTEM_CONFIG_INVALID; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "empty config"); } SrsConfigBuffer buffer; - if ((ret = buffer.fullfill(config_file.c_str())) != ERROR_SUCCESS) { - return ret; + if ((err = buffer.fullfill(config_file.c_str())) != srs_success) { + return srs_error_wrap(err, "buffer fullfil"); } - return parse_buffer(&buffer); + if ((err = parse_buffer(&buffer)) != srs_success) { + return srs_error_wrap(err, "parse buffer"); + } + + return err; } -int SrsConfig::check_config() +srs_error_t SrsConfig::check_config() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; - if ((ret = check_normal_config()) != ERROR_SUCCESS) { - return ret; + if ((err = check_normal_config()) != srs_success) { + return srs_error_wrap(err, "check normal"); } - if ((ret = check_number_connections()) != ERROR_SUCCESS) { - return ret; + if ((err = check_number_connections()) != srs_success) { + return srs_error_wrap(err, "check connections"); } - return ret; + return err; } -int SrsConfig::check_normal_config() +srs_error_t SrsConfig::check_normal_config() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; srs_trace("srs checking config..."); @@ -3561,9 +3556,7 @@ int SrsConfig::check_normal_config() // check empty //////////////////////////////////////////////////////////////////////// if (root->directives.size() == 0) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("conf is empty, ret=%d", ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "conf is empty"); } //////////////////////////////////////////////////////////////////////// @@ -3579,9 +3572,7 @@ int SrsConfig::check_normal_config() && n != "http_server" && n != "stream_caster" && n != "kafka" && n != "utc_time" && n != "work_dir" && n != "asprocess" ) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported directive %s, ret=%d", n.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal directive %s", n.c_str()); } } if (true) { @@ -3590,18 +3581,14 @@ int SrsConfig::check_normal_config() SrsConfDirective* obj = conf->at(i); string n = obj->name; if (n != "enabled" && n != "listen" && n != "crossdomain" && n != "raw_api") { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported http_api directive %s, ret=%d", n.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal http_api.%s", n.c_str()); } if (n == "raw_api") { for (int j = 0; j < (int)obj->directives.size(); j++) { string m = obj->at(j)->name; if (m != "enabled" && m != "allow_reload" && m != "allow_query" && m != "allow_update") { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported http_api.raw_api directive %s, ret=%d", m.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal http_api.raw_api.%s", m.c_str()); } } } @@ -3612,9 +3599,7 @@ int SrsConfig::check_normal_config() for (int i = 0; conf && i < (int)conf->directives.size(); i++) { string n = conf->at(i)->name; if (n != "enabled" && n != "listen" && n != "dir" && n != "crossdomain") { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported http_stream directive %s, ret=%d", n.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal http_stream.%s", n.c_str()); } } } @@ -3623,9 +3608,7 @@ int SrsConfig::check_normal_config() for (int i = 0; conf && i < (int)conf->directives.size(); i++) { string n = conf->at(i)->name; if (n != "enabled" && n != "brokers" && n != "topic") { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported kafka directive %s, ret=%d", n.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal kafka.%s", n.c_str()); } } } @@ -3634,11 +3617,8 @@ int SrsConfig::check_normal_config() for (int i = 0; conf && i < (int)conf->directives.size(); i++) { string n = conf->at(i)->name; if (n != "enabled" && n != "interval" && n != "url" - && n != "device_id" && n != "summaries" - ) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported heartbeat directive %s, ret=%d", n.c_str(), ret); - return ret; + && n != "device_id" && n != "summaries") { + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal heartbeat.%s", n.c_str()); } } } @@ -3647,9 +3627,7 @@ int SrsConfig::check_normal_config() for (int i = 0; conf && i < (int)conf->directives.size(); i++) { string n = conf->at(i)->name; if (n != "network" && n != "disk") { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported stats directive %s, ret=%d", n.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal stats.%s", n.c_str()); } } } @@ -3660,16 +3638,12 @@ int SrsConfig::check_normal_config() if (true) { vector listens = get_listens(); if (listens.size() <= 0) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("directive \"listen\" is empty, ret=%d", ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "listen requires params"); } for (int i = 0; i < (int)listens.size(); i++) { string port = listens[i]; if (port.empty() || ::atoi(port.c_str()) <= 0) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("directive listen invalid, port=%s, ret=%d", port.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "listen.port=%d is invalid", port.c_str()); } } } @@ -3678,29 +3652,22 @@ int SrsConfig::check_normal_config() // check heartbeat //////////////////////////////////////////////////////////////////////// if (get_heartbeat_interval() <= 0) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("directive heartbeat interval invalid, interval=%" PRId64 ", ret=%d", - get_heartbeat_interval(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "invalid heartbeat.interval=%" PRId64, + get_heartbeat_interval()); } //////////////////////////////////////////////////////////////////////// // check stats //////////////////////////////////////////////////////////////////////// if (get_stats_network() < 0) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("directive stats network invalid, network=%d, ret=%d", - get_stats_network(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "invalid stats.network=%d", get_stats_network()); } if (true) { vector ips = srs_get_local_ipv4_ips(); int index = get_stats_network(); if (index >= (int)ips.size()) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("stats network invalid, total local ip count=%d, index=%d, ret=%d", - (int)ips.size(), index, ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "invalid stats.network=%d of %d", + index, (int)ips.size()); } srs_warn("stats network use index=%d, ip=%s", index, ips.at(index).c_str()); } @@ -3722,20 +3689,14 @@ int SrsConfig::check_normal_config() // check http api //////////////////////////////////////////////////////////////////////// if (get_http_api_listen().empty()) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("directive http_api listen invalid, listen=%s, ret=%d", - get_http_api_listen().c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "http_api.listen requires params"); } //////////////////////////////////////////////////////////////////////// // check http stream //////////////////////////////////////////////////////////////////////// if (get_http_stream_listen().empty()) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("directive http_stream listen invalid, listen=%s, ret=%d", - get_http_stream_listen().c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "http_stream.listen requires params"); } //////////////////////////////////////////////////////////////////////// @@ -3744,9 +3705,7 @@ int SrsConfig::check_normal_config() if (true) { std::string log_filename = this->get_log_file(); if (get_log_tank_file() && log_filename.empty()) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("must specifies the file to write log to. ret=%d", ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "log file is empty"); } if (get_log_tank_file()) { srs_trace("write log to file %s", log_filename.c_str()); @@ -3767,11 +3726,8 @@ int SrsConfig::check_normal_config() SrsConfDirective* conf = stream_caster->at(i); string n = conf->name; if (n != "enabled" && n != "caster" && n != "output" - && n != "listen" && n != "rtp_port_min" && n != "rtp_port_max" - ) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported stream_caster directive %s, ret=%d", n.c_str(), ret); - return ret; + && n != "listen" && n != "rtp_port_min" && n != "rtp_port_max") { + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal stream_caster.%s", n.c_str()); } } } @@ -3792,96 +3748,73 @@ int SrsConfig::check_normal_config() && n != "play" && n != "publish" && n != "cluster" && n != "security" && n != "http_remux" && n != "dash" && n != "http_static" && n != "hds" && n != "exec" - && n != "in_ack_size" && n != "out_ack_size" - ) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported vhost directive %s, ret=%d", n.c_str(), ret); - return ret; + && n != "in_ack_size" && n != "out_ack_size") { + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.%s", n.c_str()); } // for each sub directives of vhost. if (n == "dvr") { for (int j = 0; j < (int)conf->directives.size(); j++) { string m = conf->at(j)->name; if (m != "enabled" && m != "dvr_apply" && m != "dvr_path" && m != "dvr_plan" - && m != "dvr_duration" && m != "dvr_wait_keyframe" && m != "time_jitter" - ) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported vhost dvr directive %s, ret=%d", m.c_str(), ret); - return ret; + && m != "dvr_duration" && m != "dvr_wait_keyframe" && m != "time_jitter") { + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.dvr.%s of %s", m.c_str(), vhost->arg0().c_str()); } } } else if (n == "refer") { for (int j = 0; j < (int)conf->directives.size(); j++) { string m = conf->at(j)->name; if (m != "enabled" && m != "all" && m != "publish" && m != "play") { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported vhost refer directive %s, ret=%d", m.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.refer.%s of %s", m.c_str(), vhost->arg0().c_str()); } } } else if (n == "exec") { for (int j = 0; j < (int)conf->directives.size(); j++) { string m = conf->at(j)->name; if (m != "enabled" && m != "publish") { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported vhost exec directive %s, ret=%d", m.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.exec.%s of %s", m.c_str(), vhost->arg0().c_str()); } } } else if (n == "play") { for (int j = 0; j < (int)conf->directives.size(); j++) { string m = conf->at(j)->name; if (m != "time_jitter" && m != "mix_correct" && m != "atc" && m != "atc_auto" && m != "mw_latency" - && m != "gop_cache" && m != "queue_length" && m != "send_min_interval" && m != "reduce_sequence_header" - ) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported vhost play directive %s, ret=%d", m.c_str(), ret); - return ret; + && m != "gop_cache" && m != "queue_length" && m != "send_min_interval" && m != "reduce_sequence_header") { + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.play.%s of %s", m.c_str(), vhost->arg0().c_str()); } } } else if (n == "cluster") { for (int j = 0; j < (int)conf->directives.size(); j++) { string m = conf->at(j)->name; if (m != "mode" && m != "origin" && m != "token_traverse" && m != "vhost" && m != "debug_srs_upnode") { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported vhost cluster directive %s, ret=%d", m.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.cluster.%s of %s", m.c_str(), vhost->arg0().c_str()); } } } else if (n == "publish") { for (int j = 0; j < (int)conf->directives.size(); j++) { string m = conf->at(j)->name; if (m != "mr" && m != "mr_latency" && m != "firstpkt_timeout" && m != "normal_timeout" && m != "parse_sps") { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported vhost publish directive %s, ret=%d", m.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.publish.%s of %s", m.c_str(), vhost->arg0().c_str()); } } } else if (n == "ingest") { for (int j = 0; j < (int)conf->directives.size(); j++) { string m = conf->at(j)->name; if (m != "enabled" && m != "input" && m != "ffmpeg" && m != "engine") { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported vhost ingest directive %s, ret=%d", m.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.ingest.%s of %s", m.c_str(), vhost->arg0().c_str()); } } } else if (n == "http_static") { for (int j = 0; j < (int)conf->directives.size(); j++) { string m = conf->at(j)->name; if (m != "enabled" && m != "mount" && m != "dir") { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported vhost http directive %s, ret=%d", m.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.http_static.%s of %s", m.c_str(), vhost->arg0().c_str()); } } } else if (n == "http_remux") { for (int j = 0; j < (int)conf->directives.size(); j++) { string m = conf->at(j)->name; if (m != "enabled" && m != "mount" && m != "fast_cache") { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported vhost http_remux directive %s, ret=%d", m.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.http_remux.%s of %s", m.c_str(), vhost->arg0().c_str()); } } } else if (n == "dash") { @@ -3889,9 +3822,7 @@ int SrsConfig::check_normal_config() string m = conf->at(j)->name; if (m != "enabled" && m != "dash_fragment" && m != "dash_update_period" && m != "dash_timeshift" && m != "dash_path" && m != "dash_mpd_file") { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("Illegal directive %s in vhost.dash, ret=%d", m.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.dash.%s of %s", m.c_str(), vhost->arg0().c_str()); } } } else if (n == "hls") { @@ -3900,16 +3831,13 @@ int SrsConfig::check_normal_config() if (m != "enabled" && m != "hls_entry_prefix" && m != "hls_path" && m != "hls_fragment" && m != "hls_window" && m != "hls_on_error" && m != "hls_storage" && m != "hls_mount" && m != "hls_td_ratio" && m != "hls_aof_ratio" && m != "hls_acodec" && m != "hls_vcodec" && m != "hls_m3u8_file" && m != "hls_ts_file" && m != "hls_ts_floor" && m != "hls_cleanup" && m != "hls_nb_notify" - && m != "hls_wait_keyframe" && m != "hls_dispose" - ) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported vhost hls directive %s, ret=%d", m.c_str(), ret); - return ret; + && m != "hls_wait_keyframe" && m != "hls_dispose") { + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.hls.%s of %s", m.c_str(), vhost->arg0().c_str()); } // TODO: FIXME: remove it in future. if (m == "hls_storage" || m == "hls_mount") { - srs_warn("HLS RAM is removed from SRS2 to SRS3+, please read https://github.com/ossrs/srs/issues/513."); + srs_warn("HLS RAM is removed in SRS3+, read https://github.com/ossrs/srs/issues/513."); } } } else if (n == "http_hooks") { @@ -3917,20 +3845,15 @@ int SrsConfig::check_normal_config() string m = conf->at(j)->name; if (m != "enabled" && m != "on_connect" && m != "on_close" && m != "on_publish" && m != "on_unpublish" && m != "on_play" && m != "on_stop" - && m != "on_dvr" && m != "on_hls" && m != "on_hls_notify" - ) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported vhost http_hooks directive %s, ret=%d", m.c_str(), ret); - return ret; + && m != "on_dvr" && m != "on_hls" && m != "on_hls_notify") { + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.http_hooks.%s of %s", m.c_str(), vhost->arg0().c_str()); } } } else if (n == "forward") { for (int j = 0; j < (int)conf->directives.size(); j++) { string m = conf->at(j)->name; if (m != "enabled" && m != "destination") { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported vhost forward directive %s, ret=%d", m.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.forward.%s of %s", m.c_str(), vhost->arg0().c_str()); } } } else if (n == "security") { @@ -3938,9 +3861,7 @@ int SrsConfig::check_normal_config() SrsConfDirective* security = conf->at(j); string m = security->name.c_str(); if (m != "enabled" && m != "deny" && m != "allow") { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported vhost security directive %s, ret=%d", m.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.security.%s of %s", m.c_str(), vhost->arg0().c_str()); } } } else if (n == "transcode") { @@ -3948,9 +3869,7 @@ int SrsConfig::check_normal_config() SrsConfDirective* trans = conf->at(j); string m = trans->name.c_str(); if (m != "enabled" && m != "ffmpeg" && m != "engine") { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported vhost transcode directive %s, ret=%d", m.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.transcode.%s of %s", m.c_str(), vhost->arg0().c_str()); } if (m == "engine") { for (int k = 0; k < (int)trans->directives.size(); k++) { @@ -3960,11 +3879,8 @@ int SrsConfig::check_normal_config() && e != "vthreads" && e != "vprofile" && e != "vpreset" && e != "vparams" && e != "acodec" && e != "abitrate" && e != "asample_rate" && e != "achannels" && e != "aparams" && e != "output" && e != "perfile" - && e != "iformat" && e != "oformat" - ) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported vhost transcode engine directive %s, ret=%d", e.c_str(), ret); - return ret; + && e != "iformat" && e != "oformat") { + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.transcode.engine.%s of %s", m.c_str(), vhost->arg0().c_str()); } } } @@ -3973,9 +3889,7 @@ int SrsConfig::check_normal_config() for (int j = 0; j < (int)conf->directives.size(); j++) { string m = conf->at(j)->name; if (m != "enabled" && m != "key" && m != "interval" && m != "limit_kbps") { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("unsupported vhost bandcheck directive %s, ret=%d", m.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal vhost.bandcheck.%s of %s", m.c_str(), vhost->arg0().c_str()); } } } @@ -3995,10 +3909,8 @@ int SrsConfig::check_normal_config() std::string id = conf->arg0(); for (int k = 0; k < (int)ids.size(); k++) { if (id == ids.at(k)) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("directive \"ingest\" id duplicated, vhost=%s, id=%s, ret=%d", - vhost->name.c_str(), id.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "ingest id=%s exists for %s", + id.c_str(), vhost->arg0().c_str()); } } ids.push_back(id); @@ -4009,38 +3921,30 @@ int SrsConfig::check_normal_config() // check chunk size //////////////////////////////////////////////////////////////////////// if (get_global_chunk_size() < SRS_CONSTS_RTMP_MIN_CHUNK_SIZE - || get_global_chunk_size() > SRS_CONSTS_RTMP_MAX_CHUNK_SIZE - ) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("directive chunk_size invalid, chunk_size=%d, must in [%d, %d], ret=%d", - get_global_chunk_size(), SRS_CONSTS_RTMP_MIN_CHUNK_SIZE, - SRS_CONSTS_RTMP_MAX_CHUNK_SIZE, ret); - return ret; + || get_global_chunk_size() > SRS_CONSTS_RTMP_MAX_CHUNK_SIZE) { + srs_warn("chunk_size=%s should be in [%d, %d]", get_global_chunk_size(), + SRS_CONSTS_RTMP_MIN_CHUNK_SIZE, SRS_CONSTS_RTMP_MAX_CHUNK_SIZE); } for (int i = 0; i < (int)vhosts.size(); i++) { SrsConfDirective* vhost = vhosts[i]; if (get_chunk_size(vhost->arg0()) < SRS_CONSTS_RTMP_MIN_CHUNK_SIZE - || get_chunk_size(vhost->arg0()) > SRS_CONSTS_RTMP_MAX_CHUNK_SIZE - ) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("directive vhost %s chunk_size invalid, chunk_size=%d, must in [%d, %d], ret=%d", - vhost->arg0().c_str(), get_chunk_size(vhost->arg0()), SRS_CONSTS_RTMP_MIN_CHUNK_SIZE, - SRS_CONSTS_RTMP_MAX_CHUNK_SIZE, ret); - return ret; + || get_chunk_size(vhost->arg0()) > SRS_CONSTS_RTMP_MAX_CHUNK_SIZE) { + srs_warn("chunk_size=%s of %s should be in [%d, %d]", get_global_chunk_size(), vhost->arg0().c_str(), + SRS_CONSTS_RTMP_MIN_CHUNK_SIZE, SRS_CONSTS_RTMP_MAX_CHUNK_SIZE); } } for (int i = 0; i < (int)vhosts.size(); i++) { SrsConfDirective* vhost = vhosts[i]; srs_assert(vhost != NULL); if (get_dvr_enabled(vhost->arg0())) { - srs_warn("dvr of vhost %s is disabled by configure", vhost->arg0().c_str()); + srs_warn("can't enable vhost.dvr of %s", vhost->arg0().c_str()); } if (get_hls_enabled(vhost->arg0())) { - srs_warn("hls of vhost %s is disabled by configure", vhost->arg0().c_str()); + srs_warn("can't enable vhost.hls of %s", vhost->arg0().c_str()); } #ifndef SRS_AUTO_TRANSCODE if (get_transcode_enabled(get_transcode(vhost->arg0(), ""))) { - srs_warn("transcode of vhost %s is disabled by configure", vhost->arg0().c_str()); + srs_warn("can't enable vhost.transcode of %s", vhost->arg0().c_str()); } #endif #ifndef SRS_AUTO_INGEST @@ -4048,9 +3952,8 @@ int SrsConfig::check_normal_config() for (int j = 0; j < (int)ingesters.size(); j++) { SrsConfDirective* ingest = ingesters[j]; if (get_ingest_enabled(ingest)) { - srs_warn("ingest %s of vhost %s is disabled by configure", - ingest->arg0().c_str(), vhost->arg0().c_str() - ); + srs_warn("cant' enable vhost.ingest.%s of %s", + ingest->arg0().c_str(), vhost->arg0().c_str()); } } #endif @@ -4059,25 +3962,21 @@ int SrsConfig::check_normal_config() // asprocess conflict with daemon if (get_asprocess() && get_deamon()) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("daemon conflict with asprocess, ret=%d", ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "daemon conflicts with asprocess"); } - return ret; + return err; } -int SrsConfig::check_number_connections() +srs_error_t SrsConfig::check_number_connections() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; //////////////////////////////////////////////////////////////////////// // check max connections //////////////////////////////////////////////////////////////////////// if (get_max_connections() <= 0) { - ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("directive max_connections invalid, max_connections=%d, ret=%d", get_max_connections(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "max_connections=%d is invalid", get_max_connections()); } // check max connections of system limits @@ -4104,27 +4003,27 @@ int SrsConfig::check_number_connections() // for each play connections, we open a pipe(2fds) to convert SrsConsumver to io, // refine performance, @see: https://github.com/ossrs/srs/issues/194 if (nb_total >= max_open_files) { - ret = ERROR_SYSTEM_CONFIG_INVALID; srs_error("invalid max_connections=%d, required=%d, system limit to %d, " - "total=%d(max_connections=%d, nb_consumed_fds=%d), ret=%d. " + "total=%d(max_connections=%d, nb_consumed_fds=%d). " "you can change max_connections from %d to %d, or " "you can login as root and set the limit: ulimit -HSn %d", nb_connections, nb_total + 100, max_open_files, nb_total, nb_connections, nb_consumed_fds, - ret, nb_connections, nb_canbe, nb_total + 100); - return ret; + nb_connections, nb_canbe, nb_total + 100); + return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "%d exceed max open files=%d", + nb_total, max_open_files); } } - return ret; + return err; } -int SrsConfig::parse_buffer(SrsConfigBuffer* buffer) +srs_error_t SrsConfig::parse_buffer(SrsConfigBuffer* buffer) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; - if ((ret = root->parse(buffer)) != ERROR_SUCCESS) { - return ret; + if ((err = root->parse(buffer)) != srs_success) { + return srs_error_wrap(err, "root parse"); } // mock by dolphin mode. @@ -4144,7 +4043,7 @@ int SrsConfig::parse_buffer(SrsConfigBuffer* buffer) set_config_directive(root, "srs_log_tank", "console"); } - return ret; + return err; } string SrsConfig::cwd() diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 6dacca915..d59f0c382 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -73,7 +73,7 @@ namespace _srs_internal /** * fullfill the buffer with content of file specified by filename. */ - virtual int fullfill(const char* filename); + virtual srs_error_t fullfill(const char* filename); /** * whether buffer is empty. */ @@ -119,7 +119,7 @@ extern std::string srs_config_bool2switch(const std::string& sbool); * so we must transform the vhost directive anytime load the config. * @param root the root directive to transform, in and out parameter. */ -extern int srs_config_transform_vhost(SrsConfDirective* root); +extern srs_error_t srs_config_transform_vhost(SrsConfDirective* root); // @global config object. extern SrsConfig* _srs_config; @@ -239,7 +239,7 @@ public: /** * parse config directive from file buffer. */ - virtual int parse(_srs_internal::SrsConfigBuffer* buffer); + virtual srs_error_t parse(_srs_internal::SrsConfigBuffer* buffer); /** * persistence the directive to writer. * @param level, the root is level0, all its directives are level1, and so on. @@ -277,7 +277,7 @@ private: * 2. initialize the directive by args, args[0] is name, args[1-N] is args of directive, * 3. if ret flag indicates there are child-directives, read_conf(directive, block) recursively. */ - virtual int parse_conf(_srs_internal::SrsConfigBuffer* buffer, SrsDirectiveType type); + virtual srs_error_t parse_conf(_srs_internal::SrsConfigBuffer* buffer, SrsDirectiveType type); /** * read a token from buffer. * a token, is the directive args and a flag indicates whether has child-directives. @@ -285,7 +285,7 @@ private: * @param line_start, the actual start line of directive. * @return, an error code indicates error or has child-directives. */ - virtual int read_token(_srs_internal::SrsConfigBuffer* buffer, std::vector& args, int& line_start); + virtual srs_error_t read_token(_srs_internal::SrsConfigBuffer* buffer, std::vector& args, int& line_start); }; /** @@ -412,12 +412,12 @@ public: /** * parse the cli, the main(argc,argv) function. */ - virtual int parse_options(int argc, char** argv); + virtual srs_error_t parse_options(int argc, char** argv); /** * initialize the cwd for server, * because we may change the workdir. */ - virtual int initialize_cwd(); + virtual srs_error_t initialize_cwd(); /** * persistence current config to file. */ @@ -530,7 +530,7 @@ private: /** * parse each argv. */ - virtual int parse_argv(int& i, char** argv); + virtual srs_error_t parse_argv(int& i, char** argv); /** * print help and exit. */ @@ -539,21 +539,21 @@ public: /** * parse the config file, which is specified by cli. */ - virtual int parse_file(const char* filename); + virtual srs_error_t parse_file(const char* filename); /** * check the parsed config. */ - virtual int check_config(); + virtual srs_error_t check_config(); protected: - virtual int check_normal_config(); - virtual int check_number_connections(); + virtual srs_error_t check_normal_config(); + virtual srs_error_t check_number_connections(); protected: /** * parse config from the buffer. * @param buffer, the config buffer, user must delete it. * @remark, use protected for the utest to override with mock. */ - virtual int parse_buffer(_srs_internal::SrsConfigBuffer* buffer); + virtual srs_error_t parse_buffer(_srs_internal::SrsConfigBuffer* buffer); // global env public: /** diff --git a/trunk/src/app/srs_app_log.cpp b/trunk/src/app/srs_app_log.cpp index 78149a186..f572ef716 100644 --- a/trunk/src/app/srs_app_log.cpp +++ b/trunk/src/app/srs_app_log.cpp @@ -68,10 +68,8 @@ SrsFastLog::~SrsFastLog() } } -int SrsFastLog::initialize() +srs_error_t SrsFastLog::initialize() { - int ret = ERROR_SUCCESS; - if (_srs_config) { _srs_config->subscribe(this); @@ -80,7 +78,7 @@ int SrsFastLog::initialize() utc = _srs_config->get_utc_time(); } - return ret; + return srs_success; } void SrsFastLog::reopen() diff --git a/trunk/src/app/srs_app_log.hpp b/trunk/src/app/srs_app_log.hpp index 7835fbdf7..746ee4bf3 100644 --- a/trunk/src/app/srs_app_log.hpp +++ b/trunk/src/app/srs_app_log.hpp @@ -56,7 +56,7 @@ public: virtual ~SrsFastLog(); // interface ISrsLog public: - virtual int initialize(); + virtual srs_error_t initialize(); virtual void reopen(); virtual void verbose(const char* tag, int context_id, const char* fmt, ...); virtual void info(const char* tag, int context_id, const char* fmt, ...); diff --git a/trunk/src/kernel/srs_kernel_log.cpp b/trunk/src/kernel/srs_kernel_log.cpp index acfcca1e6..820fd4f57 100644 --- a/trunk/src/kernel/srs_kernel_log.cpp +++ b/trunk/src/kernel/srs_kernel_log.cpp @@ -33,9 +33,9 @@ ISrsLog::~ISrsLog() { } -int ISrsLog::initialize() +srs_error_t ISrsLog::initialize() { - return ERROR_SUCCESS; + return srs_success; } void ISrsLog::reopen() diff --git a/trunk/src/kernel/srs_kernel_log.hpp b/trunk/src/kernel/srs_kernel_log.hpp index 39b5069d0..17e9a0b4c 100644 --- a/trunk/src/kernel/srs_kernel_log.hpp +++ b/trunk/src/kernel/srs_kernel_log.hpp @@ -65,7 +65,7 @@ public: /** * initialize log utilities. */ - virtual int initialize(); + virtual srs_error_t initialize(); /** * reopen the log file for log rotate. */ diff --git a/trunk/src/main/srs_main_server.cpp b/trunk/src/main/srs_main_server.cpp index 44b733c54..dbd2b4d9e 100644 --- a/trunk/src/main/srs_main_server.cpp +++ b/trunk/src/main/srs_main_server.cpp @@ -67,9 +67,9 @@ extern const char* _srs_version; /** * main entrance. */ -int main(int argc, char** argv) +srs_error_t do_main(int argc, char** argv) { - int ret = ERROR_SUCCESS; + srs_error_t err= srs_success; // TODO: support both little and big endian. srs_assert(srs_is_little_endian()); @@ -98,23 +98,23 @@ int main(int argc, char** argv) // never use srs log(srs_trace, srs_error, etc) before config parse the option, // which will load the log config and apply it. - if ((ret = _srs_config->parse_options(argc, argv)) != ERROR_SUCCESS) { - return ret; + if ((err = _srs_config->parse_options(argc, argv)) != srs_success) { + return srs_error_wrap(err, "config parse options"); } // change the work dir and set cwd. + int r0 = 0; string cwd = _srs_config->get_work_dir(); - if (!cwd.empty() && cwd != "./" && (ret = chdir(cwd.c_str())) != ERROR_SUCCESS) { - srs_error("change cwd to %s failed. ret=%d", cwd.c_str(), ret); - return ret; + if (!cwd.empty() && cwd != "./" && (r0 = chdir(cwd.c_str())) == -1) { + return srs_error_new(-1, "chdir to %s, r0=%d", cwd.c_str(), r0); } - if ((ret = _srs_config->initialize_cwd()) != ERROR_SUCCESS) { - return ret; + if ((err = _srs_config->initialize_cwd()) != srs_success) { + return srs_error_wrap(err, "config cwd"); } // config parsed, initialize log. - if ((ret = _srs_log->initialize()) != ERROR_SUCCESS) { - return ret; + if ((err = _srs_log->initialize()) != srs_success) { + return srs_error_wrap(err, "log initialize"); } // config already applied to log. @@ -172,8 +172,8 @@ int main(int argc, char** argv) } // we check the config when the log initialized. - if ((ret = _srs_config->check_config()) != ERROR_SUCCESS) { - return ret; + if ((err = _srs_config->check_config()) != srs_success) { + return srs_error_wrap(err, "check config"); } // features @@ -182,12 +182,21 @@ int main(int argc, char** argv) SrsServer* svr = new SrsServer(); SrsAutoFree(SrsServer, svr); - srs_error_t err = run(svr); + if ((err = run(svr)) != srs_success) { + return srs_error_wrap(err, "run"); + } + + return err; +} + +int main(int argc, char** argv) { + srs_error_t err = do_main(argc, argv); + if (err != srs_success) { srs_error("Failed, %s", srs_error_desc(err).c_str()); } - ret = srs_error_code(err); + int ret = srs_error_code(err); srs_freep(err); return ret; } diff --git a/trunk/src/service/srs_service_log.cpp b/trunk/src/service/srs_service_log.cpp index 768cc9396..b193e81bf 100644 --- a/trunk/src/service/srs_service_log.cpp +++ b/trunk/src/service/srs_service_log.cpp @@ -91,9 +91,9 @@ SrsConsoleLog::~SrsConsoleLog() srs_freepa(buffer); } -int SrsConsoleLog::initialize() +srs_error_t SrsConsoleLog::initialize() { - return ERROR_SUCCESS; + return srs_success; } void SrsConsoleLog::reopen() diff --git a/trunk/src/service/srs_service_log.hpp b/trunk/src/service/srs_service_log.hpp index f13e94f5c..1f742c239 100644 --- a/trunk/src/service/srs_service_log.hpp +++ b/trunk/src/service/srs_service_log.hpp @@ -65,7 +65,7 @@ public: virtual ~SrsConsoleLog(); // interface ISrsLog public: - virtual int initialize(); + virtual srs_error_t initialize(); virtual void reopen(); virtual void verbose(const char* tag, int context_id, const char* fmt, ...); virtual void info(const char* tag, int context_id, const char* fmt, ...); diff --git a/trunk/src/utest/srs_utest_config.cpp b/trunk/src/utest/srs_utest_config.cpp index d15b95035..af4164097 100644 --- a/trunk/src/utest/srs_utest_config.cpp +++ b/trunk/src/utest/srs_utest_config.cpp @@ -50,9 +50,9 @@ MockSrsConfigBuffer::~MockSrsConfigBuffer() { } -int MockSrsConfigBuffer::fullfill(const char* /*filename*/) +srs_error_t MockSrsConfigBuffer::fullfill(const char* /*filename*/) { - return ERROR_SUCCESS; + return srs_success; } MockSrsConfig::MockSrsConfig() @@ -63,22 +63,25 @@ MockSrsConfig::~MockSrsConfig() { } -int MockSrsConfig::parse(string buf) +srs_error_t MockSrsConfig::parse(string buf) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; MockSrsConfigBuffer buffer(buf); - if ((ret = parse_buffer(&buffer)) != ERROR_SUCCESS) { - return ret; + if ((err = parse_buffer(&buffer)) != srs_success) { + return srs_error_wrap(err, "parse buffer"); } - if ((ret = srs_config_transform_vhost(root)) != ERROR_SUCCESS) { - srs_error("transform config failed. ret=%d", ret); - return ret; + if ((err = srs_config_transform_vhost(root)) != srs_success) { + return srs_error_wrap(err, "transform config"); } - return check_normal_config(); + if ((err = check_normal_config()) != srs_success) { + return srs_error_wrap(err, "check normal config"); + } + + return err; } #ifdef ENABLE_UTEST_CONFIG diff --git a/trunk/src/utest/srs_utest_config.hpp b/trunk/src/utest/srs_utest_config.hpp index 88128fadf..6d5d388a7 100644 --- a/trunk/src/utest/srs_utest_config.hpp +++ b/trunk/src/utest/srs_utest_config.hpp @@ -41,7 +41,7 @@ public: MockSrsConfigBuffer(std::string buf); virtual ~MockSrsConfigBuffer(); public: - virtual int fullfill(const char* filename); + virtual srs_error_t fullfill(const char* filename); }; class MockSrsConfig : public SrsConfig @@ -50,7 +50,7 @@ public: MockSrsConfig(); virtual ~MockSrsConfig(); public: - virtual int parse(std::string buf); + virtual srs_error_t parse(std::string buf); }; #endif diff --git a/trunk/src/utest/srs_utest_reload.cpp b/trunk/src/utest/srs_utest_reload.cpp index 1ec254db6..a5d4bce6e 100644 --- a/trunk/src/utest/srs_utest_reload.cpp +++ b/trunk/src/utest/srs_utest_reload.cpp @@ -283,16 +283,21 @@ MockSrsReloadConfig::~MockSrsReloadConfig() { } -int MockSrsReloadConfig::do_reload(string buf) +srs_error_t MockSrsReloadConfig::do_reload(string buf) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; MockSrsReloadConfig conf; - if ((ret = conf.parse(buf)) != ERROR_SUCCESS) { - return ret; + if ((err = conf.parse(buf)) != srs_success) { + return srs_error_wrap(err, "parse"); + } + + if ((ret = MockSrsConfig::reload_conf(&conf)) != ERROR_SUCCESS) { + return srs_error_new(ret, "reload conf"); } - return MockSrsConfig::reload_conf(&conf); + return err; } #ifdef ENABLE_UTEST_RELOAD diff --git a/trunk/src/utest/srs_utest_reload.hpp b/trunk/src/utest/srs_utest_reload.hpp index 19456a254..4e5eb2f18 100644 --- a/trunk/src/utest/srs_utest_reload.hpp +++ b/trunk/src/utest/srs_utest_reload.hpp @@ -99,7 +99,7 @@ public: MockSrsReloadConfig(); virtual ~MockSrsReloadConfig(); public: - virtual int do_reload(std::string buf); + virtual srs_error_t do_reload(std::string buf); }; #endif