mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
RTC: Use error pithy print to reduce dup logs
This commit is contained in:
parent
c2c35a7330
commit
c115f77038
3 changed files with 118 additions and 18 deletions
|
@ -506,6 +506,9 @@ srs_error_t SrsUdpMuxListener::cycle()
|
||||||
uint64_t nn_loop = 0;
|
uint64_t nn_loop = 0;
|
||||||
srs_utime_t time_last = srs_get_system_time();
|
srs_utime_t time_last = srs_get_system_time();
|
||||||
|
|
||||||
|
SrsErrorPithyPrint* epp = new SrsErrorPithyPrint();
|
||||||
|
SrsAutoFree(SrsErrorPithyPrint, epp);
|
||||||
|
|
||||||
set_socket_buffer();
|
set_socket_buffer();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -524,7 +527,7 @@ srs_error_t SrsUdpMuxListener::cycle()
|
||||||
int nread = skt.recvfrom(SRS_UTIME_NO_TIMEOUT);
|
int nread = skt.recvfrom(SRS_UTIME_NO_TIMEOUT);
|
||||||
if (nread <= 0) {
|
if (nread <= 0) {
|
||||||
if (nread < 0) {
|
if (nread < 0) {
|
||||||
srs_warn("udp recv error");
|
srs_warn("udp recv error nn=%d", nread);
|
||||||
}
|
}
|
||||||
// remux udp never return
|
// remux udp never return
|
||||||
continue;
|
continue;
|
||||||
|
@ -539,8 +542,9 @@ srs_error_t SrsUdpMuxListener::cycle()
|
||||||
err = handler->on_udp_packet(&skt);
|
err = handler->on_udp_packet(&skt);
|
||||||
}
|
}
|
||||||
if (err != srs_success) {
|
if (err != srs_success) {
|
||||||
// remux udp never return
|
if (epp->can_print(err)) {
|
||||||
srs_warn("udp packet handler error:%s", srs_error_desc(err).c_str());
|
srs_warn("handle udp pkt err: %s", srs_error_desc(err).c_str());
|
||||||
|
}
|
||||||
srs_freep(err);
|
srs_freep(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
#include <srs_app_pithy_print.hpp>
|
#include <srs_app_pithy_print.hpp>
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <map>
|
using namespace std;
|
||||||
|
|
||||||
#include <srs_kernel_log.hpp>
|
#include <srs_kernel_log.hpp>
|
||||||
#include <srs_app_config.hpp>
|
#include <srs_app_config.hpp>
|
||||||
|
@ -75,7 +75,81 @@ srs_error_t SrsStageInfo::on_reload_pithy_print()
|
||||||
return srs_success;
|
return srs_success;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::map<int, SrsStageInfo*> _srs_stages;
|
SrsStageManager::SrsStageManager()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
SrsStageManager::~SrsStageManager()
|
||||||
|
{
|
||||||
|
map<int, SrsStageInfo*>::iterator it;
|
||||||
|
for (it = stages.begin(); it != stages.end(); ++it) {
|
||||||
|
SrsStageInfo* stage = it->second;
|
||||||
|
srs_freep(stage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SrsStageInfo* SrsStageManager::fetch_or_create(int stage_id, bool* pnew)
|
||||||
|
{
|
||||||
|
std::map<int, SrsStageInfo*>::iterator it = stages.find(stage_id);
|
||||||
|
|
||||||
|
// Create one if not exists.
|
||||||
|
if (it == stages.end()) {
|
||||||
|
SrsStageInfo* stage = new SrsStageInfo(stage_id);
|
||||||
|
stages[stage_id] = stage;
|
||||||
|
|
||||||
|
if (pnew) {
|
||||||
|
*pnew = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return stage;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exists, fetch it.
|
||||||
|
SrsStageInfo* stage = it->second;
|
||||||
|
|
||||||
|
if (pnew) {
|
||||||
|
*pnew = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return stage;
|
||||||
|
}
|
||||||
|
|
||||||
|
SrsErrorPithyPrint::SrsErrorPithyPrint()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
SrsErrorPithyPrint::~SrsErrorPithyPrint()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SrsErrorPithyPrint::can_print(srs_error_t err)
|
||||||
|
{
|
||||||
|
int error_code = srs_error_code(err);
|
||||||
|
|
||||||
|
bool new_stage = false;
|
||||||
|
SrsStageInfo* stage = stages.fetch_or_create(error_code, &new_stage);
|
||||||
|
|
||||||
|
// Always and only one client.
|
||||||
|
if (new_stage) {
|
||||||
|
stage->nb_clients = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
srs_utime_t tick = ticks[error_code];
|
||||||
|
if (!tick) {
|
||||||
|
ticks[error_code] = tick = srs_get_system_time();
|
||||||
|
}
|
||||||
|
|
||||||
|
srs_utime_t diff = srs_get_system_time() - tick;
|
||||||
|
diff = srs_max(0, diff);
|
||||||
|
|
||||||
|
stage->elapse(diff);
|
||||||
|
ticks[error_code] = srs_get_system_time();
|
||||||
|
|
||||||
|
return new_stage || stage->can_print();
|
||||||
|
}
|
||||||
|
|
||||||
|
// The global stage manager for pithy print, multiple stages.
|
||||||
|
static SrsStageManager* _srs_stages = new SrsStageManager();
|
||||||
|
|
||||||
SrsPithyPrint::SrsPithyPrint(int _stage_id)
|
SrsPithyPrint::SrsPithyPrint(int _stage_id)
|
||||||
{
|
{
|
||||||
|
@ -194,16 +268,7 @@ SrsPithyPrint::~SrsPithyPrint()
|
||||||
|
|
||||||
int SrsPithyPrint::enter_stage()
|
int SrsPithyPrint::enter_stage()
|
||||||
{
|
{
|
||||||
SrsStageInfo* stage = NULL;
|
SrsStageInfo* stage = _srs_stages->fetch_or_create(stage_id);
|
||||||
|
|
||||||
std::map<int, SrsStageInfo*>::iterator it = _srs_stages.find(stage_id);
|
|
||||||
if (it == _srs_stages.end()) {
|
|
||||||
stage = new SrsStageInfo(stage_id);
|
|
||||||
_srs_stages[stage_id] = stage;
|
|
||||||
} else {
|
|
||||||
stage = it->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
srs_assert(stage != NULL);
|
srs_assert(stage != NULL);
|
||||||
client_id = stage->nb_clients++;
|
client_id = stage->nb_clients++;
|
||||||
|
|
||||||
|
@ -215,7 +280,7 @@ int SrsPithyPrint::enter_stage()
|
||||||
|
|
||||||
void SrsPithyPrint::leave_stage()
|
void SrsPithyPrint::leave_stage()
|
||||||
{
|
{
|
||||||
SrsStageInfo* stage = _srs_stages[stage_id];
|
SrsStageInfo* stage = _srs_stages->fetch_or_create(stage_id);
|
||||||
srs_assert(stage != NULL);
|
srs_assert(stage != NULL);
|
||||||
|
|
||||||
stage->nb_clients--;
|
stage->nb_clients--;
|
||||||
|
@ -226,7 +291,7 @@ void SrsPithyPrint::leave_stage()
|
||||||
|
|
||||||
void SrsPithyPrint::elapse()
|
void SrsPithyPrint::elapse()
|
||||||
{
|
{
|
||||||
SrsStageInfo* stage = _srs_stages[stage_id];
|
SrsStageInfo* stage = _srs_stages->fetch_or_create(stage_id);
|
||||||
srs_assert(stage != NULL);
|
srs_assert(stage != NULL);
|
||||||
|
|
||||||
srs_utime_t diff = srs_get_system_time() - previous_tick;
|
srs_utime_t diff = srs_get_system_time() - previous_tick;
|
||||||
|
@ -239,7 +304,7 @@ void SrsPithyPrint::elapse()
|
||||||
|
|
||||||
bool SrsPithyPrint::can_print()
|
bool SrsPithyPrint::can_print()
|
||||||
{
|
{
|
||||||
SrsStageInfo* stage = _srs_stages[stage_id];
|
SrsStageInfo* stage = _srs_stages->fetch_or_create(stage_id);
|
||||||
srs_assert(stage != NULL);
|
srs_assert(stage != NULL);
|
||||||
|
|
||||||
return stage->can_print();
|
return stage->can_print();
|
||||||
|
|
|
@ -26,6 +26,8 @@
|
||||||
|
|
||||||
#include <srs_core.hpp>
|
#include <srs_core.hpp>
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
|
||||||
#include <srs_app_reload.hpp>
|
#include <srs_app_reload.hpp>
|
||||||
|
|
||||||
// The stage info to calc the age.
|
// The stage info to calc the age.
|
||||||
|
@ -48,6 +50,35 @@ public:
|
||||||
virtual srs_error_t on_reload_pithy_print();
|
virtual srs_error_t on_reload_pithy_print();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// The manager for stages, it's used for a single client stage.
|
||||||
|
// Of course, we can add the multiple user support, which is SrsPithyPrint.
|
||||||
|
class SrsStageManager
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::map<int, SrsStageInfo*> stages;
|
||||||
|
public:
|
||||||
|
SrsStageManager();
|
||||||
|
virtual ~SrsStageManager();
|
||||||
|
public:
|
||||||
|
// Fetch a stage, create one if not exists.
|
||||||
|
SrsStageInfo* fetch_or_create(int stage_id, bool* pnew = NULL);
|
||||||
|
};
|
||||||
|
|
||||||
|
// The error pithy print is a single client stage manager, each stage only has one client.
|
||||||
|
// For example, we use it for error pithy print for each UDP packet processing.
|
||||||
|
class SrsErrorPithyPrint
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
SrsStageManager stages;
|
||||||
|
std::map<int, srs_utime_t> ticks;
|
||||||
|
public:
|
||||||
|
SrsErrorPithyPrint();
|
||||||
|
virtual ~SrsErrorPithyPrint();
|
||||||
|
public:
|
||||||
|
// Whether specified stage is ready for print.
|
||||||
|
bool can_print(srs_error_t err);
|
||||||
|
};
|
||||||
|
|
||||||
// The stage is used for a collection of object to do print,
|
// The stage is used for a collection of object to do print,
|
||||||
// the print time in a stage is constant and not changed,
|
// the print time in a stage is constant and not changed,
|
||||||
// that is, we always got one message to print every specified time.
|
// that is, we always got one message to print every specified time.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue