winlin

support reload log level/logfile/tank

... ... @@ -516,6 +516,42 @@ int SrsConfig::reload()
}
srs_trace("reload pid success.");
}
// merge config: srs_log_tank
if (!srs_directive_equals(root->get("srs_log_tank"), old_root->get("srs_log_tank"))) {
for (it = subscribes.begin(); it != subscribes.end(); ++it) {
ISrsReloadHandler* subscribe = *it;
if ((ret = subscribe->on_reload_log_tank()) != ERROR_SUCCESS) {
srs_error("notify subscribes reload srs_log_tank failed. ret=%d", ret);
return ret;
}
}
srs_trace("reload srs_log_tank success.");
}
// merge config: srs_log_level
if (!srs_directive_equals(root->get("srs_log_level"), old_root->get("srs_log_level"))) {
for (it = subscribes.begin(); it != subscribes.end(); ++it) {
ISrsReloadHandler* subscribe = *it;
if ((ret = subscribe->on_reload_log_level()) != ERROR_SUCCESS) {
srs_error("notify subscribes reload srs_log_level failed. ret=%d", ret);
return ret;
}
}
srs_trace("reload srs_log_level success.");
}
// merge config: srs_log_file
if (!srs_directive_equals(root->get("srs_log_file"), old_root->get("srs_log_file"))) {
for (it = subscribes.begin(); it != subscribes.end(); ++it) {
ISrsReloadHandler* subscribe = *it;
if ((ret = subscribe->on_reload_log_file()) != ERROR_SUCCESS) {
srs_error("notify subscribes reload srs_log_file failed. ret=%d", ret);
return ret;
}
}
srs_trace("reload srs_log_file success.");
}
// directly supported for reload:
// chunk_size, ff_log_dir
... ...
... ... @@ -32,6 +32,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_app_config.hpp>
#include <srs_kernel_error.hpp>
#include <srs_app_utility.hpp>
SrsThreadContext::SrsThreadContext()
{
... ... @@ -66,6 +67,7 @@ SrsFastLog::SrsFastLog()
log_data = new char[LOG_MAX_SIZE];
fd = -1;
log_to_file_tank = false;
}
SrsFastLog::~SrsFastLog()
... ... @@ -76,22 +78,20 @@ SrsFastLog::~SrsFastLog()
::close(fd);
fd = -1;
}
}
int SrsFastLog::initialize()
{
// TODO: support reload.
return ERROR_SUCCESS;
_srs_config->unsubscribe(this);
}
int SrsFastLog::level()
int SrsFastLog::initialize()
{
return _level;
}
int ret = ERROR_SUCCESS;
_srs_config->subscribe(this);
void SrsFastLog::set_level(int level)
{
_level = level;
log_to_file_tank = _srs_config->get_srs_log_tank_file();
_level = srs_get_log_level(_srs_config->get_srs_log_level());
return ret;
}
void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...)
... ... @@ -197,6 +197,54 @@ void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...)
write_log(fd, log_data, size, SrsLogLevel::Error);
}
int SrsFastLog::on_reload_log_tank()
{
int ret = ERROR_SUCCESS;
bool tank = log_to_file_tank;
log_to_file_tank = _srs_config->get_srs_log_tank_file();
if (tank) {
return ret;
}
if (!log_to_file_tank) {
return ret;
}
if (fd > 0) {
::close(fd);
}
open_log_file();
return ret;
}
int SrsFastLog::on_reload_log_level()
{
int ret = ERROR_SUCCESS;
_level = srs_get_log_level(_srs_config->get_srs_log_level());
return ret;
}
int SrsFastLog::on_reload_log_file()
{
int ret = ERROR_SUCCESS;
if (!log_to_file_tank) {
return ret;
}
if (fd > 0) {
::close(fd);
}
open_log_file();
return ret;
}
bool SrsFastLog::generate_header(const char* tag, int context_id, const char* level_name, int* header_size)
{
// clock time
... ... @@ -247,7 +295,8 @@ void SrsFastLog::write_log(int& fd, char *str_log, int size, int level)
str_log[size++] = LOG_TAIL;
str_log[size++] = 0;
if (fd < 0 || !_srs_config->get_srs_log_tank_file()) {
// 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
... ... @@ -260,24 +309,35 @@ void SrsFastLog::write_log(int& fd, char *str_log, int size, int level)
} else{
printf("\033[31m%s\033[0m", str_log);
}
return;
}
// open log file. if specified
if (!_srs_config->get_srs_log_file().empty() && fd < 0) {
std::string filename = _srs_config->get_srs_log_file();
fd = ::open(filename.c_str(), O_RDWR | O_APPEND);
if(fd == -1 && errno == ENOENT) {
fd = open(filename.c_str(),
O_RDWR | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH
);
}
if (fd < 0) {
open_log_file();
}
// write log to file.
if (fd > 0 && _srs_config->get_srs_log_tank_file()) {
if (fd > 0) {
::write(fd, str_log, size);
}
}
void SrsFastLog::open_log_file()
{
std::string filename = _srs_config->get_srs_log_file();
if (filename.empty()) {
return;
}
fd = ::open(filename.c_str(), O_RDWR | O_APPEND);
if(fd == -1 && errno == ENOENT) {
fd = open(filename.c_str(),
O_RDWR | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH
);
}
}
... ...
... ... @@ -32,6 +32,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_app_st.hpp>
#include <srs_kernel_log.hpp>
#include <srs_app_reload.hpp>
#include <string.h>
... ... @@ -57,7 +58,7 @@ public:
/**
* we use memory/disk cache and donot flush when write log.
*/
class SrsFastLog : public ISrsLog
class SrsFastLog : public ISrsLog, public ISrsReloadHandler
{
private:
// defined in SrsLogLevel.
... ... @@ -65,21 +66,27 @@ private:
char* log_data;
// log to file if specified srs_log_file
int fd;
// whether log to file tank
bool log_to_file_tank;
public:
SrsFastLog();
virtual ~SrsFastLog();
public:
virtual int initialize();
virtual int level();
virtual void set_level(int level);
virtual void verbose(const char* tag, int context_id, const char* fmt, ...);
virtual void info(const char* tag, int context_id, const char* fmt, ...);
virtual void trace(const char* tag, int context_id, const char* fmt, ...);
virtual void warn(const char* tag, int context_id, const char* fmt, ...);
virtual void error(const char* tag, int context_id, const char* fmt, ...);
// interface ISrsThreadHandler.
public:
virtual int on_reload_log_tank();
virtual int on_reload_log_level();
virtual int on_reload_log_file();
private:
virtual bool generate_header(const char* tag, int context_id, const char* level_name, int* header_size);
static void write_log(int& fd, char* str_log, int size, int level);
virtual void write_log(int& fd, char* str_log, int size, int level);
virtual void open_log_file();
};
#endif
... ...
... ... @@ -45,6 +45,21 @@ int ISrsReloadHandler::on_reload_pid()
return ERROR_SUCCESS;
}
int ISrsReloadHandler::on_reload_log_tank()
{
return ERROR_SUCCESS;
}
int ISrsReloadHandler::on_reload_log_level()
{
return ERROR_SUCCESS;
}
int ISrsReloadHandler::on_reload_log_file()
{
return ERROR_SUCCESS;
}
int ISrsReloadHandler::on_reload_pithy_print()
{
return ERROR_SUCCESS;
... ...
... ... @@ -43,6 +43,9 @@ public:
public:
virtual int on_reload_listen();
virtual int on_reload_pid();
virtual int on_reload_log_tank();
virtual int on_reload_log_level();
virtual int on_reload_log_file();
virtual int on_reload_pithy_print();
virtual int on_reload_vhost_added(std::string vhost);
virtual int on_reload_vhost_removed(std::string vhost);
... ...
... ... @@ -38,15 +38,6 @@ int ISrsLog::initialize()
return ERROR_SUCCESS;
}
int ISrsLog::level()
{
return SrsLogLevel::Trace;
}
void ISrsLog::set_level(int /*level*/)
{
}
void ISrsLog::verbose(const char* /*tag*/, int /*context_id*/, const char* /*fmt*/, ...)
{
}
... ...
... ... @@ -69,11 +69,6 @@ public:
virtual int initialize();
public:
/**
* defined in SrsLogLevel.
*/
virtual int level();
virtual void set_level(int level);
/**
* log for verbose, very verbose information.
*/
virtual void verbose(const char* tag, int context_id, const char* fmt, ...);
... ...
... ... @@ -98,7 +98,6 @@ int main(int argc, char** argv)
if ((ret = _srs_log->initialize()) != ERROR_SUCCESS) {
return ret;
}
_srs_log->set_level(srs_get_log_level(_srs_config->get_srs_log_level()));
srs_trace("srs(simple-rtmp-server) "RTMP_SIG_SRS_VERSION);
srs_trace("uname: "SRS_UNAME);
... ...