1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/src/app/srs_app_log.cpp

328 lines
7.7 KiB
C++
Raw Normal View History

2017-03-25 09:21:39 +00:00
/**
* The MIT License (MIT)
*
2019-12-30 02:10:35 +00:00
* Copyright (c) 2013-2020 Winlin
2017-03-25 09:21:39 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
2014-03-01 10:06:20 +00:00
#include <srs_app_log.hpp>
2014-03-01 10:06:20 +00:00
#include <stdarg.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <srs_app_config.hpp>
#include <srs_kernel_error.hpp>
2014-04-12 13:35:26 +00:00
#include <srs_app_utility.hpp>
#include <srs_kernel_utility.hpp>
2014-03-01 10:06:20 +00:00
// the max size of a line of log.
#define LOG_MAX_SIZE 4096
// the tail append to each log.
#define LOG_TAIL '\n'
// reserved for the end of log data, it must be strlen(LOG_TAIL)
#define LOG_TAIL_SIZE 1
2020-06-25 05:14:59 +00:00
SrsFileLog::SrsFileLog()
2014-03-01 10:06:20 +00:00
{
2017-03-26 02:16:21 +00:00
level = SrsLogLevelTrace;
2014-03-01 10:06:20 +00:00
log_data = new char[LOG_MAX_SIZE];
2017-03-25 09:21:39 +00:00
fd = -1;
2014-04-12 13:35:26 +00:00
log_to_file_tank = false;
2015-09-09 15:37:07 +00:00
utc = false;
2014-03-01 10:06:20 +00:00
}
2020-06-25 05:14:59 +00:00
SrsFileLog::~SrsFileLog()
2014-03-01 10:06:20 +00:00
{
srs_freepa(log_data);
2017-03-25 09:21:39 +00:00
if (fd > 0) {
::close(fd);
fd = -1;
}
2017-03-25 09:21:39 +00:00
if (_srs_config) {
_srs_config->unsubscribe(this);
}
}
2020-06-25 05:14:59 +00:00
srs_error_t SrsFileLog::initialize()
{
if (_srs_config) {
_srs_config->subscribe(this);
2017-03-25 09:21:39 +00:00
log_to_file_tank = _srs_config->get_log_tank_file();
2017-03-26 02:16:21 +00:00
level = srs_get_log_level(_srs_config->get_log_level());
2015-09-09 15:37:07 +00:00
utc = _srs_config->get_utc_time();
}
2014-04-12 13:35:26 +00:00
return srs_success;
}
2020-06-25 05:14:59 +00:00
void SrsFileLog::reopen()
{
if (fd > 0) {
::close(fd);
}
if (!log_to_file_tank) {
return;
}
open_log_file();
}
void SrsFileLog::verbose(const char* tag, SrsContextId context_id, const char* fmt, ...)
2014-03-01 10:06:20 +00:00
{
2017-03-26 02:16:21 +00:00
if (level > SrsLogLevelVerbose) {
2014-03-01 10:06:20 +00:00
return;
}
int size = 0;
2017-03-26 02:16:21 +00:00
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Verb", &size)) {
2014-03-01 10:06:20 +00:00
return;
}
va_list ap;
va_start(ap, fmt);
// we reserved 1 bytes for the new line.
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap);
2017-03-25 09:21:39 +00:00
2017-03-26 02:16:21 +00:00
write_log(fd, log_data, size, SrsLogLevelVerbose);
2014-03-01 10:06:20 +00:00
}
void SrsFileLog::info(const char* tag, SrsContextId context_id, const char* fmt, ...)
2014-03-01 10:06:20 +00:00
{
2017-03-26 02:16:21 +00:00
if (level > SrsLogLevelInfo) {
2014-03-01 10:06:20 +00:00
return;
}
int size = 0;
2017-03-26 02:16:21 +00:00
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Debug", &size)) {
2014-03-01 10:06:20 +00:00
return;
}
va_list ap;
va_start(ap, fmt);
// we reserved 1 bytes for the new line.
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap);
2017-03-25 09:21:39 +00:00
2017-03-26 02:16:21 +00:00
write_log(fd, log_data, size, SrsLogLevelInfo);
2014-03-01 10:06:20 +00:00
}
void SrsFileLog::trace(const char* tag, SrsContextId context_id, const char* fmt, ...)
2014-03-01 10:06:20 +00:00
{
2017-03-26 02:16:21 +00:00
if (level > SrsLogLevelTrace) {
2014-03-01 10:06:20 +00:00
return;
}
int size = 0;
2017-03-26 02:16:21 +00:00
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Trace", &size)) {
2014-03-01 10:06:20 +00:00
return;
}
va_list ap;
va_start(ap, fmt);
// we reserved 1 bytes for the new line.
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap);
2017-03-25 09:21:39 +00:00
2017-03-26 02:16:21 +00:00
write_log(fd, log_data, size, SrsLogLevelTrace);
2014-03-01 10:06:20 +00:00
}
void SrsFileLog::warn(const char* tag, SrsContextId context_id, const char* fmt, ...)
2014-03-01 10:06:20 +00:00
{
2017-03-26 02:16:21 +00:00
if (level > SrsLogLevelWarn) {
2014-03-01 10:06:20 +00:00
return;
}
int size = 0;
2017-03-26 02:16:21 +00:00
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, true, tag, context_id, "Warn", &size)) {
2014-03-01 10:06:20 +00:00
return;
}
va_list ap;
va_start(ap, fmt);
// we reserved 1 bytes for the new line.
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap);
2017-03-25 09:21:39 +00:00
2017-03-26 02:16:21 +00:00
write_log(fd, log_data, size, SrsLogLevelWarn);
2014-03-01 10:06:20 +00:00
}
void SrsFileLog::error(const char* tag, SrsContextId context_id, const char* fmt, ...)
2014-03-01 10:06:20 +00:00
{
2017-03-26 02:16:21 +00:00
if (level > SrsLogLevelError) {
2014-03-01 10:06:20 +00:00
return;
}
int size = 0;
2017-03-26 02:16:21 +00:00
if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, true, tag, context_id, "Error", &size)) {
2014-03-01 10:06:20 +00:00
return;
}
va_list ap;
va_start(ap, fmt);
// we reserved 1 bytes for the new line.
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap);
2017-03-25 09:21:39 +00:00
// add strerror() to error msg.
// Check size to avoid security issue https://github.com/ossrs/srs/issues/1229
if (errno != 0 && size < LOG_MAX_SIZE) {
size += snprintf(log_data + size, LOG_MAX_SIZE - size, "(%s)", strerror(errno));
}
2017-03-25 09:21:39 +00:00
2017-03-26 02:16:21 +00:00
write_log(fd, log_data, size, SrsLogLevelError);
2014-03-01 10:06:20 +00:00
}
2020-06-25 05:14:59 +00:00
srs_error_t SrsFileLog::on_reload_utc_time()
2015-09-09 15:37:07 +00:00
{
utc = _srs_config->get_utc_time();
return srs_success;
2015-09-09 15:37:07 +00:00
}
2020-06-25 05:14:59 +00:00
srs_error_t SrsFileLog::on_reload_log_tank()
2014-04-12 13:35:26 +00:00
{
2017-09-22 08:14:30 +00:00
srs_error_t err = srs_success;
if (!_srs_config) {
2017-09-22 08:14:30 +00:00
return err;
}
2017-03-25 09:21:39 +00:00
2014-04-12 13:35:26 +00:00
bool tank = log_to_file_tank;
log_to_file_tank = _srs_config->get_log_tank_file();
2017-03-25 09:21:39 +00:00
2014-04-12 13:35:26 +00:00
if (tank) {
2017-09-22 08:14:30 +00:00
return err;
2014-04-12 13:35:26 +00:00
}
2017-03-25 09:21:39 +00:00
2014-04-12 13:35:26 +00:00
if (!log_to_file_tank) {
2017-09-22 08:14:30 +00:00
return err;
2014-04-12 13:35:26 +00:00
}
2017-03-25 09:21:39 +00:00
2014-04-12 13:35:26 +00:00
if (fd > 0) {
::close(fd);
}
open_log_file();
2017-09-22 08:14:30 +00:00
return err;
2014-04-12 13:35:26 +00:00
}
2020-06-25 05:14:59 +00:00
srs_error_t SrsFileLog::on_reload_log_level()
2014-04-12 13:35:26 +00:00
{
2017-09-22 08:14:30 +00:00
srs_error_t err = srs_success;
2014-04-12 13:35:26 +00:00
if (!_srs_config) {
2017-09-22 08:14:30 +00:00
return err;
}
2017-03-26 02:16:21 +00:00
level = srs_get_log_level(_srs_config->get_log_level());
2014-04-12 13:35:26 +00:00
2017-09-22 08:14:30 +00:00
return err;
2014-04-12 13:35:26 +00:00
}
2020-06-25 05:14:59 +00:00
srs_error_t SrsFileLog::on_reload_log_file()
2014-04-12 13:35:26 +00:00
{
2017-09-22 08:14:30 +00:00
srs_error_t err = srs_success;
if (!_srs_config) {
2017-09-22 08:14:30 +00:00
return err;
}
2017-03-25 09:21:39 +00:00
2014-04-12 13:35:26 +00:00
if (!log_to_file_tank) {
2017-09-22 08:14:30 +00:00
return err;
2014-04-12 13:35:26 +00:00
}
2017-03-25 09:21:39 +00:00
2014-04-12 13:35:26 +00:00
if (fd > 0) {
::close(fd);
}
open_log_file();
2017-09-22 08:14:30 +00:00
return err;
2014-04-12 13:35:26 +00:00
}
2020-06-25 05:14:59 +00:00
void SrsFileLog::write_log(int& fd, char *str_log, int size, int level)
2014-03-01 10:06:20 +00:00
{
// ensure the tail and EOF of string
// LOG_TAIL_SIZE for the TAIL char.
// 1 for the last char(0).
size = srs_min(LOG_MAX_SIZE - 1 - LOG_TAIL_SIZE, size);
// add some to the end of char.
str_log[size++] = LOG_TAIL;
2014-03-01 10:06:20 +00:00
2014-04-12 13:35:26 +00:00
// if not to file, to console and return.
if (!log_to_file_tank) {
// if is error msg, then print color msg.
// \033[31m : red text code in shell
// \033[32m : green text code in shell
// \033[33m : yellow text code in shell
// \033[0m : normal text code
2017-03-26 02:16:21 +00:00
if (level <= SrsLogLevelTrace) {
printf("%.*s", size, str_log);
2017-03-26 02:16:21 +00:00
} else if (level == SrsLogLevelWarn) {
printf("\033[33m%.*s\033[0m", size, str_log);
} else{
printf("\033[31m%.*s\033[0m", size, str_log);
}
fflush(stdout);
2017-03-25 09:21:39 +00:00
2014-04-12 13:35:26 +00:00
return;
}
// open log file. if specified
2014-04-12 13:35:26 +00:00
if (fd < 0) {
open_log_file();
}
2014-03-23 05:53:07 +00:00
// write log to file.
2014-04-12 13:35:26 +00:00
if (fd > 0) {
2014-03-23 05:53:07 +00:00
::write(fd, str_log, size);
}
2014-03-01 10:06:20 +00:00
}
2014-04-12 13:35:26 +00:00
2020-06-25 05:14:59 +00:00
void SrsFileLog::open_log_file()
2014-04-12 13:35:26 +00:00
{
if (!_srs_config) {
return;
}
std::string filename = _srs_config->get_log_file();
2014-04-12 13:35:26 +00:00
if (filename.empty()) {
return;
}
fd = ::open(filename.c_str(),
O_RDWR | O_CREAT | O_APPEND,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH
);
2014-04-12 13:35:26 +00:00
}
2014-08-02 14:18:39 +00:00