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

Log: Refine the log interface. v5.0.82

This commit is contained in:
winlin 2022-10-25 09:18:33 +08:00
parent 252851d1b0
commit e9915c3bd7
10 changed files with 91 additions and 277 deletions

View file

@ -106,7 +106,7 @@ impl_SrsContextRestore::~impl_SrsContextRestore()
// LCOV_EXCL_START
SrsConsoleLog::SrsConsoleLog(SrsLogLevel l, bool u)
{
level = l;
level_ = l;
utc = u;
buffer = new char[SRS_BASIC_LOG_SIZE];
@ -126,130 +126,26 @@ void SrsConsoleLog::reopen()
{
}
void SrsConsoleLog::verbose(const char* tag, SrsContextId context_id, const char* fmt, ...)
void SrsConsoleLog::log(SrsLogLevel level, const char* tag, const SrsContextId& context_id, const char* fmt, va_list args)
{
if (level > SrsLogLevelVerbose) {
if (level < level_ || level >= SrsLogLevelDisabled) {
return;
}
int size = 0;
if (!srs_log_header(buffer, SRS_BASIC_LOG_SIZE, utc, false, tag, context_id, "Verb", &size)) {
if (!srs_log_header(buffer, SRS_BASIC_LOG_SIZE, utc, level >= SrsLogLevelWarn, tag, context_id, srs_log_level_strings[level], &size)) {
return;
}
va_list ap;
va_start(ap, fmt);
int r0 = vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
va_end(ap);
// Something not expected, drop the log.
int r0 = vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, args);
if (r0 <= 0 || r0 >= SRS_BASIC_LOG_SIZE - size) {
return;
}
size += r0;
fprintf(stdout, "%s\n", buffer);
}
void SrsConsoleLog::info(const char* tag, SrsContextId context_id, const char* fmt, ...)
{
if (level > SrsLogLevelInfo) {
return;
}
int size = 0;
if (!srs_log_header(buffer, SRS_BASIC_LOG_SIZE, utc, false, tag, context_id, "Debug", &size)) {
return;
}
va_list ap;
va_start(ap, fmt);
int r0 = vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= SRS_BASIC_LOG_SIZE - size) {
return;
}
size += r0;
fprintf(stdout, "%s\n", buffer);
}
void SrsConsoleLog::trace(const char* tag, SrsContextId context_id, const char* fmt, ...)
{
if (level > SrsLogLevelTrace) {
return;
}
int size = 0;
if (!srs_log_header(buffer, SRS_BASIC_LOG_SIZE, utc, false, tag, context_id, "Trace", &size)) {
return;
}
va_list ap;
va_start(ap, fmt);
int r0 = vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= SRS_BASIC_LOG_SIZE - size) {
return;
}
size += r0;
fprintf(stdout, "%s\n", buffer);
}
void SrsConsoleLog::warn(const char* tag, SrsContextId context_id, const char* fmt, ...)
{
if (level > SrsLogLevelWarn) {
return;
}
int size = 0;
if (!srs_log_header(buffer, SRS_BASIC_LOG_SIZE, utc, true, tag, context_id, "Warn", &size)) {
return;
}
va_list ap;
va_start(ap, fmt);
int r0 = vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= SRS_BASIC_LOG_SIZE - size) {
return;
}
size += r0;
fprintf(stderr, "%s\n", buffer);
}
void SrsConsoleLog::error(const char* tag, SrsContextId context_id, const char* fmt, ...)
{
if (level > SrsLogLevelError) {
return;
}
int size = 0;
if (!srs_log_header(buffer, SRS_BASIC_LOG_SIZE, utc, true, tag, context_id, "Error", &size)) {
return;
}
va_list ap;
va_start(ap, fmt);
int r0 = vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= SRS_BASIC_LOG_SIZE - size) {
return;
}
size += r0;
// add strerror() to error msg.
if (errno != 0) {
// Add errno and strerror() if error.
if (level == SrsLogLevelError && errno != 0) {
r0 = snprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, "(%s)", strerror(errno));
// Something not expected, drop the log.
@ -258,10 +154,13 @@ void SrsConsoleLog::error(const char* tag, SrsContextId context_id, const char*
}
size += r0;
}
fprintf(stderr, "%s\n", buffer);
if (level >= SrsLogLevelWarn) {
fprintf(stderr, "%s\n", buffer);
} else {
fprintf(stdout, "%s\n", buffer);
}
}
// LCOV_EXCL_STOP
bool srs_log_header(char* buffer, int size, bool utc, bool dangerous, const char* tag, SrsContextId cid, const char* level, int* psize)
{

View file

@ -52,7 +52,7 @@ public:
class SrsConsoleLog : public ISrsLog
{
private:
SrsLogLevel level;
SrsLogLevel level_;
bool utc;
private:
char* buffer;
@ -63,11 +63,7 @@ public:
public:
virtual srs_error_t initialize();
virtual void reopen();
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, ...);
virtual void log(SrsLogLevel level, const char* tag, const SrsContextId& context_id, const char* fmt, va_list args);
};
// Generate the log header.