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:
parent
2948b90f43
commit
81d2e10f65
11 changed files with 47 additions and 115 deletions
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -23,8 +23,6 @@
|
|||
|
||||
#include <srs_kernel_log.hpp>
|
||||
|
||||
#include <srs_kernel_error.hpp>
|
||||
|
||||
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 "";
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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<srs_thread_t, std::string> cache;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue