1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

For #913, config and log support complex error.

This commit is contained in:
winlin 2017-06-11 14:03:19 +08:00
parent 860aac3e50
commit b5c14938d9
13 changed files with 267 additions and 353 deletions

File diff suppressed because it is too large Load diff

View file

@ -73,7 +73,7 @@ namespace _srs_internal
/** /**
* fullfill the buffer with content of file specified by filename. * 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. * 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. * so we must transform the vhost directive anytime load the config.
* @param root the root directive to transform, in and out parameter. * @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. // @global config object.
extern SrsConfig* _srs_config; extern SrsConfig* _srs_config;
@ -239,7 +239,7 @@ public:
/** /**
* parse config directive from file buffer. * 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. * persistence the directive to writer.
* @param level, the root is level0, all its directives are level1, and so on. * @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, * 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. * 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. * read a token from buffer.
* a token, is the directive args and a flag indicates whether has child-directives. * 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. * @param line_start, the actual start line of directive.
* @return, an error code indicates error or has child-directives. * @return, an error code indicates error or has child-directives.
*/ */
virtual int read_token(_srs_internal::SrsConfigBuffer* buffer, std::vector<std::string>& args, int& line_start); virtual srs_error_t read_token(_srs_internal::SrsConfigBuffer* buffer, std::vector<std::string>& args, int& line_start);
}; };
/** /**
@ -412,12 +412,12 @@ public:
/** /**
* parse the cli, the main(argc,argv) function. * 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, * initialize the cwd for server,
* because we may change the workdir. * because we may change the workdir.
*/ */
virtual int initialize_cwd(); virtual srs_error_t initialize_cwd();
/** /**
* persistence current config to file. * persistence current config to file.
*/ */
@ -530,7 +530,7 @@ private:
/** /**
* parse each argv. * 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. * print help and exit.
*/ */
@ -539,21 +539,21 @@ public:
/** /**
* parse the config file, which is specified by cli. * 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. * check the parsed config.
*/ */
virtual int check_config(); virtual srs_error_t check_config();
protected: protected:
virtual int check_normal_config(); virtual srs_error_t check_normal_config();
virtual int check_number_connections(); virtual srs_error_t check_number_connections();
protected: protected:
/** /**
* parse config from the buffer. * parse config from the buffer.
* @param buffer, the config buffer, user must delete it. * @param buffer, the config buffer, user must delete it.
* @remark, use protected for the utest to override with mock. * @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 // global env
public: public:
/** /**

View file

@ -68,10 +68,8 @@ SrsFastLog::~SrsFastLog()
} }
} }
int SrsFastLog::initialize() srs_error_t SrsFastLog::initialize()
{ {
int ret = ERROR_SUCCESS;
if (_srs_config) { if (_srs_config) {
_srs_config->subscribe(this); _srs_config->subscribe(this);
@ -80,7 +78,7 @@ int SrsFastLog::initialize()
utc = _srs_config->get_utc_time(); utc = _srs_config->get_utc_time();
} }
return ret; return srs_success;
} }
void SrsFastLog::reopen() void SrsFastLog::reopen()

View file

@ -56,7 +56,7 @@ public:
virtual ~SrsFastLog(); virtual ~SrsFastLog();
// interface ISrsLog // interface ISrsLog
public: public:
virtual int initialize(); virtual srs_error_t initialize();
virtual void reopen(); virtual void reopen();
virtual void verbose(const char* tag, int context_id, const char* fmt, ...); virtual void verbose(const char* tag, int context_id, const char* fmt, ...);
virtual void info(const char* tag, int context_id, const char* fmt, ...); virtual void info(const char* tag, int context_id, const char* fmt, ...);

View file

@ -33,9 +33,9 @@ ISrsLog::~ISrsLog()
{ {
} }
int ISrsLog::initialize() srs_error_t ISrsLog::initialize()
{ {
return ERROR_SUCCESS; return srs_success;
} }
void ISrsLog::reopen() void ISrsLog::reopen()

View file

@ -65,7 +65,7 @@ public:
/** /**
* initialize log utilities. * initialize log utilities.
*/ */
virtual int initialize(); virtual srs_error_t initialize();
/** /**
* reopen the log file for log rotate. * reopen the log file for log rotate.
*/ */

View file

@ -67,9 +67,9 @@ extern const char* _srs_version;
/** /**
* main entrance. * 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. // TODO: support both little and big endian.
srs_assert(srs_is_little_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, // never use srs log(srs_trace, srs_error, etc) before config parse the option,
// which will load the log config and apply it. // which will load the log config and apply it.
if ((ret = _srs_config->parse_options(argc, argv)) != ERROR_SUCCESS) { if ((err = _srs_config->parse_options(argc, argv)) != srs_success) {
return ret; return srs_error_wrap(err, "config parse options");
} }
// change the work dir and set cwd. // change the work dir and set cwd.
int r0 = 0;
string cwd = _srs_config->get_work_dir(); string cwd = _srs_config->get_work_dir();
if (!cwd.empty() && cwd != "./" && (ret = chdir(cwd.c_str())) != ERROR_SUCCESS) { if (!cwd.empty() && cwd != "./" && (r0 = chdir(cwd.c_str())) == -1) {
srs_error("change cwd to %s failed. ret=%d", cwd.c_str(), ret); return srs_error_new(-1, "chdir to %s, r0=%d", cwd.c_str(), r0);
return ret;
} }
if ((ret = _srs_config->initialize_cwd()) != ERROR_SUCCESS) { if ((err = _srs_config->initialize_cwd()) != srs_success) {
return ret; return srs_error_wrap(err, "config cwd");
} }
// config parsed, initialize log. // config parsed, initialize log.
if ((ret = _srs_log->initialize()) != ERROR_SUCCESS) { if ((err = _srs_log->initialize()) != srs_success) {
return ret; return srs_error_wrap(err, "log initialize");
} }
// config already applied to log. // config already applied to log.
@ -172,8 +172,8 @@ int main(int argc, char** argv)
} }
// we check the config when the log initialized. // we check the config when the log initialized.
if ((ret = _srs_config->check_config()) != ERROR_SUCCESS) { if ((err = _srs_config->check_config()) != srs_success) {
return ret; return srs_error_wrap(err, "check config");
} }
// features // features
@ -182,12 +182,21 @@ int main(int argc, char** argv)
SrsServer* svr = new SrsServer(); SrsServer* svr = new SrsServer();
SrsAutoFree(SrsServer, svr); 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) { if (err != srs_success) {
srs_error("Failed, %s", srs_error_desc(err).c_str()); srs_error("Failed, %s", srs_error_desc(err).c_str());
} }
ret = srs_error_code(err); int ret = srs_error_code(err);
srs_freep(err); srs_freep(err);
return ret; return ret;
} }

View file

@ -91,9 +91,9 @@ SrsConsoleLog::~SrsConsoleLog()
srs_freepa(buffer); srs_freepa(buffer);
} }
int SrsConsoleLog::initialize() srs_error_t SrsConsoleLog::initialize()
{ {
return ERROR_SUCCESS; return srs_success;
} }
void SrsConsoleLog::reopen() void SrsConsoleLog::reopen()

View file

@ -65,7 +65,7 @@ public:
virtual ~SrsConsoleLog(); virtual ~SrsConsoleLog();
// interface ISrsLog // interface ISrsLog
public: public:
virtual int initialize(); virtual srs_error_t initialize();
virtual void reopen(); virtual void reopen();
virtual void verbose(const char* tag, int context_id, const char* fmt, ...); virtual void verbose(const char* tag, int context_id, const char* fmt, ...);
virtual void info(const char* tag, int context_id, const char* fmt, ...); virtual void info(const char* tag, int context_id, const char* fmt, ...);

View file

@ -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() 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); MockSrsConfigBuffer buffer(buf);
if ((ret = parse_buffer(&buffer)) != ERROR_SUCCESS) { if ((err = parse_buffer(&buffer)) != srs_success) {
return ret; return srs_error_wrap(err, "parse buffer");
} }
if ((ret = srs_config_transform_vhost(root)) != ERROR_SUCCESS) { if ((err = srs_config_transform_vhost(root)) != srs_success) {
srs_error("transform config failed. ret=%d", ret); return srs_error_wrap(err, "transform config");
return ret;
} }
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 #ifdef ENABLE_UTEST_CONFIG

View file

@ -41,7 +41,7 @@ public:
MockSrsConfigBuffer(std::string buf); MockSrsConfigBuffer(std::string buf);
virtual ~MockSrsConfigBuffer(); virtual ~MockSrsConfigBuffer();
public: public:
virtual int fullfill(const char* filename); virtual srs_error_t fullfill(const char* filename);
}; };
class MockSrsConfig : public SrsConfig class MockSrsConfig : public SrsConfig
@ -50,7 +50,7 @@ public:
MockSrsConfig(); MockSrsConfig();
virtual ~MockSrsConfig(); virtual ~MockSrsConfig();
public: public:
virtual int parse(std::string buf); virtual srs_error_t parse(std::string buf);
}; };
#endif #endif

View file

@ -283,16 +283,21 @@ MockSrsReloadConfig::~MockSrsReloadConfig()
{ {
} }
int MockSrsReloadConfig::do_reload(string buf) srs_error_t MockSrsReloadConfig::do_reload(string buf)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
MockSrsReloadConfig conf; MockSrsReloadConfig conf;
if ((ret = conf.parse(buf)) != ERROR_SUCCESS) { if ((err = conf.parse(buf)) != srs_success) {
return ret; return srs_error_wrap(err, "parse");
} }
return MockSrsConfig::reload_conf(&conf); if ((ret = MockSrsConfig::reload_conf(&conf)) != ERROR_SUCCESS) {
return srs_error_new(ret, "reload conf");
}
return err;
} }
#ifdef ENABLE_UTEST_RELOAD #ifdef ENABLE_UTEST_RELOAD

View file

@ -99,7 +99,7 @@ public:
MockSrsReloadConfig(); MockSrsReloadConfig();
virtual ~MockSrsReloadConfig(); virtual ~MockSrsReloadConfig();
public: public:
virtual int do_reload(std::string buf); virtual srs_error_t do_reload(std::string buf);
}; };
#endif #endif