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

@ -6,6 +6,18 @@
#include <srs_kernel_log.hpp>
#include <stdarg.h>
const char* srs_log_level_strings[] = {
"Forbidden",
"Verb",
"Debug", NULL,
"Trace", NULL, NULL, NULL,
"Warn", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
"Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
"Disabled",
};
ISrsLog::ISrsLog()
{
}
@ -22,4 +34,15 @@ ISrsContext::~ISrsContext()
{
}
void srs_logger_impl(SrsLogLevel level, const char* tag, const SrsContextId& context_id, const char* fmt, ...)
{
if (!_srs_log) return;
va_list args;
va_start(args, fmt);
_srs_log->log(level, tag, context_id, fmt, args);
va_end(args);
}

View file

@ -14,6 +14,7 @@
#include <errno.h>
#include <string.h>
#include <string>
#include <stdarg.h>
#include <srs_kernel_consts.hpp>
@ -33,6 +34,9 @@ enum SrsLogLevel
SrsLogLevelDisabled = 0x20,
};
// Get the level in string.
extern const char* srs_log_level_strings[];
// The log interface provides method to write log.
// but we provides some macro, which enable us to disable the log when compile.
// @see also SmtDebug/SmtTrace/SmtWarn/SmtError which is corresponding to Debug/Trace/Warn/Fatal.
@ -47,17 +51,8 @@ public:
// Reopen the log file for log rotate.
virtual void reopen() = 0;
public:
// The log for verbose, very verbose information.
virtual void verbose(const char* tag, SrsContextId context_id, const char* fmt, ...) = 0;
// The log for debug, detail information.
virtual void info(const char* tag, SrsContextId context_id, const char* fmt, ...) = 0;
// The log for trace, important information.
virtual void trace(const char* tag, SrsContextId 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, SrsContextId 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, SrsContextId context_id, const char* fmt, ...) = 0;
// Write a application level log. All parameters are required except the tag.
virtual void log(SrsLogLevel level, const char* tag, const SrsContextId& context_id, const char* fmt, va_list args) = 0;
};
// The logic context, for example, a RTMP connection, or RTC Session, etc.
@ -82,26 +77,29 @@ public:
virtual const SrsContextId& set_id(const SrsContextId& v) = 0;
};
// @global User must implements the LogContext and define a global instance.
extern ISrsContext* _srs_context;
// @global User must provides a log object
extern ISrsLog* _srs_log;
// @global User must implements the LogContext and define a global instance.
extern ISrsContext* _srs_context;
// Global log function implementation. Please use helper macros, for example, srs_trace or srs_error.
extern void srs_logger_impl(SrsLogLevel level, const char* tag, const SrsContextId& context_id, const char* fmt, ...);
// Log style.
// Use __FUNCTION__ to print c method
// Use __PRETTY_FUNCTION__ to print c++ class:method
#define srs_verbose(msg, ...) _srs_log->verbose(NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_info(msg, ...) _srs_log->info(NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_trace(msg, ...) _srs_log->trace(NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_warn(msg, ...) _srs_log->warn(NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_error(msg, ...) _srs_log->error(NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_verbose(msg, ...) srs_logger_impl(SrsLogLevelVerbose, NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_info(msg, ...) srs_logger_impl(SrsLogLevelInfo, NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_trace(msg, ...) srs_logger_impl(SrsLogLevelTrace, NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_warn(msg, ...) srs_logger_impl(SrsLogLevelWarn, NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_error(msg, ...) srs_logger_impl(SrsLogLevelError, NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
// With tag.
#define srs_verbose2(tag, msg, ...) _srs_log->verbose(tag, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_info2(tag, msg, ...) _srs_log->info(tag, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_trace2(tag, msg, ...) _srs_log->trace(tag, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_warn2(tag, msg, ...) _srs_log->warn(tag, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_error2(tag, msg, ...) _srs_log->error(tag, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_verbose2(tag, msg, ...) srs_logger_impl(SrsLogLevelVerbose, tag, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_info2(tag, msg, ...) srs_logger_impl(SrsLogLevelInfo, tag, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_trace2(tag, msg, ...) srs_logger_impl(SrsLogLevelTrace, tag, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_warn2(tag, msg, ...) srs_logger_impl(SrsLogLevelWarn, tag, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_error2(tag, msg, ...) srs_logger_impl(SrsLogLevelError, tag, _srs_context->get_id(), msg, ##__VA_ARGS__)
// TODO: FIXME: Add more verbose and info logs.
#ifndef SRS_VERBOSE