From c115f7703863b24e1262478ad80d6eb73c488c80 Mon Sep 17 00:00:00 2001 From: winlin Date: Wed, 22 Jul 2020 15:51:48 +0800 Subject: [PATCH] RTC: Use error pithy print to reduce dup logs --- trunk/src/app/srs_app_listener.cpp | 10 ++- trunk/src/app/srs_app_pithy_print.cpp | 95 ++++++++++++++++++++++----- trunk/src/app/srs_app_pithy_print.hpp | 31 +++++++++ 3 files changed, 118 insertions(+), 18 deletions(-) diff --git a/trunk/src/app/srs_app_listener.cpp b/trunk/src/app/srs_app_listener.cpp index 313be0b17..eaa14b0be 100755 --- a/trunk/src/app/srs_app_listener.cpp +++ b/trunk/src/app/srs_app_listener.cpp @@ -506,6 +506,9 @@ srs_error_t SrsUdpMuxListener::cycle() uint64_t nn_loop = 0; srs_utime_t time_last = srs_get_system_time(); + SrsErrorPithyPrint* epp = new SrsErrorPithyPrint(); + SrsAutoFree(SrsErrorPithyPrint, epp); + set_socket_buffer(); while (true) { @@ -524,7 +527,7 @@ srs_error_t SrsUdpMuxListener::cycle() int nread = skt.recvfrom(SRS_UTIME_NO_TIMEOUT); if (nread <= 0) { if (nread < 0) { - srs_warn("udp recv error"); + srs_warn("udp recv error nn=%d", nread); } // remux udp never return continue; @@ -539,8 +542,9 @@ srs_error_t SrsUdpMuxListener::cycle() err = handler->on_udp_packet(&skt); } if (err != srs_success) { - // remux udp never return - srs_warn("udp packet handler error:%s", srs_error_desc(err).c_str()); + if (epp->can_print(err)) { + srs_warn("handle udp pkt err: %s", srs_error_desc(err).c_str()); + } srs_freep(err); } diff --git a/trunk/src/app/srs_app_pithy_print.cpp b/trunk/src/app/srs_app_pithy_print.cpp index 58f8bca4a..9494c13e9 100644 --- a/trunk/src/app/srs_app_pithy_print.cpp +++ b/trunk/src/app/srs_app_pithy_print.cpp @@ -24,7 +24,7 @@ #include #include -#include +using namespace std; #include #include @@ -75,7 +75,81 @@ srs_error_t SrsStageInfo::on_reload_pithy_print() return srs_success; } -static std::map _srs_stages; +SrsStageManager::SrsStageManager() +{ +} + +SrsStageManager::~SrsStageManager() +{ + map::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::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::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(); diff --git a/trunk/src/app/srs_app_pithy_print.hpp b/trunk/src/app/srs_app_pithy_print.hpp index ab289e0e7..ac985fdb5 100644 --- a/trunk/src/app/srs_app_pithy_print.hpp +++ b/trunk/src/app/srs_app_pithy_print.hpp @@ -26,6 +26,8 @@ #include +#include + #include // The stage info to calc the age. @@ -48,6 +50,35 @@ public: 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 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 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 print time in a stage is constant and not changed, // that is, we always got one message to print every specified time.