1
0
Fork 0
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:
winlin 2020-07-22 15:51:48 +08:00
parent c2c35a7330
commit c115f77038
3 changed files with 118 additions and 18 deletions

View file

@ -24,7 +24,7 @@
#include <srs_app_pithy_print.hpp>
#include <stdlib.h>
#include <map>
using namespace std;
#include <srs_kernel_log.hpp>
#include <srs_app_config.hpp>
@ -75,7 +75,81 @@ srs_error_t SrsStageInfo::on_reload_pithy_print()
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)
{
@ -194,16 +268,7 @@ SrsPithyPrint::~SrsPithyPrint()
int SrsPithyPrint::enter_stage()
{
SrsStageInfo* stage = NULL;
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;
}
SrsStageInfo* stage = _srs_stages->fetch_or_create(stage_id);
srs_assert(stage != NULL);
client_id = stage->nb_clients++;
@ -215,7 +280,7 @@ int SrsPithyPrint::enter_stage()
void SrsPithyPrint::leave_stage()
{
SrsStageInfo* stage = _srs_stages[stage_id];
SrsStageInfo* stage = _srs_stages->fetch_or_create(stage_id);
srs_assert(stage != NULL);
stage->nb_clients--;
@ -226,7 +291,7 @@ void SrsPithyPrint::leave_stage()
void SrsPithyPrint::elapse()
{
SrsStageInfo* stage = _srs_stages[stage_id];
SrsStageInfo* stage = _srs_stages->fetch_or_create(stage_id);
srs_assert(stage != NULL);
srs_utime_t diff = srs_get_system_time() - previous_tick;
@ -239,7 +304,7 @@ void SrsPithyPrint::elapse()
bool SrsPithyPrint::can_print()
{
SrsStageInfo* stage = _srs_stages[stage_id];
SrsStageInfo* stage = _srs_stages->fetch_or_create(stage_id);
srs_assert(stage != NULL);
return stage->can_print();