mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
For #820, extract service module.
This commit is contained in:
parent
0a57a46016
commit
ff822b55cd
16 changed files with 510 additions and 213 deletions
|
@ -35,51 +35,6 @@
|
|||
#include <srs_app_utility.hpp>
|
||||
#include <srs_kernel_utility.hpp>
|
||||
|
||||
SrsThreadContext::SrsThreadContext()
|
||||
{
|
||||
}
|
||||
|
||||
SrsThreadContext::~SrsThreadContext()
|
||||
{
|
||||
}
|
||||
|
||||
int SrsThreadContext::generate_id()
|
||||
{
|
||||
static int id = 100;
|
||||
|
||||
int gid = id++;
|
||||
cache[st_thread_self()] = gid;
|
||||
return gid;
|
||||
}
|
||||
|
||||
int SrsThreadContext::get_id()
|
||||
{
|
||||
return cache[st_thread_self()];
|
||||
}
|
||||
|
||||
int SrsThreadContext::set_id(int v)
|
||||
{
|
||||
st_thread_t self = st_thread_self();
|
||||
|
||||
int ov = 0;
|
||||
if (cache.find(self) != cache.end()) {
|
||||
ov = cache[self];
|
||||
}
|
||||
|
||||
cache[self] = v;
|
||||
|
||||
return ov;
|
||||
}
|
||||
|
||||
void SrsThreadContext::clear_cid()
|
||||
{
|
||||
st_thread_t self = st_thread_self();
|
||||
std::map<st_thread_t, int>::iterator it = cache.find(self);
|
||||
if (it != cache.end()) {
|
||||
cache.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
// the max size of a line of log.
|
||||
#define LOG_MAX_SIZE 4096
|
||||
|
||||
|
@ -90,7 +45,7 @@ void SrsThreadContext::clear_cid()
|
|||
|
||||
SrsFastLog::SrsFastLog()
|
||||
{
|
||||
_level = SrsLogLevel::Trace;
|
||||
level = SrsLogLevelTrace;
|
||||
log_data = new char[LOG_MAX_SIZE];
|
||||
|
||||
fd = -1;
|
||||
|
@ -120,7 +75,7 @@ int SrsFastLog::initialize()
|
|||
_srs_config->subscribe(this);
|
||||
|
||||
log_to_file_tank = _srs_config->get_log_tank_file();
|
||||
_level = srs_get_log_level(_srs_config->get_log_level());
|
||||
level = srs_get_log_level(_srs_config->get_log_level());
|
||||
utc = _srs_config->get_utc_time();
|
||||
}
|
||||
|
||||
|
@ -142,12 +97,12 @@ void SrsFastLog::reopen()
|
|||
|
||||
void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...)
|
||||
{
|
||||
if (_level > SrsLogLevel::Verbose) {
|
||||
if (level > SrsLogLevelVerbose) {
|
||||
return;
|
||||
}
|
||||
|
||||
int size = 0;
|
||||
if (!generate_header(false, tag, context_id, "Verb", &size)) {
|
||||
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Verb", &size)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -157,17 +112,17 @@ void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...)
|
|||
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
write_log(fd, log_data, size, SrsLogLevel::Verbose);
|
||||
write_log(fd, log_data, size, SrsLogLevelVerbose);
|
||||
}
|
||||
|
||||
void SrsFastLog::info(const char* tag, int context_id, const char* fmt, ...)
|
||||
{
|
||||
if (_level > SrsLogLevel::Info) {
|
||||
if (level > SrsLogLevelInfo) {
|
||||
return;
|
||||
}
|
||||
|
||||
int size = 0;
|
||||
if (!generate_header(false, tag, context_id, "Debug", &size)) {
|
||||
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Debug", &size)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -177,17 +132,17 @@ void SrsFastLog::info(const char* tag, int context_id, const char* fmt, ...)
|
|||
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
write_log(fd, log_data, size, SrsLogLevel::Info);
|
||||
write_log(fd, log_data, size, SrsLogLevelInfo);
|
||||
}
|
||||
|
||||
void SrsFastLog::trace(const char* tag, int context_id, const char* fmt, ...)
|
||||
{
|
||||
if (_level > SrsLogLevel::Trace) {
|
||||
if (level > SrsLogLevelTrace) {
|
||||
return;
|
||||
}
|
||||
|
||||
int size = 0;
|
||||
if (!generate_header(false, tag, context_id, "Trace", &size)) {
|
||||
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Trace", &size)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -197,17 +152,17 @@ void SrsFastLog::trace(const char* tag, int context_id, const char* fmt, ...)
|
|||
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
write_log(fd, log_data, size, SrsLogLevel::Trace);
|
||||
write_log(fd, log_data, size, SrsLogLevelTrace);
|
||||
}
|
||||
|
||||
void SrsFastLog::warn(const char* tag, int context_id, const char* fmt, ...)
|
||||
{
|
||||
if (_level > SrsLogLevel::Warn) {
|
||||
if (level > SrsLogLevelWarn) {
|
||||
return;
|
||||
}
|
||||
|
||||
int size = 0;
|
||||
if (!generate_header(true, tag, context_id, "Warn", &size)) {
|
||||
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, true, tag, context_id, "Warn", &size)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -217,17 +172,17 @@ void SrsFastLog::warn(const char* tag, int context_id, const char* fmt, ...)
|
|||
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
write_log(fd, log_data, size, SrsLogLevel::Warn);
|
||||
write_log(fd, log_data, size, SrsLogLevelWarn);
|
||||
}
|
||||
|
||||
void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...)
|
||||
{
|
||||
if (_level > SrsLogLevel::Error) {
|
||||
if (level > SrsLogLevelError) {
|
||||
return;
|
||||
}
|
||||
|
||||
int size = 0;
|
||||
if (!generate_header(true, tag, context_id, "Error", &size)) {
|
||||
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, true, tag, context_id, "Error", &size)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -242,7 +197,7 @@ void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...)
|
|||
size += snprintf(log_data + size, LOG_MAX_SIZE - size, "(%s)", strerror(errno));
|
||||
}
|
||||
|
||||
write_log(fd, log_data, size, SrsLogLevel::Error);
|
||||
write_log(fd, log_data, size, SrsLogLevelError);
|
||||
}
|
||||
|
||||
int SrsFastLog::on_reload_utc_time()
|
||||
|
@ -287,7 +242,7 @@ int SrsFastLog::on_reload_log_level()
|
|||
return ret;
|
||||
}
|
||||
|
||||
_level = srs_get_log_level(_srs_config->get_log_level());
|
||||
level = srs_get_log_level(_srs_config->get_log_level());
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -312,65 +267,6 @@ int SrsFastLog::on_reload_log_file()
|
|||
return ret;
|
||||
}
|
||||
|
||||
bool SrsFastLog::generate_header(bool error, const char* tag, int context_id, const char* level_name, int* header_size)
|
||||
{
|
||||
// clock time
|
||||
timeval tv;
|
||||
if (gettimeofday(&tv, NULL) == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// to calendar time
|
||||
struct tm* tm;
|
||||
if (utc) {
|
||||
if ((tm = gmtime(&tv.tv_sec)) == NULL) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if ((tm = localtime(&tv.tv_sec)) == NULL) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// write log header
|
||||
int log_header_size = -1;
|
||||
|
||||
if (error) {
|
||||
if (tag) {
|
||||
log_header_size = snprintf(log_data, LOG_MAX_SIZE,
|
||||
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%s][%d][%d][%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_name, tag, getpid(), context_id, errno);
|
||||
} else {
|
||||
log_header_size = snprintf(log_data, LOG_MAX_SIZE,
|
||||
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%d][%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_name, getpid(), context_id, errno);
|
||||
}
|
||||
} else {
|
||||
if (tag) {
|
||||
log_header_size = snprintf(log_data, LOG_MAX_SIZE,
|
||||
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%s][%d][%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_name, tag, getpid(), context_id);
|
||||
} else {
|
||||
log_header_size = snprintf(log_data, LOG_MAX_SIZE,
|
||||
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%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_name, getpid(), context_id);
|
||||
}
|
||||
}
|
||||
|
||||
if (log_header_size == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// write the header size.
|
||||
*header_size = srs_min(LOG_MAX_SIZE - 1, log_header_size);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SrsFastLog::write_log(int& fd, char *str_log, int size, int level)
|
||||
{
|
||||
// ensure the tail and EOF of string
|
||||
|
@ -388,9 +284,9 @@ void SrsFastLog::write_log(int& fd, char *str_log, int size, int level)
|
|||
// \033[32m : green text code in shell
|
||||
// \033[33m : yellow text code in shell
|
||||
// \033[0m : normal text code
|
||||
if (level <= SrsLogLevel::Trace) {
|
||||
if (level <= SrsLogLevelTrace) {
|
||||
printf("%.*s", size, str_log);
|
||||
} else if (level == SrsLogLevel::Warn) {
|
||||
} else if (level == SrsLogLevelWarn) {
|
||||
printf("\033[33m%.*s\033[0m", size, str_log);
|
||||
} else{
|
||||
printf("\033[31m%.*s\033[0m", size, str_log);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue