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

Log: Use object as context id for complex context

This commit is contained in:
winlin 2020-07-05 23:26:55 +08:00
parent bff7ef085d
commit 6624b8acca
34 changed files with 276 additions and 238 deletions

View file

@ -42,7 +42,7 @@ SrsThreadContext::~SrsThreadContext()
{
}
string SrsThreadContext::generate_id()
SrsContextId SrsThreadContext::generate_id()
{
static int id = 0;
@ -51,22 +51,21 @@ string SrsThreadContext::generate_id()
}
int gid = id++;
stringstream ss;
ss << gid;
cache[srs_thread_self()] = ss.str();
return ss.str();
SrsContextId cid = SrsContextId(srs_int2str(gid));
cache[srs_thread_self()] = cid;
return cid;
}
string SrsThreadContext::get_id()
SrsContextId SrsThreadContext::get_id()
{
return cache[srs_thread_self()];
}
string SrsThreadContext::set_id(string v)
SrsContextId SrsThreadContext::set_id(SrsContextId v)
{
srs_thread_t self = srs_thread_self();
string ov;
SrsContextId ov;
if (cache.find(self) != cache.end()) {
ov = cache[self];
}
@ -79,7 +78,7 @@ string SrsThreadContext::set_id(string v)
void SrsThreadContext::clear_cid()
{
srs_thread_t self = srs_thread_self();
std::map<srs_thread_t, string>::iterator it = cache.find(self);
std::map<srs_thread_t, SrsContextId>::iterator it = cache.find(self);
if (it != cache.end()) {
cache.erase(it);
}
@ -108,7 +107,7 @@ void SrsConsoleLog::reopen()
{
}
void SrsConsoleLog::verbose(const char* tag, const char* context_id, const char* fmt, ...)
void SrsConsoleLog::verbose(const char* tag, SrsContextId context_id, const char* fmt, ...)
{
if (level > SrsLogLevelVerbose) {
return;
@ -128,7 +127,7 @@ void SrsConsoleLog::verbose(const char* tag, const char* context_id, const char*
fprintf(stdout, "%s\n", buffer);
}
void SrsConsoleLog::info(const char* tag, const char* context_id, const char* fmt, ...)
void SrsConsoleLog::info(const char* tag, SrsContextId context_id, const char* fmt, ...)
{
if (level > SrsLogLevelInfo) {
return;
@ -148,7 +147,7 @@ void SrsConsoleLog::info(const char* tag, const char* context_id, const char* fm
fprintf(stdout, "%s\n", buffer);
}
void SrsConsoleLog::trace(const char* tag, const char* context_id, const char* fmt, ...)
void SrsConsoleLog::trace(const char* tag, SrsContextId context_id, const char* fmt, ...)
{
if (level > SrsLogLevelTrace) {
return;
@ -168,7 +167,7 @@ void SrsConsoleLog::trace(const char* tag, const char* context_id, const char* f
fprintf(stdout, "%s\n", buffer);
}
void SrsConsoleLog::warn(const char* tag, const char* context_id, const char* fmt, ...)
void SrsConsoleLog::warn(const char* tag, SrsContextId context_id, const char* fmt, ...)
{
if (level > SrsLogLevelWarn) {
return;
@ -188,7 +187,7 @@ void SrsConsoleLog::warn(const char* tag, const char* context_id, const char* fm
fprintf(stderr, "%s\n", buffer);
}
void SrsConsoleLog::error(const char* tag, const char* context_id, const char* fmt, ...)
void SrsConsoleLog::error(const char* tag, SrsContextId context_id, const char* fmt, ...)
{
if (level > SrsLogLevelError) {
return;
@ -214,7 +213,7 @@ void SrsConsoleLog::error(const char* tag, const char* context_id, const char* f
}
// LCOV_EXCL_STOP
bool srs_log_header(char* buffer, int size, bool utc, bool dangerous, const char* tag, const char* cid, const char* level, int* psize)
bool srs_log_header(char* buffer, int size, bool utc, bool dangerous, const char* tag, SrsContextId cid, const char* level, int* psize)
{
// clock time
timeval tv;
@ -240,24 +239,24 @@ bool srs_log_header(char* buffer, int size, bool utc, bool dangerous, const char
written = snprintf(buffer, size,
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%s][%d][%s][%d] ",
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000),
level, tag, getpid(), cid, errno);
level, tag, getpid(), cid.c_str(), errno);
} else {
written = snprintf(buffer, size,
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%s][%d] ",
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000),
level, getpid(), cid, errno);
level, getpid(), cid.c_str(), errno);
}
} else {
if (tag) {
written = snprintf(buffer, size,
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%s][%d][%s] ",
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000),
level, tag, getpid(), cid);
level, tag, getpid(), cid.c_str());
} else {
written = snprintf(buffer, size,
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%s] ",
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000),
level, getpid(), cid);
level, getpid(), cid.c_str());
}
}

View file

@ -37,14 +37,14 @@
class SrsThreadContext : public ISrsContext
{
private:
std::map<srs_thread_t, std::string> cache;
std::map<srs_thread_t, SrsContextId> cache;
public:
SrsThreadContext();
virtual ~SrsThreadContext();
public:
virtual std::string generate_id();
virtual std::string get_id();
virtual std::string set_id(std::string v);
virtual SrsContextId generate_id();
virtual SrsContextId get_id();
virtual SrsContextId set_id(SrsContextId v);
public:
virtual void clear_cid();
};
@ -64,11 +64,11 @@ public:
public:
virtual srs_error_t initialize();
virtual void reopen();
virtual void verbose(const char* tag, const char* context_id, const char* fmt, ...);
virtual void info(const char* tag, const char* context_id, const char* fmt, ...);
virtual void trace(const char* tag, const char* context_id, const char* fmt, ...);
virtual void warn(const char* tag, const char* context_id, const char* fmt, ...);
virtual void error(const char* tag, const char* context_id, const char* fmt, ...);
virtual void verbose(const char* tag, SrsContextId context_id, const char* fmt, ...);
virtual void info(const char* tag, SrsContextId context_id, const char* fmt, ...);
virtual void trace(const char* tag, SrsContextId context_id, const char* fmt, ...);
virtual void warn(const char* tag, SrsContextId context_id, const char* fmt, ...);
virtual void error(const char* tag, SrsContextId context_id, const char* fmt, ...);
};
// Generate the log header.
@ -76,6 +76,6 @@ public:
// @param utc Whether use UTC time format in the log header.
// @param psize Output the actual header size.
// @remark It's a internal API.
bool srs_log_header(char* buffer, int size, bool utc, bool dangerous, const char* tag, const char* cid, const char* level, int* psize);
bool srs_log_header(char* buffer, int size, bool utc, bool dangerous, const char* tag, SrsContextId cid, const char* level, int* psize);
#endif

View file

@ -71,7 +71,7 @@ srs_error_t srs_st_init()
}
// Before ST init, we might have already inited the background cid.
string cid = _srs_context->get_id();
SrsContextId cid = _srs_context->get_id();
if (cid.empty()) {
cid = _srs_context->generate_id();
}