1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

Refactor ISrsContext and ISrsLog

This commit is contained in:
winlin 2020-06-25 13:14:59 +08:00
parent 2948b90f43
commit 81d2e10f65
11 changed files with 47 additions and 115 deletions

View file

@ -44,7 +44,7 @@
// reserved for the end of log data, it must be strlen(LOG_TAIL) // reserved for the end of log data, it must be strlen(LOG_TAIL)
#define LOG_TAIL_SIZE 1 #define LOG_TAIL_SIZE 1
SrsFastLog::SrsFastLog() SrsFileLog::SrsFileLog()
{ {
level = SrsLogLevelTrace; level = SrsLogLevelTrace;
log_data = new char[LOG_MAX_SIZE]; log_data = new char[LOG_MAX_SIZE];
@ -54,7 +54,7 @@ SrsFastLog::SrsFastLog()
utc = false; utc = false;
} }
SrsFastLog::~SrsFastLog() SrsFileLog::~SrsFileLog()
{ {
srs_freepa(log_data); srs_freepa(log_data);
@ -68,7 +68,7 @@ SrsFastLog::~SrsFastLog()
} }
} }
srs_error_t SrsFastLog::initialize() srs_error_t SrsFileLog::initialize()
{ {
if (_srs_config) { if (_srs_config) {
_srs_config->subscribe(this); _srs_config->subscribe(this);
@ -81,7 +81,7 @@ srs_error_t SrsFastLog::initialize()
return srs_success; return srs_success;
} }
void SrsFastLog::reopen() void SrsFileLog::reopen()
{ {
if (fd > 0) { if (fd > 0) {
::close(fd); ::close(fd);
@ -94,7 +94,7 @@ void SrsFastLog::reopen()
open_log_file(); 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) { if (level > SrsLogLevelVerbose) {
return; 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); 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) { if (level > SrsLogLevelInfo) {
return; 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); 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) { if (level > SrsLogLevelTrace) {
return; 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); 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) { if (level > SrsLogLevelWarn) {
return; 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); 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) { if (level > SrsLogLevelError) {
return; 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); 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(); utc = _srs_config->get_utc_time();
return srs_success; 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; srs_error_t err = srs_success;
@ -234,7 +234,7 @@ srs_error_t SrsFastLog::on_reload_log_tank()
return err; return err;
} }
srs_error_t SrsFastLog::on_reload_log_level() srs_error_t SrsFileLog::on_reload_log_level()
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
@ -247,7 +247,7 @@ srs_error_t SrsFastLog::on_reload_log_level()
return err; return err;
} }
srs_error_t SrsFastLog::on_reload_log_file() srs_error_t SrsFileLog::on_reload_log_file()
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
@ -267,7 +267,7 @@ srs_error_t SrsFastLog::on_reload_log_file()
return err; 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 // ensure the tail and EOF of string
// LOG_TAIL_SIZE for the TAIL char. // 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) { if (!_srs_config) {
return; return;

View file

@ -35,7 +35,7 @@
// Use memory/disk cache and donot flush when write log. // 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. // 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. // 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: private:
// Defined in SrsLogLevel. // Defined in SrsLogLevel.
@ -49,8 +49,8 @@ private:
// Whether use utc time. // Whether use utc time.
bool utc; bool utc;
public: public:
SrsFastLog(); SrsFileLog();
virtual ~SrsFastLog(); virtual ~SrsFileLog();
// Interface ISrsLog // Interface ISrsLog
public: public:
virtual srs_error_t initialize(); virtual srs_error_t initialize();

View file

@ -23,8 +23,6 @@
#include <srs_kernel_log.hpp> #include <srs_kernel_log.hpp>
#include <srs_kernel_error.hpp>
ISrsLog::ISrsLog() ISrsLog::ISrsLog()
{ {
} }
@ -33,56 +31,12 @@ ISrsLog::~ISrsLog()
{ {
} }
srs_error_t ISrsLog::initialize() ISrsContext::ISrsContext()
{
return srs_success;
}
void ISrsLog::reopen()
{ {
} }
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 "";
}

View file

@ -60,49 +60,49 @@ public:
virtual ~ISrsLog(); virtual ~ISrsLog();
public: public:
// Initialize log utilities. // Initialize log utilities.
virtual srs_error_t initialize(); virtual srs_error_t initialize() = 0;
// Reopen the log file for log rotate. // Reopen the log file for log rotate.
virtual void reopen(); virtual void reopen() = 0;
public: public:
// The log for verbose, very verbose information. // 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. // 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. // 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. // 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, // The log for error, something error occur, do something about the error, ie. close the connection,
// but we will donot abort the program. // 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. // The logic context for a RTMP connection, or RTC Session.
// Usage: // We can grep the context id to identify the logic unit, for debugging.
// _srs_context->generate_id(); // when thread start. // For example:
// _srs_context->get_id(); // get current generated id. // _srs_context->generate_id(); // Generate a new context.
// int old_id = _srs_context->set_id(1000); // set context id if need to merge thread context. // _srs_context->get_id(); // Get current context.
// The context for multiple clients. // int old_id = _srs_context->set_id("1000"); // Change the context.
class ISrsThreadContext class ISrsContext
{ {
public: public:
ISrsThreadContext(); ISrsContext();
virtual ~ISrsThreadContext(); virtual ~ISrsContext();
public: public:
// Generate the id for current context. // Generate the id for current context.
virtual std::string generate_id(); virtual std::string generate_id() = 0;
// Get the generated id of current context. // Get the generated id of current context.
virtual std::string get_id(); virtual std::string get_id() = 0;
// Set the id of current context. // Set the id of current context.
// @return the previous id value; 0 if no 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 // @global User must provides a log object
extern ISrsLog* _srs_log; extern ISrsLog* _srs_log;
// @global User must implements the LogContext and define a global instance. // @global User must implements the LogContext and define a global instance.
extern ISrsThreadContext* _srs_context; extern ISrsContext* _srs_context;
// Log style. // Log style.
// Use __FUNCTION__ to print c method // Use __FUNCTION__ to print c method

View file

@ -52,7 +52,7 @@ srs_error_t proxy_hls2rtmp(std::string hls, std::string rtmp);
// @global log and context. // @global log and context.
ISrsLog* _srs_log = new SrsConsoleLog(SrsLogLevelTrace, false); ISrsLog* _srs_log = new SrsConsoleLog(SrsLogLevelTrace, false);
ISrsThreadContext* _srs_context = new SrsThreadContext(); ISrsContext* _srs_context = new SrsThreadContext();
/** /**
* main entrance. * main entrance.

View file

@ -38,7 +38,7 @@ using namespace std;
// @global log and context. // @global log and context.
ISrsLog* _srs_log = new SrsConsoleLog(SrsLogLevelTrace, false); 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) srs_error_t parse(std::string mp4_file, bool verbose)
{ {

View file

@ -69,8 +69,8 @@ srs_error_t run_hybrid_server();
void show_macro_features(); void show_macro_features();
// @global log and context. // @global log and context.
ISrsLog* _srs_log = new SrsFastLog(); ISrsLog* _srs_log = new SrsFileLog();
ISrsThreadContext* _srs_context = new SrsThreadContext(); ISrsContext* _srs_context = new SrsThreadContext();
// @global config object for app module. // @global config object for app module.
SrsConfig* _srs_config = new SrsConfig(); SrsConfig* _srs_config = new SrsConfig();

View file

@ -34,7 +34,7 @@
// The st thread context, get_id will get the st-thread id, // The st thread context, get_id will get the st-thread id,
// which identify the client. // which identify the client.
class SrsThreadContext : public ISrsThreadContext class SrsThreadContext : public ISrsContext
{ {
private: private:
std::map<srs_thread_t, std::string> cache; std::map<srs_thread_t, std::string> cache;

View file

@ -41,7 +41,7 @@ srs_utime_t _srs_tmp_timeout = (100 * SRS_UTIME_MILLISECONDS);
// kernel module. // kernel module.
ISrsLog* _srs_log = new MockEmptyLog(SrsLogLevelDisabled); ISrsLog* _srs_log = new MockEmptyLog(SrsLogLevelDisabled);
ISrsThreadContext* _srs_context = new ISrsThreadContext(); ISrsContext* _srs_context = new SrsThreadContext();
// app module. // app module.
SrsConfig* _srs_config = NULL; SrsConfig* _srs_config = NULL;
SrsServer* _srs_server = NULL; SrsServer* _srs_server = NULL;

View file

@ -95,7 +95,7 @@ extern srs_utime_t _srs_tmp_timeout;
// print the bytes. // print the bytes.
void srs_bytes_print(char* pa, int size); void srs_bytes_print(char* pa, int size);
class MockEmptyLog : public SrsFastLog class MockEmptyLog : public SrsFileLog
{ {
public: public:
MockEmptyLog(SrsLogLevel l); MockEmptyLog(SrsLogLevel l);

View file

@ -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) VOID TEST(KernelMp3Test, CoverAll)
{ {
srs_error_t err; srs_error_t err;