winlin

add log level in conf. change to 0.9.45

@@ -26,6 +26,10 @@ ff_log_dir ./objs/logs; @@ -26,6 +26,10 @@ ff_log_dir ./objs/logs;
26 # if file, write log to file. requires srs_log_file if log to file. 26 # if file, write log to file. requires srs_log_file if log to file.
27 # default: file. 27 # default: file.
28 srs_log_tank file; 28 srs_log_tank file;
  29 +# the log level, for all log tanks.
  30 +# can be: verbose, info, trace, warn, error
  31 +# defualt: trace
  32 +srs_log_level trace;
29 # when srs_log_tank is file, specifies the log file. 33 # when srs_log_tank is file, specifies the log file.
30 # default: ./objs/srs.log 34 # default: ./objs/srs.log
31 srs_log_file ./objs/srs.log; 35 srs_log_file ./objs/srs.log;
@@ -1288,6 +1288,18 @@ string SrsConfig::get_srs_log_file() @@ -1288,6 +1288,18 @@ string SrsConfig::get_srs_log_file()
1288 return conf->arg0(); 1288 return conf->arg0();
1289 } 1289 }
1290 1290
  1291 +string SrsConfig::get_srs_log_level()
  1292 +{
  1293 + srs_assert(root);
  1294 +
  1295 + SrsConfDirective* conf = root->get("srs_log_level");
  1296 + if (!conf || conf->arg0().empty()) {
  1297 + return "trace";
  1298 + }
  1299 +
  1300 + return conf->arg0();
  1301 +}
  1302 +
1291 bool SrsConfig::get_srs_log_tank_file() 1303 bool SrsConfig::get_srs_log_tank_file()
1292 { 1304 {
1293 srs_assert(root); 1305 srs_assert(root);
@@ -149,6 +149,7 @@ public: @@ -149,6 +149,7 @@ public:
149 virtual std::string get_engine_output(SrsConfDirective* engine); 149 virtual std::string get_engine_output(SrsConfDirective* engine);
150 virtual std::string get_ffmpeg_log_dir(); 150 virtual std::string get_ffmpeg_log_dir();
151 virtual bool get_srs_log_tank_file(); 151 virtual bool get_srs_log_tank_file();
  152 + virtual std::string get_srs_log_level();
152 virtual std::string get_srs_log_file(); 153 virtual std::string get_srs_log_file();
153 virtual bool get_deamon(); 154 virtual bool get_deamon();
154 virtual int get_max_connections(); 155 virtual int get_max_connections();
@@ -587,8 +587,8 @@ int SrsHlsMuxer::segment_open(int64_t segment_start_dts) @@ -587,8 +587,8 @@ int SrsHlsMuxer::segment_open(int64_t segment_start_dts)
587 srs_error("open hls muxer failed. ret=%d", ret); 587 srs_error("open hls muxer failed. ret=%d", ret);
588 return ret; 588 return ret;
589 } 589 }
590 - srs_info("open HLS muxer success. vhost=%s, path=%s, tmp=%s",  
591 - vhost.c_str(), current->full_path.c_str(), tmp_file.c_str()); 590 + srs_info("open HLS muxer success. path=%s, tmp=%s",
  591 + current->full_path.c_str(), tmp_file.c_str());
592 592
593 return ret; 593 return ret;
594 } 594 }
@@ -670,7 +670,7 @@ int SrsHttpUri::initialize(std::string _url) @@ -670,7 +670,7 @@ int SrsHttpUri::initialize(std::string _url)
670 srs_info("parse url %s success", purl); 670 srs_info("parse url %s success", purl);
671 671
672 query = get_uri_field(url, &hp_u, UF_QUERY); 672 query = get_uri_field(url, &hp_u, UF_QUERY);
673 - srs_info("parse query %s success", query); 673 + srs_info("parse query %s success", query.c_str());
674 674
675 return ret; 675 return ret;
676 } 676 }
@@ -61,10 +61,12 @@ int SrsThreadContext::get_id() @@ -61,10 +61,12 @@ int SrsThreadContext::get_id()
61 61
62 SrsFastLog::SrsFastLog() 62 SrsFastLog::SrsFastLog()
63 { 63 {
64 - level = SrsLogLevel::Trace; 64 + _level = SrsLogLevel::Trace;
65 log_data = new char[LOG_MAX_SIZE]; 65 log_data = new char[LOG_MAX_SIZE];
66 66
67 fd = -1; 67 fd = -1;
  68 +
  69 + // TODO: support reload.
68 } 70 }
69 71
70 SrsFastLog::~SrsFastLog() 72 SrsFastLog::~SrsFastLog()
@@ -77,9 +79,19 @@ SrsFastLog::~SrsFastLog() @@ -77,9 +79,19 @@ SrsFastLog::~SrsFastLog()
77 } 79 }
78 } 80 }
79 81
  82 +int SrsFastLog::level()
  83 +{
  84 + return _level;
  85 +}
  86 +
  87 +void SrsFastLog::set_level(int level)
  88 +{
  89 + _level = level;
  90 +}
  91 +
80 void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...) 92 void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...)
81 { 93 {
82 - if (level > SrsLogLevel::Verbose) { 94 + if (_level > SrsLogLevel::Verbose) {
83 return; 95 return;
84 } 96 }
85 97
@@ -94,12 +106,12 @@ void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...) @@ -94,12 +106,12 @@ void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...)
94 size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap); 106 size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
95 va_end(ap); 107 va_end(ap);
96 108
97 - write_log(log_data, size, SrsLogLevel::Verbose); 109 + write_log(fd, log_data, size, SrsLogLevel::Verbose);
98 } 110 }
99 111
100 void SrsFastLog::info(const char* tag, int context_id, const char* fmt, ...) 112 void SrsFastLog::info(const char* tag, int context_id, const char* fmt, ...)
101 { 113 {
102 - if (level > SrsLogLevel::Info) { 114 + if (_level > SrsLogLevel::Info) {
103 return; 115 return;
104 } 116 }
105 117
@@ -114,12 +126,12 @@ void SrsFastLog::info(const char* tag, int context_id, const char* fmt, ...) @@ -114,12 +126,12 @@ void SrsFastLog::info(const char* tag, int context_id, const char* fmt, ...)
114 size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap); 126 size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
115 va_end(ap); 127 va_end(ap);
116 128
117 - write_log(log_data, size, SrsLogLevel::Info); 129 + write_log(fd, log_data, size, SrsLogLevel::Info);
118 } 130 }
119 131
120 void SrsFastLog::trace(const char* tag, int context_id, const char* fmt, ...) 132 void SrsFastLog::trace(const char* tag, int context_id, const char* fmt, ...)
121 { 133 {
122 - if (level > SrsLogLevel::Trace) { 134 + if (_level > SrsLogLevel::Trace) {
123 return; 135 return;
124 } 136 }
125 137
@@ -134,12 +146,12 @@ void SrsFastLog::trace(const char* tag, int context_id, const char* fmt, ...) @@ -134,12 +146,12 @@ void SrsFastLog::trace(const char* tag, int context_id, const char* fmt, ...)
134 size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap); 146 size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
135 va_end(ap); 147 va_end(ap);
136 148
137 - write_log(log_data, size, SrsLogLevel::Trace); 149 + write_log(fd, log_data, size, SrsLogLevel::Trace);
138 } 150 }
139 151
140 void SrsFastLog::warn(const char* tag, int context_id, const char* fmt, ...) 152 void SrsFastLog::warn(const char* tag, int context_id, const char* fmt, ...)
141 { 153 {
142 - if (level > SrsLogLevel::Warn) { 154 + if (_level > SrsLogLevel::Warn) {
143 return; 155 return;
144 } 156 }
145 157
@@ -154,12 +166,12 @@ void SrsFastLog::warn(const char* tag, int context_id, const char* fmt, ...) @@ -154,12 +166,12 @@ void SrsFastLog::warn(const char* tag, int context_id, const char* fmt, ...)
154 size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap); 166 size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
155 va_end(ap); 167 va_end(ap);
156 168
157 - write_log(log_data, size, SrsLogLevel::Warn); 169 + write_log(fd, log_data, size, SrsLogLevel::Warn);
158 } 170 }
159 171
160 void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...) 172 void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...)
161 { 173 {
162 - if (level > SrsLogLevel::Error) { 174 + if (_level > SrsLogLevel::Error) {
163 return; 175 return;
164 } 176 }
165 177
@@ -177,7 +189,7 @@ void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...) @@ -177,7 +189,7 @@ void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...)
177 // add strerror() to error msg. 189 // add strerror() to error msg.
178 size += snprintf(log_data + size, LOG_MAX_SIZE - size, "(%s)", strerror(errno)); 190 size += snprintf(log_data + size, LOG_MAX_SIZE - size, "(%s)", strerror(errno));
179 191
180 - write_log(log_data, size, SrsLogLevel::Error); 192 + write_log(fd, log_data, size, SrsLogLevel::Error);
181 } 193 }
182 194
183 bool SrsFastLog::generate_header(const char* tag, int context_id, const char* level_name, int* header_size) 195 bool SrsFastLog::generate_header(const char* tag, int context_id, const char* level_name, int* header_size)
@@ -219,7 +231,7 @@ bool SrsFastLog::generate_header(const char* tag, int context_id, const char* le @@ -219,7 +231,7 @@ bool SrsFastLog::generate_header(const char* tag, int context_id, const char* le
219 return true; 231 return true;
220 } 232 }
221 233
222 -void SrsFastLog::write_log(char *str_log, int size, int _level) 234 +void SrsFastLog::write_log(int& fd, char *str_log, int size, int level)
223 { 235 {
224 // ensure the tail and EOF of string 236 // ensure the tail and EOF of string
225 // LOG_TAIL_SIZE for the TAIL char. 237 // LOG_TAIL_SIZE for the TAIL char.
@@ -227,8 +239,8 @@ void SrsFastLog::write_log(char *str_log, int size, int _level) @@ -227,8 +239,8 @@ void SrsFastLog::write_log(char *str_log, int size, int _level)
227 size = srs_min(LOG_MAX_SIZE - 1 - LOG_TAIL_SIZE, size); 239 size = srs_min(LOG_MAX_SIZE - 1 - LOG_TAIL_SIZE, size);
228 240
229 // add some to the end of char. 241 // add some to the end of char.
230 - log_data[size++] = LOG_TAIL;  
231 - log_data[size++] = 0; 242 + str_log[size++] = LOG_TAIL;
  243 + str_log[size++] = 0;
232 244
233 if (fd < 0 || !_srs_config->get_srs_log_tank_file()) { 245 if (fd < 0 || !_srs_config->get_srs_log_tank_file()) {
234 // if is error msg, then print color msg. 246 // if is error msg, then print color msg.
@@ -236,9 +248,9 @@ void SrsFastLog::write_log(char *str_log, int size, int _level) @@ -236,9 +248,9 @@ void SrsFastLog::write_log(char *str_log, int size, int _level)
236 // \033[32m : green text code in shell 248 // \033[32m : green text code in shell
237 // \033[33m : yellow text code in shell 249 // \033[33m : yellow text code in shell
238 // \033[0m : normal text code 250 // \033[0m : normal text code
239 - if (_level <= SrsLogLevel::Trace) { 251 + if (level <= SrsLogLevel::Trace) {
240 printf("%s", str_log); 252 printf("%s", str_log);
241 - } else if (_level == SrsLogLevel::Warn) { 253 + } else if (level == SrsLogLevel::Warn) {
242 printf("\033[33m%s\033[0m", str_log); 254 printf("\033[33m%s\033[0m", str_log);
243 } else{ 255 } else{
244 printf("\033[31m%s\033[0m", str_log); 256 printf("\033[31m%s\033[0m", str_log);
@@ -55,30 +55,13 @@ public: @@ -55,30 +55,13 @@ public:
55 }; 55 };
56 56
57 /** 57 /**
58 -* the log level, for example:  
59 -* if specified Debug level, all level messages will be logged.  
60 -* if specified Warn level, only Warn/Error/Fatal level messages will be logged.  
61 -*/  
62 -class SrsLogLevel  
63 -{  
64 -public:  
65 - // only used for very verbose debug, generally,  
66 - // we compile without this level for high performance.  
67 - static const int Verbose = 0x01;  
68 - static const int Info = 0x02;  
69 - static const int Trace = 0x03;  
70 - static const int Warn = 0x04;  
71 - static const int Error = 0x05;  
72 -};  
73 -  
74 -/**  
75 * we use memory/disk cache and donot flush when write log. 58 * we use memory/disk cache and donot flush when write log.
76 */ 59 */
77 class SrsFastLog : public ISrsLog 60 class SrsFastLog : public ISrsLog
78 { 61 {
79 private: 62 private:
80 // defined in SrsLogLevel. 63 // defined in SrsLogLevel.
81 - int level; 64 + int _level;
82 char* log_data; 65 char* log_data;
83 // log to file if specified srs_log_file 66 // log to file if specified srs_log_file
84 int fd; 67 int fd;
@@ -86,6 +69,8 @@ public: @@ -86,6 +69,8 @@ public:
86 SrsFastLog(); 69 SrsFastLog();
87 virtual ~SrsFastLog(); 70 virtual ~SrsFastLog();
88 public: 71 public:
  72 + virtual int level();
  73 + virtual void set_level(int level);
89 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, ...);
90 virtual void info(const char* tag, int context_id, const char* fmt, ...); 75 virtual void info(const char* tag, int context_id, const char* fmt, ...);
91 virtual void trace(const char* tag, int context_id, const char* fmt, ...); 76 virtual void trace(const char* tag, int context_id, const char* fmt, ...);
@@ -93,7 +78,7 @@ public: @@ -93,7 +78,7 @@ public:
93 virtual void error(const char* tag, int context_id, const char* fmt, ...); 78 virtual void error(const char* tag, int context_id, const char* fmt, ...);
94 private: 79 private:
95 virtual bool generate_header(const char* tag, int context_id, const char* level_name, int* header_size); 80 virtual bool generate_header(const char* tag, int context_id, const char* level_name, int* header_size);
96 - virtual void write_log(char* str_log, int size, int _level); 81 + static void write_log(int& fd, char* str_log, int size, int level);
97 }; 82 };
98 83
99 #endif 84 #endif
@@ -149,8 +149,6 @@ int SrsListener::cycle() @@ -149,8 +149,6 @@ int SrsListener::cycle()
149 return ret; 149 return ret;
150 } 150 }
151 151
152 - srs_verbose("accept client finished. conns=%d, ret=%d", (int)conns.size(), ret);  
153 -  
154 return ret; 152 return ret;
155 } 153 }
156 154
@@ -493,13 +491,15 @@ int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd) @@ -493,13 +491,15 @@ int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd)
493 491
494 // directly enqueue, the cycle thread will remove the client. 492 // directly enqueue, the cycle thread will remove the client.
495 conns.push_back(conn); 493 conns.push_back(conn);
496 - srs_verbose("add conn from port %d to vector. conns=%d", port, (int)conns.size()); 494 + srs_verbose("add conn to vector.");
497 495
498 // cycle will start process thread and when finished remove the client. 496 // cycle will start process thread and when finished remove the client.
499 if ((ret = conn->start()) != ERROR_SUCCESS) { 497 if ((ret = conn->start()) != ERROR_SUCCESS) {
500 return ret; 498 return ret;
501 } 499 }
502 - srs_verbose("conn start finished. ret=%d", ret); 500 + srs_verbose("conn started success .");
  501 +
  502 + srs_verbose("accept client finished. conns=%d, ret=%d", (int)conns.size(), ret);
503 503
504 return ret; 504 return ret;
505 } 505 }
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // current release version 31 // current release version
32 #define VERSION_MAJOR "0" 32 #define VERSION_MAJOR "0"
33 #define VERSION_MINOR "9" 33 #define VERSION_MINOR "9"
34 -#define VERSION_REVISION "44" 34 +#define VERSION_REVISION "45"
35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION 35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
36 // server info. 36 // server info.
37 #define RTMP_SIG_SRS_KEY "srs" 37 #define RTMP_SIG_SRS_KEY "srs"
@@ -31,6 +31,15 @@ ISrsLog::~ISrsLog() @@ -31,6 +31,15 @@ ISrsLog::~ISrsLog()
31 { 31 {
32 } 32 }
33 33
  34 +int ISrsLog::level()
  35 +{
  36 + return SrsLogLevel::Trace;
  37 +}
  38 +
  39 +void ISrsLog::set_level(int /*level*/)
  40 +{
  41 +}
  42 +
34 void ISrsLog::verbose(const char* /*tag*/, int /*context_id*/, const char* /*fmt*/, ...) 43 void ISrsLog::verbose(const char* /*tag*/, int /*context_id*/, const char* /*fmt*/, ...)
35 { 44 {
36 } 45 }
@@ -36,6 +36,23 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -36,6 +36,23 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 #include <string.h> 36 #include <string.h>
37 37
38 /** 38 /**
  39 +* the log level, for example:
  40 +* if specified Debug level, all level messages will be logged.
  41 +* if specified Warn level, only Warn/Error/Fatal level messages will be logged.
  42 +*/
  43 +class SrsLogLevel
  44 +{
  45 +public:
  46 + // only used for very verbose debug, generally,
  47 + // we compile without this level for high performance.
  48 + static const int Verbose = 0x01;
  49 + static const int Info = 0x02;
  50 + static const int Trace = 0x03;
  51 + static const int Warn = 0x04;
  52 + static const int Error = 0x05;
  53 +};
  54 +
  55 +/**
39 * the log interface provides method to write log. 56 * the log interface provides method to write log.
40 * but we provides some macro, which enable us to disable the log when compile. 57 * but we provides some macro, which enable us to disable the log when compile.
41 * @see also SmtDebug/SmtTrace/SmtWarn/SmtError which is corresponding to Debug/Trace/Warn/Fatal. 58 * @see also SmtDebug/SmtTrace/SmtWarn/SmtError which is corresponding to Debug/Trace/Warn/Fatal.
@@ -47,6 +64,11 @@ public: @@ -47,6 +64,11 @@ public:
47 virtual ~ISrsLog(); 64 virtual ~ISrsLog();
48 public: 65 public:
49 /** 66 /**
  67 + * defined in SrsLogLevel.
  68 + */
  69 + virtual int level();
  70 + virtual void set_level(int level);
  71 + /**
50 * log for verbose, very verbose information. 72 * log for verbose, very verbose information.
51 */ 73 */
52 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, ...);
@@ -157,6 +157,21 @@ int main(int argc, char** argv) @@ -157,6 +157,21 @@ int main(int argc, char** argv)
157 return ret; 157 return ret;
158 } 158 }
159 159
  160 + // config parsed, initialize log.
  161 + if ("verbose" == _srs_config->get_srs_log_level()) {
  162 + _srs_log->set_level(SrsLogLevel::Verbose);
  163 + } else if ("info" == _srs_config->get_srs_log_level()) {
  164 + _srs_log->set_level(SrsLogLevel::Info);
  165 + } else if ("trace" == _srs_config->get_srs_log_level()) {
  166 + _srs_log->set_level(SrsLogLevel::Trace);
  167 + } else if ("warn" == _srs_config->get_srs_log_level()) {
  168 + _srs_log->set_level(SrsLogLevel::Warn);
  169 + } else if ("error" == _srs_config->get_srs_log_level()) {
  170 + _srs_log->set_level(SrsLogLevel::Error);
  171 + } else {
  172 + _srs_log->set_level(SrsLogLevel::Trace);
  173 + }
  174 +
160 srs_trace("srs(simple-rtmp-server) "RTMP_SIG_SRS_VERSION); 175 srs_trace("srs(simple-rtmp-server) "RTMP_SIG_SRS_VERSION);
161 srs_trace("uname: "SRS_UNAME); 176 srs_trace("uname: "SRS_UNAME);
162 srs_trace("build: %s, %s", SRS_BUILD_DATE, srs_is_little_endian()? "little-endian":"big-endian"); 177 srs_trace("build: %s, %s", SRS_BUILD_DATE, srs_is_little_endian()? "little-endian":"big-endian");