From 81d2e10f65bf232c9985a0de0a7969d3b760d680 Mon Sep 17 00:00:00 2001 From: winlin Date: Thu, 25 Jun 2020 13:14:59 +0800 Subject: [PATCH] Refactor ISrsContext and ISrsLog --- trunk/src/app/srs_app_log.cpp | 30 ++++++++-------- trunk/src/app/srs_app_log.hpp | 6 ++-- trunk/src/kernel/srs_kernel_log.cpp | 50 ++------------------------ trunk/src/kernel/srs_kernel_log.hpp | 40 ++++++++++----------- trunk/src/main/srs_main_ingest_hls.cpp | 2 +- trunk/src/main/srs_main_mp4_parser.cpp | 2 +- trunk/src/main/srs_main_server.cpp | 4 +-- trunk/src/protocol/srs_service_log.hpp | 2 +- trunk/src/utest/srs_utest.cpp | 2 +- trunk/src/utest/srs_utest.hpp | 2 +- trunk/src/utest/srs_utest_kernel.cpp | 22 ------------ 11 files changed, 47 insertions(+), 115 deletions(-) diff --git a/trunk/src/app/srs_app_log.cpp b/trunk/src/app/srs_app_log.cpp index 0671bfb9f..045812f50 100644 --- a/trunk/src/app/srs_app_log.cpp +++ b/trunk/src/app/srs_app_log.cpp @@ -44,7 +44,7 @@ // reserved for the end of log data, it must be strlen(LOG_TAIL) #define LOG_TAIL_SIZE 1 -SrsFastLog::SrsFastLog() +SrsFileLog::SrsFileLog() { level = SrsLogLevelTrace; log_data = new char[LOG_MAX_SIZE]; @@ -54,7 +54,7 @@ SrsFastLog::SrsFastLog() utc = false; } -SrsFastLog::~SrsFastLog() +SrsFileLog::~SrsFileLog() { srs_freepa(log_data); @@ -68,7 +68,7 @@ SrsFastLog::~SrsFastLog() } } -srs_error_t SrsFastLog::initialize() +srs_error_t SrsFileLog::initialize() { if (_srs_config) { _srs_config->subscribe(this); @@ -81,7 +81,7 @@ srs_error_t SrsFastLog::initialize() return srs_success; } -void SrsFastLog::reopen() +void SrsFileLog::reopen() { if (fd > 0) { ::close(fd); @@ -94,7 +94,7 @@ void SrsFastLog::reopen() open_log_file(); } -void SrsFastLog::verbose(const char* tag, const char* context_id, const char* fmt, ...) +void SrsFileLog::verbose(const char* tag, const char* context_id, const char* fmt, ...) { if (level > SrsLogLevelVerbose) { return; @@ -114,7 +114,7 @@ void SrsFastLog::verbose(const char* tag, const char* context_id, const char* fm write_log(fd, log_data, size, SrsLogLevelVerbose); } -void SrsFastLog::info(const char* tag, const char* context_id, const char* fmt, ...) +void SrsFileLog::info(const char* tag, const char* context_id, const char* fmt, ...) { if (level > SrsLogLevelInfo) { return; @@ -134,7 +134,7 @@ void SrsFastLog::info(const char* tag, const char* context_id, const char* fmt, write_log(fd, log_data, size, SrsLogLevelInfo); } -void SrsFastLog::trace(const char* tag, const char* context_id, const char* fmt, ...) +void SrsFileLog::trace(const char* tag, const char* context_id, const char* fmt, ...) { if (level > SrsLogLevelTrace) { return; @@ -154,7 +154,7 @@ void SrsFastLog::trace(const char* tag, const char* context_id, const char* fmt, write_log(fd, log_data, size, SrsLogLevelTrace); } -void SrsFastLog::warn(const char* tag, const char* context_id, const char* fmt, ...) +void SrsFileLog::warn(const char* tag, const char* context_id, const char* fmt, ...) { if (level > SrsLogLevelWarn) { return; @@ -174,7 +174,7 @@ void SrsFastLog::warn(const char* tag, const char* context_id, const char* fmt, write_log(fd, log_data, size, SrsLogLevelWarn); } -void SrsFastLog::error(const char* tag, const char* context_id, const char* fmt, ...) +void SrsFileLog::error(const char* tag, const char* context_id, const char* fmt, ...) { if (level > SrsLogLevelError) { return; @@ -200,14 +200,14 @@ void SrsFastLog::error(const char* tag, const char* context_id, const char* fmt, write_log(fd, log_data, size, SrsLogLevelError); } -srs_error_t SrsFastLog::on_reload_utc_time() +srs_error_t SrsFileLog::on_reload_utc_time() { utc = _srs_config->get_utc_time(); return srs_success; } -srs_error_t SrsFastLog::on_reload_log_tank() +srs_error_t SrsFileLog::on_reload_log_tank() { srs_error_t err = srs_success; @@ -234,7 +234,7 @@ srs_error_t SrsFastLog::on_reload_log_tank() return err; } -srs_error_t SrsFastLog::on_reload_log_level() +srs_error_t SrsFileLog::on_reload_log_level() { srs_error_t err = srs_success; @@ -247,7 +247,7 @@ srs_error_t SrsFastLog::on_reload_log_level() return err; } -srs_error_t SrsFastLog::on_reload_log_file() +srs_error_t SrsFileLog::on_reload_log_file() { srs_error_t err = srs_success; @@ -267,7 +267,7 @@ srs_error_t SrsFastLog::on_reload_log_file() return err; } -void SrsFastLog::write_log(int& fd, char *str_log, int size, int level) +void SrsFileLog::write_log(int& fd, char *str_log, int size, int level) { // ensure the tail and EOF of string // LOG_TAIL_SIZE for the TAIL char. @@ -307,7 +307,7 @@ void SrsFastLog::write_log(int& fd, char *str_log, int size, int level) } } -void SrsFastLog::open_log_file() +void SrsFileLog::open_log_file() { if (!_srs_config) { return; diff --git a/trunk/src/app/srs_app_log.hpp b/trunk/src/app/srs_app_log.hpp index 81a9e1696..d05461c06 100644 --- a/trunk/src/app/srs_app_log.hpp +++ b/trunk/src/app/srs_app_log.hpp @@ -35,7 +35,7 @@ // Use memory/disk cache and donot flush when write log. // it's ok to use it without config, which will log to console, and default trace level. // when you want to use different level, override this classs, set the protected _level. -class SrsFastLog : public ISrsLog, public ISrsReloadHandler +class SrsFileLog : public ISrsLog, public ISrsReloadHandler { private: // Defined in SrsLogLevel. @@ -49,8 +49,8 @@ private: // Whether use utc time. bool utc; public: - SrsFastLog(); - virtual ~SrsFastLog(); + SrsFileLog(); + virtual ~SrsFileLog(); // Interface ISrsLog public: virtual srs_error_t initialize(); diff --git a/trunk/src/kernel/srs_kernel_log.cpp b/trunk/src/kernel/srs_kernel_log.cpp index 2fd5ae072..6cfe61068 100644 --- a/trunk/src/kernel/srs_kernel_log.cpp +++ b/trunk/src/kernel/srs_kernel_log.cpp @@ -23,8 +23,6 @@ #include -#include - ISrsLog::ISrsLog() { } @@ -33,56 +31,12 @@ ISrsLog::~ISrsLog() { } -srs_error_t ISrsLog::initialize() -{ - return srs_success; -} - -void ISrsLog::reopen() +ISrsContext::ISrsContext() { } -void ISrsLog::verbose(const char* /*tag*/, const char* /*context_id*/, const char* /*fmt*/, ...) +ISrsContext::~ISrsContext() { } -void ISrsLog::info(const char* /*tag*/, const char* /*context_id*/, const char* /*fmt*/, ...) -{ -} - -void ISrsLog::trace(const char* /*tag*/, const char* /*context_id*/, const char* /*fmt*/, ...) -{ -} - -void ISrsLog::warn(const char* /*tag*/, const char* /*context_id*/, const char* /*fmt*/, ...) -{ -} - -void ISrsLog::error(const char* /*tag*/, const char* /*context_id*/, const char* /*fmt*/, ...) -{ -} - -ISrsThreadContext::ISrsThreadContext() -{ -} - -ISrsThreadContext::~ISrsThreadContext() -{ -} - -std::string ISrsThreadContext::generate_id() -{ - return ""; -} - -std::string ISrsThreadContext::get_id() -{ - return ""; -} - -std::string ISrsThreadContext::set_id(std::string /*v*/) -{ - return ""; -} - diff --git a/trunk/src/kernel/srs_kernel_log.hpp b/trunk/src/kernel/srs_kernel_log.hpp index 56aefce4c..9d47c3371 100644 --- a/trunk/src/kernel/srs_kernel_log.hpp +++ b/trunk/src/kernel/srs_kernel_log.hpp @@ -60,49 +60,49 @@ public: virtual ~ISrsLog(); public: // Initialize log utilities. - virtual srs_error_t initialize(); + virtual srs_error_t initialize() = 0; // Reopen the log file for log rotate. - virtual void reopen(); + virtual void reopen() = 0; public: // The log for verbose, very verbose information. - virtual void verbose(const char* tag, const char* context_id, const char* fmt, ...); + virtual void verbose(const char* tag, const char* context_id, const char* fmt, ...) = 0; // The log for debug, detail information. - virtual void info(const char* tag, const char* context_id, const char* fmt, ...); + virtual void info(const char* tag, const char* context_id, const char* fmt, ...) = 0; // The log for trace, important information. - virtual void trace(const char* tag, const char* context_id, const char* fmt, ...); + virtual void trace(const char* tag, const char* context_id, const char* fmt, ...) = 0; // The log for warn, warn is something should take attention, but not a error. - virtual void warn(const char* tag, const char* context_id, const char* fmt, ...); + virtual void warn(const char* tag, const char* context_id, const char* fmt, ...) = 0; // The log for error, something error occur, do something about the error, ie. close the connection, // but we will donot abort the program. - virtual void error(const char* tag, const char* context_id, const char* fmt, ...); + virtual void error(const char* tag, const char* context_id, const char* fmt, ...) = 0; }; -// The context id manager to identify context, for instance, the green-thread. -// Usage: -// _srs_context->generate_id(); // when thread start. -// _srs_context->get_id(); // get current generated id. -// int old_id = _srs_context->set_id(1000); // set context id if need to merge thread context. -// The context for multiple clients. -class ISrsThreadContext +// The logic context for a RTMP connection, or RTC Session. +// We can grep the context id to identify the logic unit, for debugging. +// For example: +// _srs_context->generate_id(); // Generate a new context. +// _srs_context->get_id(); // Get current context. +// int old_id = _srs_context->set_id("1000"); // Change the context. +class ISrsContext { public: - ISrsThreadContext(); - virtual ~ISrsThreadContext(); + ISrsContext(); + virtual ~ISrsContext(); public: // Generate the id for current context. - virtual std::string generate_id(); + virtual std::string generate_id() = 0; // Get the generated id of current context. - virtual std::string get_id(); + virtual std::string get_id() = 0; // Set the id of current context. // @return the previous id value; 0 if no context. - virtual std::string set_id(std::string v); + virtual std::string set_id(std::string v) = 0; }; // @global User must provides a log object extern ISrsLog* _srs_log; // @global User must implements the LogContext and define a global instance. -extern ISrsThreadContext* _srs_context; +extern ISrsContext* _srs_context; // Log style. // Use __FUNCTION__ to print c method diff --git a/trunk/src/main/srs_main_ingest_hls.cpp b/trunk/src/main/srs_main_ingest_hls.cpp index 34eb3a7da..29b928a61 100644 --- a/trunk/src/main/srs_main_ingest_hls.cpp +++ b/trunk/src/main/srs_main_ingest_hls.cpp @@ -52,7 +52,7 @@ srs_error_t proxy_hls2rtmp(std::string hls, std::string rtmp); // @global log and context. ISrsLog* _srs_log = new SrsConsoleLog(SrsLogLevelTrace, false); -ISrsThreadContext* _srs_context = new SrsThreadContext(); +ISrsContext* _srs_context = new SrsThreadContext(); /** * main entrance. diff --git a/trunk/src/main/srs_main_mp4_parser.cpp b/trunk/src/main/srs_main_mp4_parser.cpp index 66b5ea525..c3ccfec14 100644 --- a/trunk/src/main/srs_main_mp4_parser.cpp +++ b/trunk/src/main/srs_main_mp4_parser.cpp @@ -38,7 +38,7 @@ using namespace std; // @global log and context. ISrsLog* _srs_log = new SrsConsoleLog(SrsLogLevelTrace, false); -ISrsThreadContext* _srs_context = new SrsThreadContext(); +ISrsContext* _srs_context = new SrsThreadContext(); srs_error_t parse(std::string mp4_file, bool verbose) { diff --git a/trunk/src/main/srs_main_server.cpp b/trunk/src/main/srs_main_server.cpp index 3a73b7b55..e9d6e51be 100644 --- a/trunk/src/main/srs_main_server.cpp +++ b/trunk/src/main/srs_main_server.cpp @@ -69,8 +69,8 @@ srs_error_t run_hybrid_server(); void show_macro_features(); // @global log and context. -ISrsLog* _srs_log = new SrsFastLog(); -ISrsThreadContext* _srs_context = new SrsThreadContext(); +ISrsLog* _srs_log = new SrsFileLog(); +ISrsContext* _srs_context = new SrsThreadContext(); // @global config object for app module. SrsConfig* _srs_config = new SrsConfig(); diff --git a/trunk/src/protocol/srs_service_log.hpp b/trunk/src/protocol/srs_service_log.hpp index 72c93400d..a0bd141ab 100644 --- a/trunk/src/protocol/srs_service_log.hpp +++ b/trunk/src/protocol/srs_service_log.hpp @@ -34,7 +34,7 @@ // The st thread context, get_id will get the st-thread id, // which identify the client. -class SrsThreadContext : public ISrsThreadContext +class SrsThreadContext : public ISrsContext { private: std::map cache; diff --git a/trunk/src/utest/srs_utest.cpp b/trunk/src/utest/srs_utest.cpp index 06b69e118..9d8678518 100644 --- a/trunk/src/utest/srs_utest.cpp +++ b/trunk/src/utest/srs_utest.cpp @@ -41,7 +41,7 @@ srs_utime_t _srs_tmp_timeout = (100 * SRS_UTIME_MILLISECONDS); // kernel module. ISrsLog* _srs_log = new MockEmptyLog(SrsLogLevelDisabled); -ISrsThreadContext* _srs_context = new ISrsThreadContext(); +ISrsContext* _srs_context = new SrsThreadContext(); // app module. SrsConfig* _srs_config = NULL; SrsServer* _srs_server = NULL; diff --git a/trunk/src/utest/srs_utest.hpp b/trunk/src/utest/srs_utest.hpp index ffda614d9..22d0d70e2 100644 --- a/trunk/src/utest/srs_utest.hpp +++ b/trunk/src/utest/srs_utest.hpp @@ -95,7 +95,7 @@ extern srs_utime_t _srs_tmp_timeout; // print the bytes. void srs_bytes_print(char* pa, int size); -class MockEmptyLog : public SrsFastLog +class MockEmptyLog : public SrsFileLog { public: MockEmptyLog(SrsLogLevel l); diff --git a/trunk/src/utest/srs_utest_kernel.cpp b/trunk/src/utest/srs_utest_kernel.cpp index bc0109ef1..deab330cb 100644 --- a/trunk/src/utest/srs_utest_kernel.cpp +++ b/trunk/src/utest/srs_utest_kernel.cpp @@ -4090,28 +4090,6 @@ VOID TEST(KernelFLVTest, CoverSharedPtrMessage) } } -VOID TEST(KernelLogTest, CoverAll) -{ - srs_error_t err; - - if (true) { - ISrsLog l; - HELPER_EXPECT_SUCCESS(l.initialize()); - - l.reopen(); - l.verbose("TAG", "0", "log"); - l.info("TAG", "0", "log"); - l.trace("TAG", "0", "log"); - l.warn("TAG", "0", "log"); - l.error("TAG", "0", "log"); - - ISrsThreadContext ctx; - ctx.set_id("10"); - EXPECT_EQ("", ctx.get_id()); - EXPECT_EQ("", ctx.generate_id()); - } -} - VOID TEST(KernelMp3Test, CoverAll) { srs_error_t err;