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

For #913, add complex error.

This commit is contained in:
winlin 2017-06-09 11:50:35 +08:00
parent 58521751c3
commit bb200b5d2d
6 changed files with 190 additions and 39 deletions

View file

@ -557,9 +557,10 @@ void SrsServer::dispose()
#endif #endif
} }
int SrsServer::initialize(ISrsServerCycle* cycle_handler) srs_error_t SrsServer::initialize(ISrsServerCycle* cycle_handler)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
// ensure the time is ok. // ensure the time is ok.
srs_update_system_time_ms(); srs_update_system_time_ms();
@ -575,15 +576,15 @@ int SrsServer::initialize(ISrsServerCycle* cycle_handler)
handler = cycle_handler; handler = cycle_handler;
if(handler && (ret = handler->initialize()) != ERROR_SUCCESS){ if(handler && (ret = handler->initialize()) != ERROR_SUCCESS){
return ret; return srs_error_new(ret, "handler initialize");
} }
if ((ret = http_api_mux->initialize()) != ERROR_SUCCESS) { if ((ret = http_api_mux->initialize()) != ERROR_SUCCESS) {
return ret; return srs_error_new(ret, "http api initialize");
} }
if ((ret = http_server->initialize()) != ERROR_SUCCESS) { if ((ret = http_server->initialize()) != ERROR_SUCCESS) {
return ret; return srs_error_new(ret, "http server initialize");
} }
http_heartbeat = new SrsHttpHeartbeat(); http_heartbeat = new SrsHttpHeartbeat();
@ -593,7 +594,7 @@ int SrsServer::initialize(ISrsServerCycle* cycle_handler)
ingester = new SrsIngester(); ingester = new SrsIngester();
#endif #endif
return ret; return err;
} }
int SrsServer::initialize_st() int SrsServer::initialize_st()

View file

@ -301,7 +301,7 @@ public:
* initialize server with callback handler. * initialize server with callback handler.
* @remark user must free the cycle handler. * @remark user must free the cycle handler.
*/ */
virtual int initialize(ISrsServerCycle* cycle_handler); virtual srs_error_t initialize(ISrsServerCycle* cycle_handler);
virtual int initialize_st(); virtual int initialize_st();
virtual int initialize_signal(); virtual int initialize_signal();
virtual int acquire_pid_file(); virtual int acquire_pid_file();

View file

@ -115,4 +115,8 @@
#error "only support i386/amd64/x86_64/arm cpu" #error "only support i386/amd64/x86_64/arm cpu"
#endif #endif
// Error predefined for all modules.
class SrsError;
typedef SrsError* srs_error_t;
#endif #endif

View file

@ -23,6 +23,12 @@
#include <srs_kernel_error.hpp> #include <srs_kernel_error.hpp>
#include <srs_kernel_log.hpp>
#include <errno.h>
#include <sstream>
using namespace std;
bool srs_is_system_control_error(int error_code) bool srs_is_system_control_error(int error_code)
{ {
return error_code == ERROR_CONTROL_RTMP_CLOSE return error_code == ERROR_CONTROL_RTMP_CLOSE
@ -38,3 +44,105 @@ bool srs_is_client_gracefully_close(int error_code)
|| error_code == ERROR_SOCKET_TIMEOUT; || error_code == ERROR_SOCKET_TIMEOUT;
} }
SrsError::SrsError()
{
code = ERROR_SUCCESS;
wrapped = NULL;
cid = rerrno = line = 0;
}
SrsError::~SrsError()
{
}
std::string SrsError::description() {
if (desc.empty()) {
stringstream ss;
ss << "code=" << code;
SrsError* next = this;
while (next) {
ss << " : " << next->msg;
next = next->wrapped;
}
ss << endl;
next = this;
while (next) {
ss << "thread #" << next->cid << ": "
<< next->func << "() [" << next->file << ":" << next->line << "]"
<< "[errno=" << next->rerrno << "]"
<< endl;
next = next->wrapped;
}
desc = ss.str();
}
return desc;
}
SrsError* SrsError::create(const char* func, const char* file, int line, int code, const char* fmt, ...) {
int rerrno = (int)errno;
va_list ap;
va_start(ap, fmt);
static char buffer[4096];
vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
SrsError* err = new SrsError();
err->func = func;
err->file = file;
err->line = line;
err->code = code;
err->rerrno = rerrno;
err->msg = buffer;
err->wrapped = NULL;
if (_srs_context) {
err->cid = _srs_context->get_id();
}
return err;
}
SrsError* SrsError::wrap(const char* func, const char* file, int line, SrsError* v, const char* fmt, ...) {
int rerrno = (int)errno;
va_list ap;
va_start(ap, fmt);
static char buffer[4096];
vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
SrsError* err = new SrsError();
err->func = func;
err->file = file;
err->line = line;
err->code = v->code;
err->rerrno = rerrno;
err->msg = buffer;
err->wrapped = v;
if (_srs_context) {
err->cid = _srs_context->get_id();
}
return err;
}
SrsError* SrsError::success() {
return NULL;
}
string SrsError::description(SrsError* err)
{
return err? err->description() : "Success";
}
int SrsError::error_code(SrsError* err)
{
return err? err->code : ERROR_SUCCESS;
}

View file

@ -26,6 +26,8 @@
#include <srs_core.hpp> #include <srs_core.hpp>
#include <string>
// for srs-librtmp, @see https://github.com/ossrs/srs/issues/213 // for srs-librtmp, @see https://github.com/ossrs/srs/issues/213
#ifndef _WIN32 #ifndef _WIN32
#define ERROR_SUCCESS 0 #define ERROR_SUCCESS 0
@ -330,19 +332,42 @@
extern bool srs_is_system_control_error(int error_code); extern bool srs_is_system_control_error(int error_code);
extern bool srs_is_client_gracefully_close(int error_code); extern bool srs_is_client_gracefully_close(int error_code);
/** // Use complex errors, @read https://github.com/ossrs/srs/issues/913
@remark: use column copy to generate the new error codes. class SrsError
01234567890 {
01234567891 private:
01234567892 int code;
01234567893 SrsError* wrapped;
01234567894 std::string msg;
01234567895
01234567896 std::string func;
01234567897 std::string file;
01234567898 int line;
01234567899
*/ int cid;
int rerrno;
std::string desc;
private:
SrsError();
public:
virtual ~SrsError();
private:
virtual std::string description();
public:
static SrsError* create(const char* func, const char* file, int line, int code, const char* fmt, ...);
static SrsError* wrap(const char* func, const char* file, int line, SrsError* err, const char* fmt, ...);
static SrsError* success();
static std::string description(SrsError* err);
static int error_code(SrsError* err);
};
// Error helpers, should use these functions to new or wrap an error.
#define srs_success SrsError::success()
#define srs_error_new(ret, fmt, ...) SrsError::create(__FUNCTION__, __FILE__, __LINE__, ret, fmt, ##__VA_ARGS__)
#define srs_error_wrap(err, fmt, ...) SrsError::wrap(__FUNCTION__, __FILE__, __LINE__, err, fmt, ##__VA_ARGS__)
#define srs_error_desc(err) SrsError::description(err)
#define srs_error_code(err) SrsError::error_code(err)
#endif #endif

View file

@ -50,7 +50,7 @@ using namespace std;
#include <srs_core_autofree.hpp> #include <srs_core_autofree.hpp>
// pre-declare // pre-declare
int run(SrsServer* svr); srs_error_t run(SrsServer* svr);
int run_master(SrsServer* svr); int run_master(SrsServer* svr);
void show_macro_features(); void show_macro_features();
string srs_getenv(const char* name); string srs_getenv(const char* name);
@ -182,16 +182,14 @@ 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);
* we do nothing in the constructor of server, if (err != srs_success) {
* and use initialize to create members, set hooks for instance the reload handler, srs_error("Failed, %s", srs_error_desc(err).c_str());
* all initialize will done in this stage.
*/
if ((ret = svr->initialize(NULL)) != ERROR_SUCCESS) {
return ret;
} }
return run(svr); ret = srs_error_code(err);
srs_freep(err);
return ret;
} }
/** /**
@ -363,11 +361,26 @@ string srs_getenv(const char* name)
return ""; return "";
} }
int run(SrsServer* svr) srs_error_t run(SrsServer* svr)
{ {
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
/**
* we do nothing in the constructor of server,
* and use initialize to create members, set hooks for instance the reload handler,
* all initialize will done in this stage.
*/
if ((err = svr->initialize(NULL)) != srs_success) {
return srs_error_wrap(err, "server initialize");
}
// if not deamon, directly run master. // if not deamon, directly run master.
if (!_srs_config->get_deamon()) { if (!_srs_config->get_deamon()) {
return run_master(svr); if ((ret = run_master(svr)) != ERROR_SUCCESS) {
return srs_error_new(ret, "run master");
}
return srs_success;
} }
srs_trace("start deamon mode..."); srs_trace("start deamon mode...");
@ -375,16 +388,13 @@ int run(SrsServer* svr)
int pid = fork(); int pid = fork();
if(pid < 0) { if(pid < 0) {
srs_error("create process error. ret=-1"); //ret=0 return srs_error_new(-1, "fork father process");
return -1;
} }
// grandpa // grandpa
if(pid > 0) { if(pid > 0) {
int status = 0; int status = 0;
if(waitpid(pid, &status, 0) == -1) { waitpid(pid, &status, 0);
srs_error("wait child process error! ret=-1"); //ret=0
}
srs_trace("grandpa process exit."); srs_trace("grandpa process exit.");
exit(0); exit(0);
} }
@ -393,19 +403,22 @@ int run(SrsServer* svr)
pid = fork(); pid = fork();
if(pid < 0) { if(pid < 0) {
srs_error("create process error. ret=0"); return srs_error_new(-1, "fork child process");
return -1;
} }
if(pid > 0) { if(pid > 0) {
srs_trace("father process exit. ret=0"); srs_trace("father process exit");
exit(0); exit(0);
} }
// son // son
srs_trace("son(deamon) process running."); srs_trace("son(deamon) process running.");
return run_master(svr); if ((ret = run_master(svr)) != ERROR_SUCCESS) {
return srs_error_new(ret, "daemon run master");
}
return srs_success;
} }
int run_master(SrsServer* svr) int run_master(SrsServer* svr)