1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-24 06:54:22 +00:00
srs/trunk/src/protocol/srs_protocol_log.cpp

321 lines
7.7 KiB
C++
Raw Normal View History

//
2022-06-20 11:22:25 +00:00
// Copyright (c) 2013-2022 The SRS Authors
//
// SPDX-License-Identifier: MIT or MulanPSL-2.0
//
2017-03-26 02:16:21 +00:00
2022-06-09 11:59:51 +00:00
#include <srs_protocol_log.hpp>
2017-03-26 02:16:21 +00:00
#include <stdarg.h>
#include <sys/time.h>
#include <unistd.h>
2020-06-18 03:45:43 +00:00
#include <sstream>
2017-03-26 02:16:21 +00:00
using namespace std;
#include <srs_kernel_error.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_protocol_utility.hpp>
2017-03-26 02:16:21 +00:00
2021-02-07 13:25:51 +00:00
#include <srs_protocol_kbps.hpp>
SrsPps* _srs_pps_cids_get = NULL;
SrsPps* _srs_pps_cids_set = NULL;
2021-02-07 13:25:51 +00:00
2020-07-12 11:52:03 +00:00
#define SRS_BASIC_LOG_SIZE 8192
2017-03-26 02:16:21 +00:00
SrsThreadContext::SrsThreadContext()
{
}
SrsThreadContext::~SrsThreadContext()
{
}
SrsContextId SrsThreadContext::generate_id()
2017-03-26 02:16:21 +00:00
{
2020-08-30 01:53:10 +00:00
SrsContextId cid = SrsContextId();
return cid.set_value(srs_random_str(8));
2017-03-26 02:16:21 +00:00
}
static SrsContextId _srs_context_default;
static int _srs_context_key = -1;
void _srs_context_destructor(void* arg)
{
SrsContextId* cid = (SrsContextId*)arg;
srs_freep(cid);
}
2020-07-13 03:19:34 +00:00
const SrsContextId& SrsThreadContext::get_id()
2017-03-26 02:16:21 +00:00
{
2021-02-07 13:25:51 +00:00
++_srs_pps_cids_get->sugar;
if (!srs_thread_self()) {
return _srs_context_default;
}
void* cid = srs_thread_getspecific(_srs_context_key);
if (!cid) {
return _srs_context_default;
}
return *(SrsContextId*)cid;
2017-03-26 02:16:21 +00:00
}
2020-07-13 03:19:34 +00:00
const SrsContextId& SrsThreadContext::set_id(const SrsContextId& v)
2017-03-26 02:16:21 +00:00
{
2021-02-07 13:25:51 +00:00
++_srs_pps_cids_set->sugar;
if (!srs_thread_self()) {
_srs_context_default = v;
2020-07-13 03:19:34 +00:00
return v;
2017-03-26 02:16:21 +00:00
}
2020-07-13 03:19:34 +00:00
2021-02-07 13:39:09 +00:00
SrsContextId* cid = new SrsContextId();
*cid = v;
if (_srs_context_key < 0) {
int r0 = srs_key_create(&_srs_context_key, _srs_context_destructor);
srs_assert(r0 == 0);
}
int r0 = srs_thread_setspecific(_srs_context_key, cid);
srs_assert(r0 == 0);
return v;
2017-03-26 02:16:21 +00:00
}
void SrsThreadContext::clear_cid()
{
}
2020-07-13 05:52:23 +00:00
impl_SrsContextRestore::impl_SrsContextRestore(SrsContextId cid)
{
cid_ = cid;
}
impl_SrsContextRestore::~impl_SrsContextRestore()
{
_srs_context->set_id(cid_);
}
2020-01-08 06:24:41 +00:00
// LCOV_EXCL_START
2017-03-26 02:16:21 +00:00
SrsConsoleLog::SrsConsoleLog(SrsLogLevel l, bool u)
{
level = l;
utc = u;
buffer = new char[SRS_BASIC_LOG_SIZE];
}
SrsConsoleLog::~SrsConsoleLog()
{
srs_freepa(buffer);
}
srs_error_t SrsConsoleLog::initialize()
2017-03-26 02:16:21 +00:00
{
return srs_success;
2017-03-26 02:16:21 +00:00
}
void SrsConsoleLog::reopen()
{
}
void SrsConsoleLog::verbose(const char* tag, SrsContextId context_id, const char* fmt, ...)
2017-03-26 02:16:21 +00:00
{
if (level > SrsLogLevelVerbose) {
return;
}
int size = 0;
if (!srs_log_header(buffer, SRS_BASIC_LOG_SIZE, utc, false, tag, context_id, "Verb", &size)) {
return;
}
va_list ap;
va_start(ap, fmt);
int r0 = vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
2017-03-26 02:16:21 +00:00
va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= SRS_BASIC_LOG_SIZE - size) {
return;
}
size += r0;
2017-03-26 02:16:21 +00:00
2017-03-26 09:18:34 +00:00
fprintf(stdout, "%s\n", buffer);
2017-03-26 02:16:21 +00:00
}
void SrsConsoleLog::info(const char* tag, SrsContextId context_id, const char* fmt, ...)
2017-03-26 02:16:21 +00:00
{
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);
2017-03-26 02:16:21 +00:00
va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= SRS_BASIC_LOG_SIZE - size) {
return;
}
size += r0;
2017-03-26 02:16:21 +00:00
2017-03-26 09:18:34 +00:00
fprintf(stdout, "%s\n", buffer);
2017-03-26 02:16:21 +00:00
}
void SrsConsoleLog::trace(const char* tag, SrsContextId context_id, const char* fmt, ...)
2017-03-26 02:16:21 +00:00
{
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);
2017-03-26 02:16:21 +00:00
va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= SRS_BASIC_LOG_SIZE - size) {
return;
}
size += r0;
2017-03-26 02:16:21 +00:00
2017-03-26 09:18:34 +00:00
fprintf(stdout, "%s\n", buffer);
2017-03-26 02:16:21 +00:00
}
void SrsConsoleLog::warn(const char* tag, SrsContextId context_id, const char* fmt, ...)
2017-03-26 02:16:21 +00:00
{
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);
2017-03-26 02:16:21 +00:00
va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= SRS_BASIC_LOG_SIZE - size) {
return;
}
size += r0;
2017-03-26 02:16:21 +00:00
2017-03-26 09:18:34 +00:00
fprintf(stderr, "%s\n", buffer);
2017-03-26 02:16:21 +00:00
}
void SrsConsoleLog::error(const char* tag, SrsContextId context_id, const char* fmt, ...)
2017-03-26 02:16:21 +00:00
{
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);
2017-03-26 02:16:21 +00:00
va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= SRS_BASIC_LOG_SIZE - size) {
return;
}
size += r0;
2017-03-26 02:16:21 +00:00
// add strerror() to error msg.
if (errno != 0) {
r0 = snprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, "(%s)", strerror(errno));
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= SRS_BASIC_LOG_SIZE - size) {
return;
}
size += r0;
2017-03-26 02:16:21 +00:00
}
2017-03-26 09:18:34 +00:00
fprintf(stderr, "%s\n", buffer);
2017-03-26 02:16:21 +00:00
}
2020-01-08 06:24:41 +00:00
// LCOV_EXCL_STOP
2017-03-26 02:16:21 +00:00
bool srs_log_header(char* buffer, int size, bool utc, bool dangerous, const char* tag, SrsContextId cid, const char* level, int* psize)
2017-03-26 02:16:21 +00:00
{
// clock time
timeval tv;
if (gettimeofday(&tv, NULL) == -1) {
return false;
}
// to calendar time
2021-07-17 11:43:22 +00:00
struct tm now;
// Each of these functions returns NULL in case an error was detected. @see https://linux.die.net/man/3/localtime_r
2017-03-26 02:16:21 +00:00
if (utc) {
2021-07-17 11:43:22 +00:00
if (gmtime_r(&tv.tv_sec, &now) == NULL) {
2017-03-26 02:16:21 +00:00
return false;
}
} else {
2021-07-17 11:43:22 +00:00
if (localtime_r(&tv.tv_sec, &now) == NULL) {
2017-03-26 02:16:21 +00:00
return false;
}
}
2017-05-31 05:52:21 +00:00
int written = -1;
2017-03-26 02:16:21 +00:00
if (dangerous) {
if (tag) {
2017-05-31 05:52:21 +00:00
written = snprintf(buffer, size,
2020-09-10 03:07:21 +00:00
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%s][%d][%s] ",
2021-07-17 11:43:22 +00:00
1900 + now.tm_year, 1 + now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec, (int)(tv.tv_usec / 1000),
2020-09-10 03:07:21 +00:00
level, getpid(), cid.c_str(), errno, tag);
2017-03-26 02:16:21 +00:00
} else {
2017-05-31 05:52:21 +00:00
written = snprintf(buffer, size,
2020-06-18 03:45:43 +00:00
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%s][%d] ",
2021-07-17 11:43:22 +00:00
1900 + now.tm_year, 1 + now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec, (int)(tv.tv_usec / 1000),
level, getpid(), cid.c_str(), errno);
2017-03-26 02:16:21 +00:00
}
} else {
if (tag) {
2017-05-31 05:52:21 +00:00
written = snprintf(buffer, size,
2020-09-10 03:07:21 +00:00
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%s][%s] ",
2021-07-17 11:43:22 +00:00
1900 + now.tm_year, 1 + now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec, (int)(tv.tv_usec / 1000),
2020-09-10 03:07:21 +00:00
level, getpid(), cid.c_str(), tag);
2017-03-26 02:16:21 +00:00
} else {
2017-05-31 05:52:21 +00:00
written = snprintf(buffer, size,
2020-06-18 03:45:43 +00:00
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%s] ",
2021-07-17 11:43:22 +00:00
1900 + now.tm_year, 1 + now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec, (int)(tv.tv_usec / 1000),
level, getpid(), cid.c_str());
2017-03-26 02:16:21 +00:00
}
}
// Exceed the size, ignore this log.
// Check size to avoid security issue https://github.com/ossrs/srs/issues/1229
if (written <= 0 || written >= size) {
2017-03-26 02:16:21 +00:00
return false;
}
// write the header size.
2017-05-31 05:52:21 +00:00
*psize = written;
2017-03-26 02:16:21 +00:00
return true;
}