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
|
|
|
|
2014-03-02 13:49:09 +00:00
|
|
|
#include <srs_app_log.hpp>
|
2014-03-01 10:06:20 +00:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2014-03-23 04:21:36 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2017-05-30 01:05:02 +00:00
|
|
|
#include <unistd.h>
|
2014-03-23 04:21:36 +00:00
|
|
|
|
|
|
|
#include <srs_app_config.hpp>
|
2014-04-12 12:35:40 +00:00
|
|
|
#include <srs_kernel_error.hpp>
|
2014-04-12 13:35:26 +00:00
|
|
|
#include <srs_app_utility.hpp>
|
2014-06-08 05:03:03 +00:00
|
|
|
#include <srs_kernel_utility.hpp>
|
2014-03-23 04:21:36 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
SrsFastLog::SrsFastLog()
|
|
|
|
{
|
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
|
|
|
|
2014-03-23 04:21:36 +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
|
|
|
}
|
|
|
|
|
|
|
|
SrsFastLog::~SrsFastLog()
|
|
|
|
{
|
2015-11-02 03:02:27 +00:00
|
|
|
srs_freepa(log_data);
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2014-03-23 04:21:36 +00:00
|
|
|
if (fd > 0) {
|
|
|
|
::close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2014-07-26 06:43:37 +00:00
|
|
|
if (_srs_config) {
|
|
|
|
_srs_config->unsubscribe(this);
|
|
|
|
}
|
2014-04-12 12:35:40 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 06:03:19 +00:00
|
|
|
srs_error_t SrsFastLog::initialize()
|
2014-04-03 10:32:51 +00:00
|
|
|
{
|
2014-07-26 06:43:37 +00:00
|
|
|
if (_srs_config) {
|
|
|
|
_srs_config->subscribe(this);
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2014-07-26 06:43:37 +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-07-26 06:43:37 +00:00
|
|
|
}
|
2014-04-12 13:35:26 +00:00
|
|
|
|
2017-06-11 06:03:19 +00:00
|
|
|
return srs_success;
|
2014-04-03 10:32:51 +00:00
|
|
|
}
|
|
|
|
|
2016-12-08 10:23:22 +00:00
|
|
|
void SrsFastLog::reopen()
|
|
|
|
{
|
|
|
|
if (fd > 0) {
|
|
|
|
::close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!log_to_file_tank) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
open_log_file();
|
|
|
|
}
|
|
|
|
|
2014-03-01 10:06:20 +00:00
|
|
|
void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...)
|
|
|
|
{
|
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 SrsFastLog::info(const char* tag, int context_id, const char* fmt, ...)
|
|
|
|
{
|
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 SrsFastLog::trace(const char* tag, int context_id, const char* fmt, ...)
|
|
|
|
{
|
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 SrsFastLog::warn(const char* tag, int context_id, const char* fmt, ...)
|
|
|
|
{
|
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 SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...)
|
|
|
|
{
|
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
|
|
|
|
2014-03-14 20:53:46 +00:00
|
|
|
// add strerror() to error msg.
|
2019-12-11 03:56:00 +00:00
|
|
|
// Check size to avoid security issue https://github.com/ossrs/srs/issues/1229
|
|
|
|
if (errno != 0 && size < LOG_MAX_SIZE) {
|
2014-07-26 07:34:45 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-29 12:02:38 +00:00
|
|
|
srs_error_t SrsFastLog::on_reload_utc_time()
|
2015-09-09 15:37:07 +00:00
|
|
|
{
|
|
|
|
utc = _srs_config->get_utc_time();
|
|
|
|
|
2017-07-29 12:02:38 +00:00
|
|
|
return srs_success;
|
2015-09-09 15:37:07 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 08:14:30 +00:00
|
|
|
srs_error_t SrsFastLog::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;
|
2014-07-26 06:58:33 +00:00
|
|
|
|
|
|
|
if (!_srs_config) {
|
2017-09-22 08:14:30 +00:00
|
|
|
return err;
|
2014-07-26 06:58:33 +00:00
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2014-04-12 13:35:26 +00:00
|
|
|
bool tank = log_to_file_tank;
|
2014-05-18 08:15:35 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-09-22 08:14:30 +00:00
|
|
|
srs_error_t SrsFastLog::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
|
|
|
|
2014-07-26 06:58:33 +00:00
|
|
|
if (!_srs_config) {
|
2017-09-22 08:14:30 +00:00
|
|
|
return err;
|
2014-07-26 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-09-22 08:14:30 +00:00
|
|
|
srs_error_t SrsFastLog::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;
|
2014-07-26 06:58:33 +00:00
|
|
|
|
|
|
|
if (!_srs_config) {
|
2017-09-22 08:14:30 +00:00
|
|
|
return err;
|
2014-07-26 06:58:33 +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
|
|
|
}
|
|
|
|
|
2014-04-03 10:32:51 +00:00
|
|
|
void SrsFastLog::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.
|
2014-04-03 10:32:51 +00:00
|
|
|
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) {
|
2014-03-23 06:40:55 +00:00
|
|
|
// 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) {
|
2015-01-02 04:56:15 +00:00
|
|
|
printf("%.*s", size, str_log);
|
2017-03-26 02:16:21 +00:00
|
|
|
} else if (level == SrsLogLevelWarn) {
|
2015-01-02 04:56:15 +00:00
|
|
|
printf("\033[33m%.*s\033[0m", size, str_log);
|
2014-03-23 06:40:55 +00:00
|
|
|
} else{
|
2015-01-02 04:56:15 +00:00
|
|
|
printf("\033[31m%.*s\033[0m", size, str_log);
|
2014-03-23 06:40:55 +00:00
|
|
|
}
|
2016-09-08 12:58:52 +00:00
|
|
|
fflush(stdout);
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2014-04-12 13:35:26 +00:00
|
|
|
return;
|
2014-03-14 20:53:46 +00:00
|
|
|
}
|
2014-03-23 06:40:55 +00:00
|
|
|
|
|
|
|
// open log file. if specified
|
2014-04-12 13:35:26 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
open_log_file();
|
2014-03-23 04:21:36 +00:00
|
|
|
}
|
2014-03-23 06:40:55 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
void SrsFastLog::open_log_file()
|
|
|
|
{
|
2014-07-26 06:58:33 +00:00
|
|
|
if (!_srs_config) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-18 08:15:35 +00:00
|
|
|
std::string filename = _srs_config->get_log_file();
|
2014-04-12 13:35:26 +00:00
|
|
|
|
|
|
|
if (filename.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-08 10:05:42 +00:00
|
|
|
|
2020-01-09 02:03:17 +00:00
|
|
|
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
|
|
|
|