Provide new srs log tank mode: ALL

The ALL mode will show logs at both console and save to file.
This commit is contained in:
Jason-JP-Yang 2025-05-29 18:50:45 +08:00
parent 241a33b7b5
commit 5853becb61
5 changed files with 56 additions and 5 deletions

View File

@ -4,7 +4,7 @@
listen 1935;
max_connections 1000;
daemon off;
srs_log_tank console;
srs_log_tank all;
http_api {
enabled on;
listen 1985;

View File

@ -6678,10 +6678,12 @@ string SrsConfig::get_ingest_input_url(SrsConfDirective* conf)
bool SrsConfig::get_log_tank_file()
{
if (!srs_getenv("srs.srs_log_tank").empty()) { // SRS_SRS_LOG_TANK
return srs_getenv("srs.srs_log_tank") != "console";
string tank = srs_getenv("srs.srs_log_tank");
return tank != "console" && tank != "";
}
if (!srs_getenv("srs.log_tank").empty()) { // SRS_LOG_TANK
return srs_getenv("srs.log_tank") != "console";
string tank = srs_getenv("srs.log_tank");
return tank != "console" && tank != "";
}
static bool DEFAULT = true;
@ -6695,7 +6697,28 @@ bool SrsConfig::get_log_tank_file()
return DEFAULT;
}
return conf->arg0() != "console";
string tank = conf->arg0();
return tank != "console" && tank != "";
}
bool SrsConfig::get_log_tank_all()
{
if (!srs_getenv("srs.srs_log_tank").empty()) { // SRS_SRS_LOG_TANK
string tank = srs_getenv("srs.srs_log_tank");
return tank == "all";
}
if (!srs_getenv("srs.log_tank").empty()) { // SRS_LOG_TANK
string tank = srs_getenv("srs.log_tank");
return tank == "all";
}
SrsConfDirective* conf = root->get("srs_log_tank");
if (!conf || conf->arg0().empty()) {
return false;
}
string tank = conf->arg0();
return tank == "all";
}
string SrsConfig::get_log_level()

View File

@ -891,6 +891,8 @@ public:
public:
// Whether log to file.
virtual bool get_log_tank_file();
// Whether log to all (both console and file).
virtual bool get_log_tank_all();
// Get the log level.
virtual std::string get_log_level();
virtual std::string get_log_level_v2();

View File

@ -35,6 +35,7 @@ SrsFileLog::SrsFileLog()
fd = -1;
log_to_file_tank = false;
log_to_all_tank = false;
utc = false;
mutex_ = new SrsThreadMutex();
@ -62,6 +63,7 @@ srs_error_t SrsFileLog::initialize()
_srs_config->subscribe(this);
log_to_file_tank = _srs_config->get_log_tank_file();
log_to_all_tank = _srs_config->get_log_tank_all();
utc = _srs_config->get_utc_time();
std::string level = _srs_config->get_log_level();
@ -78,7 +80,7 @@ void SrsFileLog::reopen()
::close(fd);
}
if (!log_to_file_tank) {
if (!log_to_file_tank && !log_to_all_tank) {
return;
}
@ -132,6 +134,28 @@ void SrsFileLog::write_log(int& fd, char *str_log, int size, int level)
// add some to the end of char.
str_log[size++] = LOG_TAIL;
// if log_to_all_tank is true, output to both console and file
if (log_to_all_tank) {
// Output to console first
if (level <= SrsLogLevelTrace) {
printf("%.*s", size, str_log);
} 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);
// Then write to file
if (fd < 0) {
open_log_file();
}
if (fd > 0) {
::write(fd, str_log, size);
}
return;
}
// if not to file, to console and return.
if (!log_to_file_tank) {
// if is error msg, then print color msg.

View File

@ -39,6 +39,8 @@ private:
int fd;
// Whether log to file tank
bool log_to_file_tank;
// Whether log to all (both console and file)
bool log_to_all_tank;
// Whether use utc time.
bool utc;
// TODO: FIXME: use macro define like SRS_MULTI_THREAD_LOG to switch enable log mutex or not.