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

support reload log level/logfile/tank

This commit is contained in:
winlin 2014-04-12 21:35:26 +08:00
parent f652c28485
commit eda1ddc03d
8 changed files with 149 additions and 43 deletions

View file

@ -517,6 +517,42 @@ int SrsConfig::reload()
srs_trace("reload pid success."); 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: // directly supported for reload:
// chunk_size, ff_log_dir // chunk_size, ff_log_dir

View file

@ -32,6 +32,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_app_config.hpp> #include <srs_app_config.hpp>
#include <srs_kernel_error.hpp> #include <srs_kernel_error.hpp>
#include <srs_app_utility.hpp>
SrsThreadContext::SrsThreadContext() SrsThreadContext::SrsThreadContext()
{ {
@ -66,6 +67,7 @@ SrsFastLog::SrsFastLog()
log_data = new char[LOG_MAX_SIZE]; log_data = new char[LOG_MAX_SIZE];
fd = -1; fd = -1;
log_to_file_tank = false;
} }
SrsFastLog::~SrsFastLog() SrsFastLog::~SrsFastLog()
@ -76,22 +78,20 @@ SrsFastLog::~SrsFastLog()
::close(fd); ::close(fd);
fd = -1; fd = -1;
} }
_srs_config->unsubscribe(this);
} }
int SrsFastLog::initialize() int SrsFastLog::initialize()
{ {
// TODO: support reload. int ret = ERROR_SUCCESS;
return ERROR_SUCCESS;
}
int SrsFastLog::level() _srs_config->subscribe(this);
{
return _level;
}
void SrsFastLog::set_level(int level) log_to_file_tank = _srs_config->get_srs_log_tank_file();
{ _level = srs_get_log_level(_srs_config->get_srs_log_level());
_level = level;
return ret;
} }
void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...) 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); 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) bool SrsFastLog::generate_header(const char* tag, int context_id, const char* level_name, int* header_size)
{ {
// clock time // 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++] = LOG_TAIL;
str_log[size++] = 0; 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. // if is error msg, then print color msg.
// \033[31m : red text code in shell // \033[31m : red text code in shell
// \033[32m : green text code in shell // \033[32m : green text code in shell
@ -260,12 +309,29 @@ void SrsFastLog::write_log(int& fd, char *str_log, int size, int level)
} else{ } else{
printf("\033[31m%s\033[0m", str_log); printf("\033[31m%s\033[0m", str_log);
} }
return;
} }
// open log file. if specified // open log file. if specified
if (!_srs_config->get_srs_log_file().empty() && fd < 0) { if (fd < 0) {
open_log_file();
}
// write log to file.
if (fd > 0) {
::write(fd, str_log, size);
}
}
void SrsFastLog::open_log_file()
{
std::string filename = _srs_config->get_srs_log_file(); std::string filename = _srs_config->get_srs_log_file();
if (filename.empty()) {
return;
}
fd = ::open(filename.c_str(), O_RDWR | O_APPEND); fd = ::open(filename.c_str(), O_RDWR | O_APPEND);
if(fd == -1 && errno == ENOENT) { if(fd == -1 && errno == ENOENT) {
@ -275,9 +341,3 @@ void SrsFastLog::write_log(int& fd, char *str_log, int size, int level)
); );
} }
} }
// write log to file.
if (fd > 0 && _srs_config->get_srs_log_tank_file()) {
::write(fd, str_log, size);
}
}

View file

@ -32,6 +32,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_app_st.hpp> #include <srs_app_st.hpp>
#include <srs_kernel_log.hpp> #include <srs_kernel_log.hpp>
#include <srs_app_reload.hpp>
#include <string.h> #include <string.h>
@ -57,7 +58,7 @@ public:
/** /**
* we use memory/disk cache and donot flush when write log. * we use memory/disk cache and donot flush when write log.
*/ */
class SrsFastLog : public ISrsLog class SrsFastLog : public ISrsLog, public ISrsReloadHandler
{ {
private: private:
// defined in SrsLogLevel. // defined in SrsLogLevel.
@ -65,21 +66,27 @@ private:
char* log_data; char* log_data;
// log to file if specified srs_log_file // log to file if specified srs_log_file
int fd; int fd;
// whether log to file tank
bool log_to_file_tank;
public: public:
SrsFastLog(); SrsFastLog();
virtual ~SrsFastLog(); virtual ~SrsFastLog();
public: public:
virtual int initialize(); 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 verbose(const char* tag, int context_id, const char* fmt, ...);
virtual void info(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 trace(const char* tag, int context_id, const char* fmt, ...);
virtual void warn(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, ...); 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: private:
virtual bool generate_header(const char* tag, int context_id, const char* level_name, int* header_size); 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 #endif

View file

@ -45,6 +45,21 @@ int ISrsReloadHandler::on_reload_pid()
return ERROR_SUCCESS; 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() int ISrsReloadHandler::on_reload_pithy_print()
{ {
return ERROR_SUCCESS; return ERROR_SUCCESS;

View file

@ -43,6 +43,9 @@ public:
public: public:
virtual int on_reload_listen(); virtual int on_reload_listen();
virtual int on_reload_pid(); 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_pithy_print();
virtual int on_reload_vhost_added(std::string vhost); virtual int on_reload_vhost_added(std::string vhost);
virtual int on_reload_vhost_removed(std::string vhost); virtual int on_reload_vhost_removed(std::string vhost);

View file

@ -38,15 +38,6 @@ int ISrsLog::initialize()
return ERROR_SUCCESS; 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*/, ...) void ISrsLog::verbose(const char* /*tag*/, int /*context_id*/, const char* /*fmt*/, ...)
{ {
} }

View file

@ -68,11 +68,6 @@ public:
*/ */
virtual int initialize(); virtual int initialize();
public: public:
/**
* defined in SrsLogLevel.
*/
virtual int level();
virtual void set_level(int level);
/** /**
* log for verbose, very verbose information. * log for verbose, very verbose information.
*/ */

View file

@ -98,7 +98,6 @@ int main(int argc, char** argv)
if ((ret = _srs_log->initialize()) != ERROR_SUCCESS) { if ((ret = _srs_log->initialize()) != ERROR_SUCCESS) {
return ret; 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("srs(simple-rtmp-server) "RTMP_SIG_SRS_VERSION);
srs_trace("uname: "SRS_UNAME); srs_trace("uname: "SRS_UNAME);