winlin

support reload log level/logfile/tank

@@ -516,6 +516,42 @@ int SrsConfig::reload() @@ -516,6 +516,42 @@ int SrsConfig::reload()
516 } 516 }
517 srs_trace("reload pid success."); 517 srs_trace("reload pid success.");
518 } 518 }
  519 +
  520 + // merge config: srs_log_tank
  521 + if (!srs_directive_equals(root->get("srs_log_tank"), old_root->get("srs_log_tank"))) {
  522 + for (it = subscribes.begin(); it != subscribes.end(); ++it) {
  523 + ISrsReloadHandler* subscribe = *it;
  524 + if ((ret = subscribe->on_reload_log_tank()) != ERROR_SUCCESS) {
  525 + srs_error("notify subscribes reload srs_log_tank failed. ret=%d", ret);
  526 + return ret;
  527 + }
  528 + }
  529 + srs_trace("reload srs_log_tank success.");
  530 + }
  531 +
  532 + // merge config: srs_log_level
  533 + if (!srs_directive_equals(root->get("srs_log_level"), old_root->get("srs_log_level"))) {
  534 + for (it = subscribes.begin(); it != subscribes.end(); ++it) {
  535 + ISrsReloadHandler* subscribe = *it;
  536 + if ((ret = subscribe->on_reload_log_level()) != ERROR_SUCCESS) {
  537 + srs_error("notify subscribes reload srs_log_level failed. ret=%d", ret);
  538 + return ret;
  539 + }
  540 + }
  541 + srs_trace("reload srs_log_level success.");
  542 + }
  543 +
  544 + // merge config: srs_log_file
  545 + if (!srs_directive_equals(root->get("srs_log_file"), old_root->get("srs_log_file"))) {
  546 + for (it = subscribes.begin(); it != subscribes.end(); ++it) {
  547 + ISrsReloadHandler* subscribe = *it;
  548 + if ((ret = subscribe->on_reload_log_file()) != ERROR_SUCCESS) {
  549 + srs_error("notify subscribes reload srs_log_file failed. ret=%d", ret);
  550 + return ret;
  551 + }
  552 + }
  553 + srs_trace("reload srs_log_file success.");
  554 + }
519 555
520 // directly supported for reload: 556 // directly supported for reload:
521 // chunk_size, ff_log_dir 557 // chunk_size, ff_log_dir
@@ -32,6 +32,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -32,6 +32,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 32
33 #include <srs_app_config.hpp> 33 #include <srs_app_config.hpp>
34 #include <srs_kernel_error.hpp> 34 #include <srs_kernel_error.hpp>
  35 +#include <srs_app_utility.hpp>
35 36
36 SrsThreadContext::SrsThreadContext() 37 SrsThreadContext::SrsThreadContext()
37 { 38 {
@@ -66,6 +67,7 @@ SrsFastLog::SrsFastLog() @@ -66,6 +67,7 @@ SrsFastLog::SrsFastLog()
66 log_data = new char[LOG_MAX_SIZE]; 67 log_data = new char[LOG_MAX_SIZE];
67 68
68 fd = -1; 69 fd = -1;
  70 + log_to_file_tank = false;
69 } 71 }
70 72
71 SrsFastLog::~SrsFastLog() 73 SrsFastLog::~SrsFastLog()
@@ -76,22 +78,20 @@ SrsFastLog::~SrsFastLog() @@ -76,22 +78,20 @@ SrsFastLog::~SrsFastLog()
76 ::close(fd); 78 ::close(fd);
77 fd = -1; 79 fd = -1;
78 } 80 }
79 -}  
80 81
81 -int SrsFastLog::initialize()  
82 -{  
83 - // TODO: support reload.  
84 - return ERROR_SUCCESS; 82 + _srs_config->unsubscribe(this);
85 } 83 }
86 84
87 -int SrsFastLog::level() 85 +int SrsFastLog::initialize()
88 { 86 {
89 - return _level;  
90 -} 87 + int ret = ERROR_SUCCESS;
  88 +
  89 + _srs_config->subscribe(this);
91 90
92 -void SrsFastLog::set_level(int level)  
93 -{  
94 - _level = level; 91 + log_to_file_tank = _srs_config->get_srs_log_tank_file();
  92 + _level = srs_get_log_level(_srs_config->get_srs_log_level());
  93 +
  94 + return ret;
95 } 95 }
96 96
97 void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...) 97 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, ...) @@ -197,6 +197,54 @@ void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...)
197 write_log(fd, log_data, size, SrsLogLevel::Error); 197 write_log(fd, log_data, size, SrsLogLevel::Error);
198 } 198 }
199 199
  200 +int SrsFastLog::on_reload_log_tank()
  201 +{
  202 + int ret = ERROR_SUCCESS;
  203 +
  204 + bool tank = log_to_file_tank;
  205 + log_to_file_tank = _srs_config->get_srs_log_tank_file();
  206 +
  207 + if (tank) {
  208 + return ret;
  209 + }
  210 +
  211 + if (!log_to_file_tank) {
  212 + return ret;
  213 + }
  214 +
  215 + if (fd > 0) {
  216 + ::close(fd);
  217 + }
  218 + open_log_file();
  219 +
  220 + return ret;
  221 +}
  222 +
  223 +int SrsFastLog::on_reload_log_level()
  224 +{
  225 + int ret = ERROR_SUCCESS;
  226 +
  227 + _level = srs_get_log_level(_srs_config->get_srs_log_level());
  228 +
  229 + return ret;
  230 +}
  231 +
  232 +int SrsFastLog::on_reload_log_file()
  233 +{
  234 + int ret = ERROR_SUCCESS;
  235 +
  236 + if (!log_to_file_tank) {
  237 + return ret;
  238 + }
  239 +
  240 + if (fd > 0) {
  241 + ::close(fd);
  242 + }
  243 + open_log_file();
  244 +
  245 + return ret;
  246 +}
  247 +
200 bool SrsFastLog::generate_header(const char* tag, int context_id, const char* level_name, int* header_size) 248 bool SrsFastLog::generate_header(const char* tag, int context_id, const char* level_name, int* header_size)
201 { 249 {
202 // clock time 250 // clock time
@@ -247,7 +295,8 @@ void SrsFastLog::write_log(int& fd, char *str_log, int size, int level) @@ -247,7 +295,8 @@ void SrsFastLog::write_log(int& fd, char *str_log, int size, int level)
247 str_log[size++] = LOG_TAIL; 295 str_log[size++] = LOG_TAIL;
248 str_log[size++] = 0; 296 str_log[size++] = 0;
249 297
250 - if (fd < 0 || !_srs_config->get_srs_log_tank_file()) { 298 + // if not to file, to console and return.
  299 + if (!log_to_file_tank) {
251 // if is error msg, then print color msg. 300 // if is error msg, then print color msg.
252 // \033[31m : red text code in shell 301 // \033[31m : red text code in shell
253 // \033[32m : green text code in shell 302 // \033[32m : green text code in shell
@@ -260,24 +309,35 @@ void SrsFastLog::write_log(int& fd, char *str_log, int size, int level) @@ -260,24 +309,35 @@ void SrsFastLog::write_log(int& fd, char *str_log, int size, int level)
260 } else{ 309 } else{
261 printf("\033[31m%s\033[0m", str_log); 310 printf("\033[31m%s\033[0m", str_log);
262 } 311 }
  312 +
  313 + return;
263 } 314 }
264 315
265 // open log file. if specified 316 // open log file. if specified
266 - if (!_srs_config->get_srs_log_file().empty() && fd < 0) {  
267 - std::string filename = _srs_config->get_srs_log_file();  
268 -  
269 - fd = ::open(filename.c_str(), O_RDWR | O_APPEND);  
270 -  
271 - if(fd == -1 && errno == ENOENT) {  
272 - fd = open(filename.c_str(),  
273 - O_RDWR | O_CREAT | O_TRUNC,  
274 - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH  
275 - );  
276 - } 317 + if (fd < 0) {
  318 + open_log_file();
277 } 319 }
278 320
279 // write log to file. 321 // write log to file.
280 - if (fd > 0 && _srs_config->get_srs_log_tank_file()) { 322 + if (fd > 0) {
281 ::write(fd, str_log, size); 323 ::write(fd, str_log, size);
282 } 324 }
283 } 325 }
  326 +
  327 +void SrsFastLog::open_log_file()
  328 +{
  329 + std::string filename = _srs_config->get_srs_log_file();
  330 +
  331 + if (filename.empty()) {
  332 + return;
  333 + }
  334 +
  335 + fd = ::open(filename.c_str(), O_RDWR | O_APPEND);
  336 +
  337 + if(fd == -1 && errno == ENOENT) {
  338 + fd = open(filename.c_str(),
  339 + O_RDWR | O_CREAT | O_TRUNC,
  340 + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH
  341 + );
  342 + }
  343 +}
@@ -32,6 +32,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -32,6 +32,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 32
33 #include <srs_app_st.hpp> 33 #include <srs_app_st.hpp>
34 #include <srs_kernel_log.hpp> 34 #include <srs_kernel_log.hpp>
  35 +#include <srs_app_reload.hpp>
35 36
36 #include <string.h> 37 #include <string.h>
37 38
@@ -57,7 +58,7 @@ public: @@ -57,7 +58,7 @@ public:
57 /** 58 /**
58 * we use memory/disk cache and donot flush when write log. 59 * we use memory/disk cache and donot flush when write log.
59 */ 60 */
60 -class SrsFastLog : public ISrsLog 61 +class SrsFastLog : public ISrsLog, public ISrsReloadHandler
61 { 62 {
62 private: 63 private:
63 // defined in SrsLogLevel. 64 // defined in SrsLogLevel.
@@ -65,21 +66,27 @@ private: @@ -65,21 +66,27 @@ private:
65 char* log_data; 66 char* log_data;
66 // log to file if specified srs_log_file 67 // log to file if specified srs_log_file
67 int fd; 68 int fd;
  69 + // whether log to file tank
  70 + bool log_to_file_tank;
68 public: 71 public:
69 SrsFastLog(); 72 SrsFastLog();
70 virtual ~SrsFastLog(); 73 virtual ~SrsFastLog();
71 public: 74 public:
72 virtual int initialize(); 75 virtual int initialize();
73 - virtual int level();  
74 - virtual void set_level(int level);  
75 virtual void verbose(const char* tag, int context_id, const char* fmt, ...); 76 virtual void verbose(const char* tag, int context_id, const char* fmt, ...);
76 virtual void info(const char* tag, int context_id, const char* fmt, ...); 77 virtual void info(const char* tag, int context_id, const char* fmt, ...);
77 virtual void trace(const char* tag, int context_id, const char* fmt, ...); 78 virtual void trace(const char* tag, int context_id, const char* fmt, ...);
78 virtual void warn(const char* tag, int context_id, const char* fmt, ...); 79 virtual void warn(const char* tag, int context_id, const char* fmt, ...);
79 virtual void error(const char* tag, int context_id, const char* fmt, ...); 80 virtual void error(const char* tag, int context_id, const char* fmt, ...);
  81 +// interface ISrsThreadHandler.
  82 +public:
  83 + virtual int on_reload_log_tank();
  84 + virtual int on_reload_log_level();
  85 + virtual int on_reload_log_file();
80 private: 86 private:
81 virtual bool generate_header(const char* tag, int context_id, const char* level_name, int* header_size); 87 virtual bool generate_header(const char* tag, int context_id, const char* level_name, int* header_size);
82 - static void write_log(int& fd, char* str_log, int size, int level); 88 + virtual void write_log(int& fd, char* str_log, int size, int level);
  89 + virtual void open_log_file();
83 }; 90 };
84 91
85 #endif 92 #endif
@@ -45,6 +45,21 @@ int ISrsReloadHandler::on_reload_pid() @@ -45,6 +45,21 @@ int ISrsReloadHandler::on_reload_pid()
45 return ERROR_SUCCESS; 45 return ERROR_SUCCESS;
46 } 46 }
47 47
  48 +int ISrsReloadHandler::on_reload_log_tank()
  49 +{
  50 + return ERROR_SUCCESS;
  51 +}
  52 +
  53 +int ISrsReloadHandler::on_reload_log_level()
  54 +{
  55 + return ERROR_SUCCESS;
  56 +}
  57 +
  58 +int ISrsReloadHandler::on_reload_log_file()
  59 +{
  60 + return ERROR_SUCCESS;
  61 +}
  62 +
48 int ISrsReloadHandler::on_reload_pithy_print() 63 int ISrsReloadHandler::on_reload_pithy_print()
49 { 64 {
50 return ERROR_SUCCESS; 65 return ERROR_SUCCESS;
@@ -43,6 +43,9 @@ public: @@ -43,6 +43,9 @@ public:
43 public: 43 public:
44 virtual int on_reload_listen(); 44 virtual int on_reload_listen();
45 virtual int on_reload_pid(); 45 virtual int on_reload_pid();
  46 + virtual int on_reload_log_tank();
  47 + virtual int on_reload_log_level();
  48 + virtual int on_reload_log_file();
46 virtual int on_reload_pithy_print(); 49 virtual int on_reload_pithy_print();
47 virtual int on_reload_vhost_added(std::string vhost); 50 virtual int on_reload_vhost_added(std::string vhost);
48 virtual int on_reload_vhost_removed(std::string vhost); 51 virtual int on_reload_vhost_removed(std::string vhost);
@@ -38,15 +38,6 @@ int ISrsLog::initialize() @@ -38,15 +38,6 @@ int ISrsLog::initialize()
38 return ERROR_SUCCESS; 38 return ERROR_SUCCESS;
39 } 39 }
40 40
41 -int ISrsLog::level()  
42 -{  
43 - return SrsLogLevel::Trace;  
44 -}  
45 -  
46 -void ISrsLog::set_level(int /*level*/)  
47 -{  
48 -}  
49 -  
50 void ISrsLog::verbose(const char* /*tag*/, int /*context_id*/, const char* /*fmt*/, ...) 41 void ISrsLog::verbose(const char* /*tag*/, int /*context_id*/, const char* /*fmt*/, ...)
51 { 42 {
52 } 43 }
@@ -69,11 +69,6 @@ public: @@ -69,11 +69,6 @@ public:
69 virtual int initialize(); 69 virtual int initialize();
70 public: 70 public:
71 /** 71 /**
72 - * defined in SrsLogLevel.  
73 - */  
74 - virtual int level();  
75 - virtual void set_level(int level);  
76 - /**  
77 * log for verbose, very verbose information. 72 * log for verbose, very verbose information.
78 */ 73 */
79 virtual void verbose(const char* tag, int context_id, const char* fmt, ...); 74 virtual void verbose(const char* tag, int context_id, const char* fmt, ...);
@@ -98,7 +98,6 @@ int main(int argc, char** argv) @@ -98,7 +98,6 @@ int main(int argc, char** argv)
98 if ((ret = _srs_log->initialize()) != ERROR_SUCCESS) { 98 if ((ret = _srs_log->initialize()) != ERROR_SUCCESS) {
99 return ret; 99 return ret;
100 } 100 }
101 - _srs_log->set_level(srs_get_log_level(_srs_config->get_srs_log_level()));  
102 101
103 srs_trace("srs(simple-rtmp-server) "RTMP_SIG_SRS_VERSION); 102 srs_trace("srs(simple-rtmp-server) "RTMP_SIG_SRS_VERSION);
104 srs_trace("uname: "SRS_UNAME); 103 srs_trace("uname: "SRS_UNAME);