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

@ -23,6 +23,12 @@
#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)
{
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;
}
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 <string>
// for srs-librtmp, @see https://github.com/ossrs/srs/issues/213
#ifndef _WIN32
#define ERROR_SUCCESS 0
@ -330,19 +332,42 @@
extern bool srs_is_system_control_error(int error_code);
extern bool srs_is_client_gracefully_close(int error_code);
/**
@remark: use column copy to generate the new error codes.
01234567890
01234567891
01234567892
01234567893
01234567894
01234567895
01234567896
01234567897
01234567898
01234567899
*/
// Use complex errors, @read https://github.com/ossrs/srs/issues/913
class SrsError
{
private:
int code;
SrsError* wrapped;
std::string msg;
std::string func;
std::string file;
int line;
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